Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add object mapping for meta and dictionaries #127

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,31 @@ Destination[] directDestinations = amadeus.airport.directDestinations.get(Params
// body can be a String version of your JSON or a JsonObject
FlightAvailability[] flightAvailabilities
= amadeus.shopping.availability.flightAvailabilities.post(body);

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that's the right place to add the example. Maybe in another section of the README such as the "Response", we could mention the support of dictionaries and meta:
Screenshot 2022-02-23 at 13 10 41

// Add objet mapping for meta and dictionaries
// eg. for dictionaries - Take Flight Offers Search as an example
FlightOfferSearch[] flightOffers = amadeus.shopping.flightOffers.post(body);
Dictionary flightOffersDic = flightOffers[0].getDictionaries();
// get locations in dictionaries
Map<String, Dictionary.LocationValue> locations = flightOffersDic.getLocations();
// get cityCode in the location (same for CountryCode)
String cityCode = flightOffersDic.getLocations().get("MAD").getCityCode();
// get aircraft in dictionaries
Map<String, String> aircraft = flightOffersDic.getAircraft();
// get currencies in dictionaries
Map<String, String> currencies = flightOffersDic.getCurrencies();
// get carriers in dictionaries
Map<String, String> carriers = flightOffersDic.getCarriers();

// eg. for meta - Take Airport Routes as an example
Destination[] directDestinations = amadeus.airport.directDestinations.get(Params
.with("departureAirportCode","MAD")
.and("max","5"));
CollectionMeta meta = directDestinations[0].getMeta();
// get count in meta
int metaCount = meta.getCount();
// get links in meta
String selfLink = meta.getLinks().getSelf();
```

## Development & Contributing
Expand Down
25 changes: 25 additions & 0 deletions src/main/java/com/amadeus/Response.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.amadeus.exceptions.ParserException;
import com.amadeus.exceptions.ResponseException;
import com.amadeus.exceptions.ServerException;
import com.amadeus.resources.Dictionary;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
Expand Down Expand Up @@ -52,6 +53,14 @@ public class Response {
* The actual Request object used to make this API call.
*/
private @Getter Request request;
/**
* The meta extracted from the JSON data - if the body contained JSON.
*/
private @Getter JsonElement meta;
/**
* The dictionaries extracted from the JSON data - if the body contained JSON.
*/
private @Getter JsonElement dictionaries;

protected Response(Request request) {
this.request = request;
Expand Down Expand Up @@ -120,6 +129,22 @@ private void parseData(HTTPClient client) {
this.warnings = result.get("warnings").getAsJsonObject();
}
}
if (parsed && result.has("meta")) {
if (result.get("meta").isJsonArray()) {
this.meta = result.get("meta").getAsJsonArray();
}
if (result.get("meta").isJsonObject()) {
this.meta = result.get("meta").getAsJsonObject();
}
}
if (parsed && result.has("dictionaries")) {
if (result.get("dictionaries").isJsonArray()) {
this.dictionaries = result.get("dictionaries").getAsJsonArray();
}
if (result.get("dictionaries").isJsonObject()) {
this.dictionaries = result.get("dictionaries").getAsJsonObject();
}
}
}

// Tries to read the body.
Expand Down
29 changes: 29 additions & 0 deletions src/main/java/com/amadeus/resources/CollectionMeta.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.amadeus.resources;

import com.amadeus.Response;
import lombok.Getter;
import lombok.ToString;

/**
* A CollectionMeta Object Mapping.
* @see Response#getMeta()
*/
@ToString
public class CollectionMeta extends Resource {
protected CollectionMeta() {}

private @Getter int count;
private @Getter CollectionLinks links;

@ToString
public class CollectionLinks {
protected CollectionLinks() {}

private @Getter String self;
private @Getter String next;
private @Getter String previous;
private @Getter String last;
private @Getter String first;
private @Getter String up;
}
}
28 changes: 28 additions & 0 deletions src/main/java/com/amadeus/resources/Dictionary.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.amadeus.resources;

import com.amadeus.Response;
import java.util.Map;
import lombok.Getter;
import lombok.ToString;

/**
* A Dictionary Object which is mapping.
* @see Response#getDictionaries()
*/
@ToString
public class Dictionary extends Resource {
protected Dictionary() {}

private @Getter Map<String, LocationValue> locations;
private @Getter Map<String, String> aircraft;
private @Getter Map<String, String> currencies;
private @Getter Map<String, String> carriers;

@ToString
public class LocationValue {
protected LocationValue() {}

private @Getter String cityCode;
private @Getter String countryCode;
}
}
12 changes: 12 additions & 0 deletions src/main/java/com/amadeus/resources/Resource.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ protected Resource() {}
* @hide as only used internally
*/
private transient @Getter Class deSerializationClass;
/**
* The original meta that this object is populated from.
*/
private transient @Getter CollectionMeta meta;
/**
* The original dictionaries that this object is populated from.
*/
private transient @Getter Dictionary dictionaries;

/**
* Turns a response into a Gson deserialized array of resources,
Expand All @@ -32,6 +40,8 @@ public static Resource[] fromArray(Response response, Class klass) {
for (Resource resource : resources) {
resource.response = response;
resource.deSerializationClass = klass;
resource.meta = gson.fromJson(response.getMeta(), CollectionMeta.class);
resource.dictionaries = gson.fromJson(response.getDictionaries(), Dictionary.class);
}
return resources;
}
Expand All @@ -46,6 +56,8 @@ public static Resource fromObject(Response response, Class klass) {
Resource resource = (Resource) gson.fromJson(response.getData(), klass);
resource.response = response;
resource.deSerializationClass = klass;
resource.meta = gson.fromJson(response.getMeta(), CollectionMeta.class);
resource.dictionaries = gson.fromJson(response.getDictionaries(), Dictionary.class);
return resource;
}
}
42 changes: 32 additions & 10 deletions src/test/java/com/amadeus/ResponseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,35 +46,45 @@ public class ResponseTest {
when(connection.getResponseCode()).thenReturn(200);
when(connection.getHeaderField("Content-Type")).thenReturn(
"application/json");
when(connection.getInputStream()).thenReturn(
new ByteArrayInputStream("{ \"data\": []}".getBytes()));
when(connection.getInputStream()).thenReturn(new ByteArrayInputStream(
"{ \"meta\":{},\"data\": [], \"dictionaries\":{}}".getBytes()));

response.parse(client);

assertEquals(response.getStatusCode(), 200);
assertEquals(response.getBody(), "{ \"data\": []}");
assertEquals(response.getBody(), "{ \"meta\":{},\"data\": [], \"dictionaries\":{}}");
assertTrue(response.isParsed());
assertNotNull(response.getResult());
assertNotNull(response.getData());
assertNotNull(response.getMeta());
assertNotNull(response.getDictionaries());
}

@Test public void testParseObjectData() throws IOException {
when(connection.getResponseCode()).thenReturn(200);
when(connection.getHeaderField("Content-Type")).thenReturn(
"application/json");
when(connection.getInputStream()).thenReturn(
new ByteArrayInputStream("{ \"data\": { \"foo\": \"bar\"}}".getBytes()));
when(connection.getInputStream()).thenReturn(new ByteArrayInputStream(
("{ \"meta\": { \"count\": 1}, "
+ "\"data\": { \"foo\": \"bar\"}, "
+ "\"dictionaries\": { "
+ "\"locations\": { \"city\" : { \"code\": \"xxx\"}}}}").getBytes()));

response.parse(client);

assertEquals(response.getStatusCode(), 200);
assertEquals(response.getBody(), "{ \"data\": { \"foo\": \"bar\"}}");
assertEquals(response.getBody(),
"{ \"meta\": { \"count\": 1}, "
+ "\"data\": { \"foo\": \"bar\"}, "
+ "\"dictionaries\": { "
+ "\"locations\": { \"city\" : { \"code\": \"xxx\"}}}}");
assertTrue(response.isParsed());
assertNotNull(response.getResult());
assertNotNull(response.getData());
assertNotNull(response.getMeta());
assertNotNull(response.getDictionaries());
}


@Test public void testNoData() throws IOException {
when(connection.getResponseCode()).thenReturn(200);
when(connection.getHeaderField("Content-Type")).thenReturn(
Expand All @@ -89,6 +99,8 @@ public class ResponseTest {
assertTrue(response.isParsed());
assertNotNull(response.getResult());
assertNull(response.getData());
assertNull(response.getMeta());
assertNull(response.getDictionaries());
}

@Test public void testEmptyBody() throws IOException {
Expand All @@ -105,6 +117,8 @@ public class ResponseTest {
assertFalse(response.isParsed());
assertNull(response.getResult());
assertNull(response.getData());
assertNull(response.getMeta());
assertNull(response.getDictionaries());
}

@Test public void testNoContent() throws IOException {
Expand All @@ -121,6 +135,8 @@ public class ResponseTest {
assertFalse(response.isParsed());
assertNull(response.getResult());
assertNull(response.getData());
assertNull(response.getMeta());
assertNull(response.getDictionaries());
}

@Test public void testEmptyConnection() throws IOException {
Expand All @@ -136,6 +152,8 @@ public class ResponseTest {
assertFalse(response.isParsed());
assertNull(response.getResult());
assertNull(response.getData());
assertNull(response.getMeta());
assertNull(response.getDictionaries());
}

@Test public void testEmptyConnectionWithStatusCode() throws IOException {
Expand All @@ -154,23 +172,27 @@ public class ResponseTest {
assertFalse(response.isParsed());
assertNull(response.getResult());
assertNull(response.getData());
assertNull(response.getMeta());
assertNull(response.getDictionaries());
}

@Test public void testErrorResponse() throws IOException {
when(connection.getResponseCode()).thenReturn(400);
when(connection.getHeaderField("Content-Type")).thenReturn(
"application/json");
when(connection.getInputStream()).thenThrow(new IOException());
when(connection.getErrorStream()).thenReturn(
new ByteArrayInputStream("{ \"data\": []}".getBytes()));
when(connection.getErrorStream()).thenReturn(new ByteArrayInputStream(
"{ \"meta\": {}, \"data\": [], \"dictionaries\": {}}".getBytes()));

response.parse(client);

assertEquals(response.getStatusCode(), 400);
assertEquals(response.getBody(), "{ \"data\": []}");
assertEquals(response.getBody(), "{ \"meta\": {}, \"data\": [], \"dictionaries\": {}}");
assertTrue(response.isParsed());
assertNotNull(response.getResult());
assertNotNull(response.getData());
assertNotNull(response.getMeta());
assertNotNull(response.getDictionaries());
}

@Test (expected = ServerException.class)
Expand Down