Autoformat, constants, make the server instance variable
This commit is contained in:
parent
a93cd4692a
commit
af74b269e4
|
@ -19,44 +19,46 @@
|
||||||
|
|
||||||
package biz.nellemann.libcvrapi;
|
package biz.nellemann.libcvrapi;
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
|
|
||||||
import biz.nellemann.libcvrapi.pojo.Company;
|
import biz.nellemann.libcvrapi.pojo.Company;
|
||||||
import com.google.gson.Gson;
|
import com.google.gson.Gson;
|
||||||
|
|
||||||
import com.google.gson.JsonSyntaxException;
|
import com.google.gson.JsonSyntaxException;
|
||||||
import org.slf4j.Logger;
|
|
||||||
import org.slf4j.LoggerFactory;
|
|
||||||
|
|
||||||
import okhttp3.Credentials;
|
import okhttp3.Credentials;
|
||||||
import okhttp3.OkHttpClient;
|
import okhttp3.OkHttpClient;
|
||||||
import okhttp3.Request;
|
import okhttp3.Request;
|
||||||
import okhttp3.Response;
|
import okhttp3.Response;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
public class CvrApi {
|
public class CvrApi {
|
||||||
|
|
||||||
private final static Logger log = LoggerFactory.getLogger(CvrApi.class);
|
private static final Logger log = LoggerFactory.getLogger(CvrApi.class);
|
||||||
private final OkHttpClient client;
|
private final OkHttpClient client = new OkHttpClient();
|
||||||
|
|
||||||
private final String userAgent;
|
private final String userAgent;
|
||||||
private final String authenticationToken;
|
private final String authenticationToken;
|
||||||
|
|
||||||
protected String baseUrl = "https://rest.cvrapi.dk/v1/dk";
|
private static final String BASE_URL = "https://rest.cvrapi.dk/v1/dk";
|
||||||
|
|
||||||
|
|
||||||
public CvrApi(String userAgent, String authenticationToken) {
|
public CvrApi(String userAgent, String authenticationToken) {
|
||||||
this.userAgent = userAgent;
|
this.userAgent = userAgent;
|
||||||
this.authenticationToken = authenticationToken;
|
this.authenticationToken = authenticationToken;
|
||||||
client = new OkHttpClient();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
protected String get(String url) throws IOException, Exception {
|
protected String get(String url) throws Exception {
|
||||||
|
|
||||||
String credential = Credentials.basic(authenticationToken, "");
|
String credential = Credentials.basic(authenticationToken, "");
|
||||||
|
|
||||||
Request request = new Request.Builder().url(url).header("User-Agent", userAgent)
|
Request request = new Request
|
||||||
.header("Authorization", credential).addHeader("Accept", "application/json;").build();
|
.Builder()
|
||||||
|
.url(url)
|
||||||
|
.header("User-Agent", userAgent)
|
||||||
|
.header("Authorization", credential)
|
||||||
|
.addHeader("Accept", "application/json;")
|
||||||
|
.build();
|
||||||
|
|
||||||
Response response = client.newCall(request).execute();
|
Response response = client.newCall(request).execute();
|
||||||
switch (response.code()) {
|
switch (response.code()) {
|
||||||
|
@ -71,7 +73,6 @@ public class CvrApi {
|
||||||
default:
|
default:
|
||||||
throw new Exception("get() - Unknown Error - status code: " + response.code());
|
throw new Exception("get() - Unknown Error - status code: " + response.code());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -82,9 +83,9 @@ public class CvrApi {
|
||||||
* @return
|
* @return
|
||||||
* @throws IOException
|
* @throws IOException
|
||||||
*/
|
*/
|
||||||
protected String getCompanyJson(String vatNumber) throws IOException, Exception {
|
protected String getCompanyJson(String vatNumber) throws Exception {
|
||||||
String response = get(baseUrl + "/company/" + vatNumber);
|
String response = get(BASE_URL + "/company/" + vatNumber);
|
||||||
log.debug("getCompanyJson() response: " + response);
|
log.debug("getCompanyJson() response: {}", response);
|
||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -95,10 +96,9 @@ public class CvrApi {
|
||||||
* @param json
|
* @param json
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
protected Company parseJsonIntoCompany(String json) throws JsonSyntaxException {
|
protected Company parseJsonIntoCompany(String json) {
|
||||||
Gson gson = new Gson();
|
Gson gson = new Gson();
|
||||||
Company company = gson.fromJson(json, Company.class);
|
return gson.fromJson(json, Company.class);
|
||||||
return company;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue