commit
007eac2965
|
@ -19,87 +19,96 @@
|
||||||
|
|
||||||
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 final String BASE_URL;
|
||||||
|
|
||||||
|
|
||||||
public CvrApi(String userAgent, String authenticationToken) {
|
public CvrApi(String userAgent, String authenticationToken) {
|
||||||
this.userAgent = userAgent;
|
this(userAgent, authenticationToken, null);
|
||||||
this.authenticationToken = authenticationToken;
|
}
|
||||||
client = new OkHttpClient();
|
|
||||||
}
|
public CvrApi(String userAgent, String authenticationToken, String baseUrl) {
|
||||||
|
this.userAgent = userAgent;
|
||||||
|
this.authenticationToken = authenticationToken;
|
||||||
|
if(baseUrl == null) {
|
||||||
|
this.BASE_URL = "https://rest.cvrapi.dk/v1/dk";
|
||||||
|
} else {
|
||||||
|
this.BASE_URL = baseUrl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
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()) {
|
||||||
case 200:
|
case 200:
|
||||||
return response.body().string();
|
return response.body().string();
|
||||||
case 401:
|
case 401:
|
||||||
log.warn("get() - 401 - Access Denied");
|
log.warn("get() - 401 - Access Denied");
|
||||||
throw new Exception("Access Denied");
|
throw new Exception("Access Denied");
|
||||||
case 404:
|
case 404:
|
||||||
log.warn("get() - 404 - Not Found");
|
log.warn("get() - 404 - Not Found");
|
||||||
throw new Exception("Not Found");
|
throw new Exception("Not Found");
|
||||||
default:
|
default:
|
||||||
throw new Exception("get() - Unknown Error - status code: " + response.code());
|
throw new Exception("get() - Unknown Error - status code: " + response.code());
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Query the API: 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
|
||||||
* @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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Use GSON to deserialize JSON into POJO's.
|
* Use GSON to deserialize JSON into POJO's.
|
||||||
*
|
*
|
||||||
* @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;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -108,22 +117,22 @@ public class CvrApi {
|
||||||
* @param vatNumber
|
* @param vatNumber
|
||||||
* @return
|
* @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) {
|
} catch (JsonSyntaxException e) {
|
||||||
log.error("Error parsing JSON", e);
|
log.error("Error parsing JSON", e);
|
||||||
return null;
|
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("Unknown Error", e);
|
log.error("Unknown Error", e);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,8 +12,7 @@ class CvrApiSpec extends Specification {
|
||||||
MockWebServer mockServer = new MockWebServer();
|
MockWebServer mockServer = new MockWebServer();
|
||||||
|
|
||||||
def setup() {
|
def setup() {
|
||||||
api = new CvrApi("Test User Agent", "testAuthToken")
|
mockServer.start()
|
||||||
mockServer.start();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
def cleanup() {
|
def cleanup() {
|
||||||
|
@ -22,6 +21,7 @@ class CvrApiSpec extends Specification {
|
||||||
|
|
||||||
void "test unsuccessful parsing of JSON"() {
|
void "test unsuccessful parsing of JSON"() {
|
||||||
setup:
|
setup:
|
||||||
|
api = new CvrApi("Test User Agent", "testAuthToken")
|
||||||
def testJson = "{'foo':'bar'}"
|
def testJson = "{'foo':'bar'}"
|
||||||
|
|
||||||
when:
|
when:
|
||||||
|
@ -33,6 +33,7 @@ class CvrApiSpec extends Specification {
|
||||||
|
|
||||||
void "test unsuccessful parsing of JSON w. VAT as text"() {
|
void "test unsuccessful parsing of JSON w. VAT as text"() {
|
||||||
setup:
|
setup:
|
||||||
|
api = new CvrApi("Test User Agent", "testAuthToken")
|
||||||
def testJson = "{'vat':'abcd1234'}"
|
def testJson = "{'vat':'abcd1234'}"
|
||||||
|
|
||||||
when:
|
when:
|
||||||
|
@ -45,6 +46,7 @@ class CvrApiSpec extends Specification {
|
||||||
void "test successful parsing company with CVR 25063864"() {
|
void "test successful parsing company with CVR 25063864"() {
|
||||||
|
|
||||||
setup:
|
setup:
|
||||||
|
api = new CvrApi("Test User Agent", "testAuthToken")
|
||||||
def testFile = new File(getClass().getResource('/25063864.json').toURI())
|
def testFile = new File(getClass().getResource('/25063864.json').toURI())
|
||||||
def testJson = testFile.getText('UTF-8')
|
def testJson = testFile.getText('UTF-8')
|
||||||
|
|
||||||
|
@ -63,6 +65,7 @@ class CvrApiSpec extends Specification {
|
||||||
void "test successful parsing of company with CVR 15027800"() {
|
void "test successful parsing of company with CVR 15027800"() {
|
||||||
|
|
||||||
setup:
|
setup:
|
||||||
|
api = new CvrApi("Test User Agent", "testAuthToken")
|
||||||
def testFile = new File(getClass().getResource('/15027800.json').toURI())
|
def testFile = new File(getClass().getResource('/15027800.json').toURI())
|
||||||
def testJson = testFile.getText('UTF-8')
|
def testJson = testFile.getText('UTF-8')
|
||||||
|
|
||||||
|
@ -83,7 +86,7 @@ class CvrApiSpec extends Specification {
|
||||||
setup:
|
setup:
|
||||||
mockServer.enqueue(new MockResponse().setBody("{}"));
|
mockServer.enqueue(new MockResponse().setBody("{}"));
|
||||||
HttpUrl baseUrl = mockServer.url("/v1/dk/");
|
HttpUrl baseUrl = mockServer.url("/v1/dk/");
|
||||||
api.baseUrl = baseUrl.toString()
|
api = new CvrApi("Test User Agent", "testAuthToken", baseUrl.toString())
|
||||||
|
|
||||||
when:
|
when:
|
||||||
def jsonString = api.getCompanyJson("15027800")
|
def jsonString = api.getCompanyJson("15027800")
|
||||||
|
@ -100,7 +103,7 @@ class CvrApiSpec extends Specification {
|
||||||
def testJson = testFile.getText('UTF-8')
|
def testJson = testFile.getText('UTF-8')
|
||||||
mockServer.enqueue(new MockResponse().setBody(testJson));
|
mockServer.enqueue(new MockResponse().setBody(testJson));
|
||||||
HttpUrl baseUrl = mockServer.url("/v1/dk/");
|
HttpUrl baseUrl = mockServer.url("/v1/dk/");
|
||||||
api.baseUrl = baseUrl.toString()
|
api = new CvrApi("Test User Agent", "testAuthToken", baseUrl.toString())
|
||||||
|
|
||||||
when:
|
when:
|
||||||
def company = api.getCompanyByVatNumber("25063864")
|
def company = api.getCompanyByVatNumber("25063864")
|
||||||
|
@ -114,7 +117,7 @@ class CvrApiSpec extends Specification {
|
||||||
setup:
|
setup:
|
||||||
mockServer.enqueue(new MockResponse().setBody("{'foo':'bar'}"));
|
mockServer.enqueue(new MockResponse().setBody("{'foo':'bar'}"));
|
||||||
HttpUrl baseUrl = mockServer.url("/v1/dk/");
|
HttpUrl baseUrl = mockServer.url("/v1/dk/");
|
||||||
api.baseUrl = baseUrl.toString()
|
api = new CvrApi("Test User Agent", "testAuthToken", baseUrl.toString())
|
||||||
|
|
||||||
when:
|
when:
|
||||||
def company = api.getCompanyByVatNumber("15027800")
|
def company = api.getCompanyByVatNumber("15027800")
|
||||||
|
@ -127,7 +130,7 @@ class CvrApiSpec extends Specification {
|
||||||
setup:
|
setup:
|
||||||
mockServer.enqueue(new MockResponse().setBody(''));
|
mockServer.enqueue(new MockResponse().setBody(''));
|
||||||
HttpUrl baseUrl = mockServer.url("/v1/dk/");
|
HttpUrl baseUrl = mockServer.url("/v1/dk/");
|
||||||
api.baseUrl = baseUrl.toString()
|
api = new CvrApi("Test User Agent", "testAuthToken", baseUrl.toString())
|
||||||
|
|
||||||
when:
|
when:
|
||||||
def company = api.getCompanyByVatNumber("15027800")
|
def company = api.getCompanyByVatNumber("15027800")
|
||||||
|
@ -140,6 +143,7 @@ class CvrApiSpec extends Specification {
|
||||||
setup:
|
setup:
|
||||||
mockServer.enqueue(new MockResponse().setResponseCode(404));
|
mockServer.enqueue(new MockResponse().setResponseCode(404));
|
||||||
HttpUrl baseUrl = mockServer.url("/v1/dk/");
|
HttpUrl baseUrl = mockServer.url("/v1/dk/");
|
||||||
|
api = new CvrApi("Test User Agent", "testAuthToken", baseUrl.toString())
|
||||||
|
|
||||||
when:
|
when:
|
||||||
def company = api.get(baseUrl.toString())
|
def company = api.get(baseUrl.toString())
|
||||||
|
@ -153,7 +157,7 @@ class CvrApiSpec extends Specification {
|
||||||
setup:
|
setup:
|
||||||
mockServer.enqueue(new MockResponse().setResponseCode(401));
|
mockServer.enqueue(new MockResponse().setResponseCode(401));
|
||||||
HttpUrl baseUrl = mockServer.url("/v1/dk/");
|
HttpUrl baseUrl = mockServer.url("/v1/dk/");
|
||||||
api.baseUrl = baseUrl.toString()
|
api = new CvrApi("Test User Agent", "testAuthToken", baseUrl.toString())
|
||||||
|
|
||||||
when:
|
when:
|
||||||
def company = api.get(baseUrl.toString())
|
def company = api.get(baseUrl.toString())
|
||||||
|
@ -167,7 +171,7 @@ class CvrApiSpec extends Specification {
|
||||||
setup:
|
setup:
|
||||||
mockServer.enqueue(new MockResponse().setResponseCode(405));
|
mockServer.enqueue(new MockResponse().setResponseCode(405));
|
||||||
HttpUrl baseUrl = mockServer.url("/v1/dk/");
|
HttpUrl baseUrl = mockServer.url("/v1/dk/");
|
||||||
api.baseUrl = baseUrl.toString()
|
api = new CvrApi("Test User Agent", "testAuthToken", baseUrl.toString())
|
||||||
|
|
||||||
when:
|
when:
|
||||||
def company = api.get(baseUrl.toString())
|
def company = api.get(baseUrl.toString())
|
||||||
|
|
Loading…
Reference in a new issue