Autoformat, constants, make the server instance variable

This commit is contained in:
mrok 2019-09-29 23:33:22 +02:00
parent a93cd4692a
commit af74b269e4
1 changed files with 68 additions and 68 deletions

View File

@ -19,87 +19,87 @@
package biz.nellemann.libcvrapi;
import java.io.IOException;
import biz.nellemann.libcvrapi.pojo.Company;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import okhttp3.Credentials;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
public class CvrApi {
private final static Logger log = LoggerFactory.getLogger(CvrApi.class);
private final OkHttpClient client;
private static final Logger log = LoggerFactory.getLogger(CvrApi.class);
private final OkHttpClient client = new OkHttpClient();
private final String userAgent;
private final String authenticationToken;
private final String userAgent;
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) {
this.userAgent = userAgent;
this.authenticationToken = authenticationToken;
client = new OkHttpClient();
}
public CvrApi(String userAgent, String authenticationToken) {
this.userAgent = userAgent;
this.authenticationToken = authenticationToken;
}
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)
.header("Authorization", credential).addHeader("Accept", "application/json;").build();
Request request = new Request
.Builder()
.url(url)
.header("User-Agent", userAgent)
.header("Authorization", credential)
.addHeader("Accept", "application/json;")
.build();
Response response = client.newCall(request).execute();
switch (response.code()) {
case 200:
return response.body().string();
case 401:
log.warn("get() - 401 - Access Denied");
throw new Exception("Access Denied");
Response response = client.newCall(request).execute();
switch (response.code()) {
case 200:
return response.body().string();
case 401:
log.warn("get() - 401 - Access Denied");
throw new Exception("Access Denied");
case 404:
log.warn("get() - 404 - Not Found");
throw new Exception("Not Found");
default:
throw new Exception("get() - Unknown Error - status code: " + response.code());
}
}
default:
throw new Exception("get() - Unknown Error - status code: " + response.code());
}
}
/**
* Query the API: https://rest.cvrapi.dk/v1/dk/company/vatNumber
*
* @param vatNumber
* @return
* @throws IOException
*/
protected String getCompanyJson(String vatNumber) throws IOException, Exception {
String response = get(baseUrl + "/company/" + vatNumber);
log.debug("getCompanyJson() response: " + response);
return response;
}
/**
* Query the API: https://rest.cvrapi.dk/v1/dk/company/vatNumber
*
* @param vatNumber
* @return
* @throws IOException
*/
protected String getCompanyJson(String vatNumber) throws Exception {
String response = get(BASE_URL + "/company/" + vatNumber);
log.debug("getCompanyJson() response: {}", response);
return response;
}
/**
* Use GSON to deserialize JSON into POJO's.
*
* @param json
* @return
*/
protected Company parseJsonIntoCompany(String json) throws JsonSyntaxException {
Gson gson = new Gson();
Company company = gson.fromJson(json, Company.class);
return company;
}
/**
* Use GSON to deserialize JSON into POJO's.
*
* @param json
* @return
*/
protected Company parseJsonIntoCompany(String json) {
Gson gson = new Gson();
return gson.fromJson(json, Company.class);
}
/**
@ -108,22 +108,22 @@ public class CvrApi {
* @param vatNumber
* @return
*/
public Company getCompanyByVatNumber(String vatNumber) {
public Company getCompanyByVatNumber(String vatNumber) {
try {
String json = getCompanyJson(vatNumber);
return parseJsonIntoCompany(json);
try {
String json = getCompanyJson(vatNumber);
return parseJsonIntoCompany(json);
} catch (JsonSyntaxException e) {
log.error("Error parsing JSON", e);
return null;
} catch (IOException e) {
log.error("IO Error", e);
return null;
} catch (Exception e) {
log.error("Unknown Error", e);
return null;
}
return null;
} catch (IOException e) {
log.error("IO Error", e);
return null;
} catch (Exception e) {
log.error("Unknown Error", e);
return null;
}
}
}
}