Lowercased fields and use GSON's alternative name to make it work.

This commit is contained in:
Mark Nellemann 2019-08-24 10:52:53 +02:00
parent cb9a603b0e
commit d64e80ecb0
11 changed files with 3184 additions and 3400 deletions

View file

@ -17,7 +17,6 @@ apply plugin: 'maven-publish'
apply plugin: 'com.jfrog.bintray'
//apply from: "${project.projectDir}/gradle/clover.gradle"
version = "${version}-SNAPSHOT"
sourceCompatibility = 1.8
targetCompatibility = 1.8

View file

@ -1,6 +1,6 @@
id = libpaqle
group = biz.nellemann.libs
version = 1.0.0
version = 1.0.1
licenses = ['APACHE-2.0'] // or something else
groovyVersion = 2.5.8
slf4jVersion = 1.7.28

View file

@ -25,6 +25,7 @@ import java.util.List;
import biz.nellemann.libpaqle.pojo.PaqleResponse;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonSyntaxException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -78,25 +79,28 @@ public class Paqle {
}
protected String getJson(List<String> vatNumbers) throws Exception {
String cvrIds = String.join(",", vatNumbers);
String jsonText = get(baseUrl + "/entities?customer=" + company + "&news&cvrIds=" + cvrIds);
log.debug("getEntities() response: " + jsonText);
protected String getJson(String vatNumber) throws Exception {
//String cvrIds = String.join(",", vatNumbers);
String jsonText = get(baseUrl + "/entities?customer=" + company + "&news&vatNumbers=" + "DK"+vatNumber);
log.debug("getJson() response: " + jsonText);
return jsonText;
}
protected PaqleResponse deserializeJson(String json) throws JsonSyntaxException {
Gson gson = new Gson();
//Gson gson = new Gson();
Gson gson = new GsonBuilder()
.setDateFormat("yyyy-MM-dd'T'HH:mm:ssz")
.create();
PaqleResponse response = gson.fromJson(json, PaqleResponse.class);
return response;
}
protected PaqleResponse getResponse(List<String> vatNumbers) {
protected PaqleResponse getResponseByVatNumber(String vatNumber) {
PaqleResponse response;
try {
String json = getJson(vatNumbers);
String json = getJson(vatNumber);
response = deserializeJson(json);
} catch ( Exception e) {
log.error("Error", e);

View file

@ -19,15 +19,24 @@
package biz.nellemann.libpaqle.pojo;
import com.google.gson.annotations.SerializedName;
public class Entity {
@SerializedName(value = "published", alternate = {"Published"})
Boolean published;
@SerializedName(value = "hidden", alternate = {"Hidden"})
Boolean hidden;
Integer OriginalId;
Integer CurrentId;
@SerializedName(value = "type", alternate = {"Type"})
String type;
@SerializedName(value = "countryCode", alternate = {"CountryCode"})
String countryCode;
String PaqleUrl;
@SerializedName(value = "paqleUrl", alternate = {"PaqleUrl"})
String paqleUrl;
/*
"Entities": [

View file

@ -1,16 +1,35 @@
package biz.nellemann.libpaqle.pojo;
import com.google.gson.annotations.SerializedName;
import java.util.Date;
import java.util.List;
public class News {
Integer EntityOriginalId;
String ClusterHash;
String Published;
String LanguageCode;
@SerializedName(value = "entityOriginalId", alternate = {"EntityOriginalId"})
Long entityOriginalId;
@SerializedName(value = "clusterHash", alternate = {"ClusterHash"})
String clusterHash;
@SerializedName(value = "published", alternate = {"Published"})
Date published;
@SerializedName(value = "languageCode", alternate = {"LanguageCode"})
String languageCode;
@SerializedName(value = "sourceName", alternate = {"SourceName"})
String sourceName;
String Url;
List<NewsHeadline> Headline;
@SerializedName(value = "url", alternate = {"Url", "URL"})
String url;
@SerializedName(value = "headline", alternate = {"Headline", "HeadLine"})
List<NewsHeadline> headline;
@SerializedName(value = "extract", alternate = {"Extract"})
List<NewsHeadline> extract;
/*
News": [

View file

@ -1,8 +1,13 @@
package biz.nellemann.libpaqle.pojo;
import com.google.gson.annotations.SerializedName;
public class NewsExtract {
String Text;
Boolean Highlight;
@SerializedName(value = "text", alternate = {"Text"})
String text;
@SerializedName(value = "highlight", alternate = {"Highlight", "HighLight"})
Boolean highlight;
}

View file

@ -1,7 +1,10 @@
package biz.nellemann.libpaqle.pojo;
import com.google.gson.annotations.SerializedName;
public class NewsHeadline {
String Text;
@SerializedName(value = "text", alternate = {"Text"})
String text;
}

View file

@ -1,13 +1,18 @@
package biz.nellemann.libpaqle.pojo;
import com.google.gson.annotations.SerializedName;
import java.util.List;
// https://entityapi.paqle.net/documentation/entities
public class PaqleResponse {
List<Entity> Entities;
List<News> News;
@SerializedName(value = "entities", alternate = {"Entities"})
List<Entity> entities;
@SerializedName(value = "news", alternate = {"News"})
List<News> news;
}

View file

@ -23,18 +23,19 @@ class PaqleSpec extends Specification {
void "test successful parsing of test JSON"() {
setup:
def testFile = new File(getClass().getResource('/test.json').toURI())
def testFile = new File(getClass().getResource('/15027800.json').toURI())
def testJson = testFile.getText('UTF-8')
mockServer.enqueue(new MockResponse().setBody(testJson));
HttpUrl baseUrl = mockServer.url("/");
paqle.baseUrl = baseUrl.toString()
when:
PaqleResponse response = paqle.getResponse(["2955224", "4004110055"])
PaqleResponse response = paqle.getResponseByVatNumber("15027800")
then:
response.Entities.size() == 2
response.News.size() == 100
response.entities.size() == 1
response.news.size() == 100
response.news[0].headline.text.contains('Opkøb skubber til Frejas vækst')
}
// TODO: Test error-cases, 401, 404, timeouts, empty responses, etc.

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff