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