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

140 lines
3.7 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-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-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;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
2021-11-03 14:48:13 +00:00
import java.util.Objects;
2019-08-17 17:53:11 +00:00
public class CvrApi {
private static final Logger log = LoggerFactory.getLogger(CvrApi.class);
private final OkHttpClient client = new OkHttpClient();
2019-08-19 08:00:01 +00:00
private final String userAgent;
private final String authenticationToken;
2019-08-17 17:53:11 +00:00
2019-10-25 14:57:15 +00:00
private final String BASE_URL;
2019-08-23 11:11:33 +00:00
2019-08-20 05:52:24 +00:00
public CvrApi(String userAgent, String authenticationToken) {
2019-10-25 14:57:15 +00:00
this(userAgent, authenticationToken, null);
}
public CvrApi(String userAgent, String authenticationToken, String baseUrl) {
this.userAgent = userAgent;
this.authenticationToken = authenticationToken;
2019-10-25 14:57:15 +00:00
if(baseUrl == null) {
this.BASE_URL = "https://rest.cvrapi.dk/v1/dk";
} else {
this.BASE_URL = baseUrl;
}
}
2019-08-17 17:53:11 +00:00
2019-08-20 05:52:24 +00:00
protected String get(String url) throws Exception {
2019-08-17 17:53:11 +00:00
String credential = Credentials.basic(authenticationToken, "");
2019-08-17 17:53:11 +00:00
Request request = new Request
.Builder()
.url(url)
.header("User-Agent", userAgent)
.header("Authorization", credential)
.addHeader("Accept", "application/json;")
.build();
2019-08-19 08:00:01 +00:00
2023-01-10 08:48:11 +00:00
try (Response response = client.newCall(request).execute()) {
switch (response.code()) {
case 200:
return Objects.requireNonNull(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());
}
}
}
/**
* Query the API: https://rest.cvrapi.dk/v1/dk/company/vatNumber
*
2021-11-03 14:48:13 +00:00
* @param vatNumber Danish VAT number
* @return String with JSON
*/
protected String getCompanyJson(String vatNumber) throws Exception {
String response = get(BASE_URL + "/company/" + vatNumber);
log.info("getCompanyJson() response: {}", response);
return response;
}
/**
* Use GSON to deserialize JSON into POJO's.
*
2021-11-03 14:48:13 +00:00
* @param json String of JSON
* @return Company object
*/
protected Company parseJsonIntoCompany(String json) {
Gson gson = new Gson();
return gson.fromJson(json, Company.class);
}
2019-08-17 17:53:11 +00:00
2019-08-20 05:52:24 +00:00
/**
* Lookup company by VAT number
*
2021-11-03 14:48:13 +00:00
* @param vatNumber Danish VAT number as String
* @return Company object
2019-08-20 05:52:24 +00:00
*/
public Company getCompanyByVatNumber(String vatNumber) {
2019-08-17 17:53:11 +00:00
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;
} catch (IOException e) {
log.error("IO Error", e);
return null;
} catch (Exception e) {
log.error("Unknown Error", e);
return null;
}
}
2019-08-17 17:53:11 +00:00
}