This commit is contained in:
Mark Nellemann 2019-08-20 07:52:24 +02:00
parent 2286658446
commit 9df016a5f4
1 changed files with 23 additions and 9 deletions

View File

@ -40,12 +40,14 @@ public class CvrApi {
private final String userAgent; private final String userAgent;
private final String authenticationToken; private final String authenticationToken;
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(); client = new OkHttpClient();
} }
private String get(String url) throws IOException, Exception { private String get(String url) throws IOException, Exception {
String credential = Credentials.basic(authenticationToken, ""); String credential = Credentials.basic(authenticationToken, "");
@ -55,18 +57,19 @@ public class CvrApi {
Response response = client.newCall(request).execute(); Response response = client.newCall(request).execute();
switch (response.code()) { switch (response.code()) {
case 200: case 200:
return response.body().string(); return response.body().string();
case 401: case 401:
throw new Exception("Access denied"); throw new Exception("Access denied");
default: default:
throw new Exception("Unknown error, status code: " + response.code()); throw new Exception("Unknown error, status code: " + response.code());
} }
} }
/** /**
* https://rest.cvrapi.dk/v1/dk/company/vatNumber * Query the API: https://rest.cvrapi.dk/v1/dk/company/vatNumber
* *
* @param vatNumber * @param vatNumber
* @return * @return
@ -77,8 +80,9 @@ public class CvrApi {
return response; return response;
} }
/** /**
* Use GSON to deserialize JSON into objects. * Use GSON to deserialize JSON into POJO's.
* *
* @param json * @param json
* @return * @return
@ -89,16 +93,26 @@ public class CvrApi {
return company; return company;
} }
/**
* Lookup company by VAT number
*
* @param vatNumber
* @return
*/
public Company getCompanyByVatNumber(String vatNumber) { public Company getCompanyByVatNumber(String vatNumber) {
try { try {
String json = getCompanyJson(vatNumber); String json = getCompanyJson(vatNumber);
return parseJsonIntoCompany(json); return parseJsonIntoCompany(json);
} catch (JsonSyntaxException e) {
log.error("Error parsing JSON", e);
return null;
} catch (IOException e) { } catch (IOException e) {
log.error("IO Error", e); log.error("IO Error", e);
return null; return null;
} catch (Exception e) { } catch (Exception e) {
log.error("Other Error", e); log.error("Unknown Error", e);
return null; return null;
} }