libcvrapi/src/main/java/biz/nellemann/libcvrapi/CvrApi.java

124 lines
3.5 KiB
Java
Raw Normal View History

2019-08-17 17:53:11 +00:00
/*
2019-08-19 08:00:01 +00:00
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
2019-08-19 18:58:01 +00:00
*
2019-08-19 08:00:01 +00:00
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
2019-08-17 17:53:11 +00:00
*/
package biz.nellemann.libcvrapi;
2019-08-19 08:00:01 +00:00
import java.io.IOException;
2019-08-23 06:18:13 +00:00
import biz.nellemann.libcvrapi.pojo.Company;
2019-08-19 08:00:01 +00:00
import com.google.gson.Gson;
2019-08-19 18:58:01 +00:00
import com.google.gson.JsonSyntaxException;
2019-08-17 17:53:11 +00:00
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
2019-08-19 08:00:01 +00:00
import okhttp3.Credentials;
2019-08-17 17:53:11 +00:00
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
public class CvrApi {
private final static Logger log = LoggerFactory.getLogger(CvrApi.class);
private final OkHttpClient client;
2019-08-19 08:00:01 +00:00
2019-08-17 17:53:11 +00:00
private final String userAgent;
private final String authenticationToken;
2019-08-20 05:52:24 +00:00
2019-08-17 17:53:11 +00:00
public CvrApi(String userAgent, String authenticationToken) {
this.userAgent = userAgent;
this.authenticationToken = authenticationToken;
client = new OkHttpClient();
}
2019-08-20 05:52:24 +00:00
2019-08-17 17:53:11 +00:00
private String get(String url) throws IOException, Exception {
String credential = Credentials.basic(authenticationToken, "");
2019-08-19 08:00:01 +00:00
Request request = new Request.Builder().url(url).header("User-Agent", userAgent)
.header("Authorization", credential).addHeader("Accept", "application/json;").build();
2019-08-17 17:53:11 +00:00
Response response = client.newCall(request).execute();
2019-08-19 08:00:01 +00:00
switch (response.code()) {
2019-08-20 05:52:24 +00:00
case 200:
return response.body().string();
case 401:
throw new Exception("Access denied");
default:
throw new Exception("Unknown error, status code: " + response.code());
2019-08-17 17:53:11 +00:00
}
}
2019-08-19 08:00:01 +00:00
2019-08-20 05:52:24 +00:00
2019-08-17 17:53:11 +00:00
/**
2019-08-20 05:52:24 +00:00
* Query the API: https://rest.cvrapi.dk/v1/dk/company/vatNumber
2019-08-19 18:58:01 +00:00
*
2019-08-17 17:53:11 +00:00
* @param vatNumber
* @return
* @throws IOException
*/
protected String getCompanyJson(String vatNumber) throws IOException, Exception {
2019-08-19 08:00:01 +00:00
String response = get("https://rest.cvrapi.dk/v1/dk/company/" + vatNumber);
2019-08-23 06:18:13 +00:00
log.debug("getCompanyJson() response: " + response);
2019-08-19 08:00:01 +00:00
return response;
2019-08-17 17:53:11 +00:00
}
2019-08-20 05:52:24 +00:00
2019-08-17 17:53:11 +00:00
/**
2019-08-20 05:52:24 +00:00
* Use GSON to deserialize JSON into POJO's.
2019-08-19 18:58:01 +00:00
*
2019-08-17 17:53:11 +00:00
* @param json
* @return
*/
2019-08-19 18:58:01 +00:00
protected Company parseJsonIntoCompany(String json) throws JsonSyntaxException {
Gson gson = new Gson();
Company company = gson.fromJson(json, Company.class);
2019-08-17 17:53:11 +00:00
return company;
}
2019-08-20 05:52:24 +00:00
/**
* Lookup company by VAT number
*
* @param vatNumber
* @return
*/
2019-08-17 17:53:11 +00:00
public Company getCompanyByVatNumber(String vatNumber) {
try {
String json = getCompanyJson(vatNumber);
return parseJsonIntoCompany(json);
2019-08-20 05:52:24 +00:00
} catch (JsonSyntaxException e) {
log.error("Error parsing JSON", e);
return null;
2019-08-17 17:53:11 +00:00
} catch (IOException e) {
log.error("IO Error", e);
return null;
} catch (Exception e) {
2019-08-20 05:52:24 +00:00
log.error("Unknown Error", e);
2019-08-17 17:53:11 +00:00
return null;
}
2019-08-19 08:00:01 +00:00
2019-08-17 17:53:11 +00:00
}
}