Update README and getResponse().

This commit is contained in:
Mark Nellemann 2019-08-23 15:44:31 +02:00
parent 7e349f3d35
commit cb9a603b0e
3 changed files with 26 additions and 35 deletions

View File

@ -6,8 +6,8 @@ A Java library to query Paqle. Work in progress.
## Usage
TODO.
Paqle paqle = new Paqle("MyCompany", "myUsername", "myPassword");
PaqleResponse response = paqle.getResponse(["57020415"]);
### Gradle
@ -17,7 +17,6 @@ A Java library to query Paqle. Work in progress.
dependencies {
compile 'biz.nellemann.libs:libpaqle:1.+'
runtime "org.slf4j:slf4j-simple:1.7.28"
}
## Development

View File

@ -78,12 +78,7 @@ public class Paqle {
}
/**
* @param vatNumbers
* @return
* @throws IOException
*/
protected String getJson(List<String> vatNumbers) throws IOException, Exception {
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);
@ -91,16 +86,23 @@ public class Paqle {
}
/**
* Use GSON to deserialize JSON into POJO's.
*
* @param json
* @return
*/
protected PaqleResponse getResponse(String json) throws JsonSyntaxException {
Gson gson = new Gson();
PaqleResponse response = gson.fromJson(json, PaqleResponse.class);
return response;
protected PaqleResponse deserializeJson(String json) throws JsonSyntaxException {
Gson gson = new Gson();
PaqleResponse response = gson.fromJson(json, PaqleResponse.class);
return response;
}
protected PaqleResponse getResponse(List<String> vatNumbers) {
PaqleResponse response;
try {
String json = getJson(vatNumbers);
response = deserializeJson(json);
} catch ( Exception e) {
log.error("Error", e);
return null;
}
return response;
}

View File

@ -20,33 +20,23 @@ class PaqleSpec extends Specification {
mockServer.shutdown()
}
void "test succesfull parsing of test JSON"() {
void "test successful parsing of test JSON"() {
setup:
def testFile = new File(getClass().getResource('/test.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(testJson)
PaqleResponse response = paqle.getResponse(["2955224", "4004110055"])
then:
response.Entities.size() == 2
response.News.size() == 100
}
void "test succesful HTTP GET 15027800"() {
setup:
mockServer.enqueue(new MockResponse().setBody("{}"));
HttpUrl baseUrl = mockServer.url("/");
paqle.baseUrl = baseUrl.toString()
when:
def jsonString = paqle.getJson(["15027800"])
then:
jsonString != null
jsonString == "{}"
}
// TODO: Test error-cases, 401, 404, timeouts, empty responses, etc.
}