From 96f1dd0a07caacd2c8a347635a6e2f454a941f9a Mon Sep 17 00:00:00 2001 From: xianqiliu Date: Fri, 18 Feb 2022 11:14:36 +0100 Subject: [PATCH 1/5] add objet mapping for meta and dictionaries --- README.md | 25 ++++++++++++++++ src/main/java/com/amadeus/Response.java | 25 ++++++++++++++++ .../com/amadeus/resources/CollectionMeta.java | 29 ++++++++++++++++++ .../com/amadeus/resources/Dictionary.java | 30 +++++++++++++++++++ .../java/com/amadeus/resources/Resource.java | 12 ++++++++ src/test/java/com/amadeus/ResponseTest.java | 30 +++++++++++++++---- 6 files changed, 145 insertions(+), 6 deletions(-) create mode 100644 src/main/java/com/amadeus/resources/CollectionMeta.java create mode 100644 src/main/java/com/amadeus/resources/Dictionary.java diff --git a/README.md b/README.md index 2523f678..c3d2b644 100644 --- a/README.md +++ b/README.md @@ -398,6 +398,31 @@ TripDetail tripDetail = amadeus.travel.tripParser.post(body); Destination[] directDestinations = amadeus.airport.directDestinations.get(Params .with("departureAirportCode","MAD") .and("max","2")); + +// 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 locations = flightOffersDic.getLocations(); +// get cityCode in the location (same for CountryCode) +String cityCode = flightOffersDic.getLocations().get("MAD").getCityCode(); +// get aircraft in dictionaries +Map aircraft = flightOffersDic.getAircraft(); +// get currencies in dictionaries +Map currencies = flightOffersDic.getCurrencies(); +// get carriers in dictionaries +Map 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 diff --git a/src/main/java/com/amadeus/Response.java b/src/main/java/com/amadeus/Response.java index ce817a97..181b3f8d 100644 --- a/src/main/java/com/amadeus/Response.java +++ b/src/main/java/com/amadeus/Response.java @@ -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; @@ -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; @@ -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. diff --git a/src/main/java/com/amadeus/resources/CollectionMeta.java b/src/main/java/com/amadeus/resources/CollectionMeta.java new file mode 100644 index 00000000..1b61dc8d --- /dev/null +++ b/src/main/java/com/amadeus/resources/CollectionMeta.java @@ -0,0 +1,29 @@ +package com.amadeus.resources; + +import com.amadeus.Response; +import lombok.Getter; +import lombok.ToString; + +/** + * A CollectionMeta Object which is mapped from the + * @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; + } +} diff --git a/src/main/java/com/amadeus/resources/Dictionary.java b/src/main/java/com/amadeus/resources/Dictionary.java new file mode 100644 index 00000000..61c52fce --- /dev/null +++ b/src/main/java/com/amadeus/resources/Dictionary.java @@ -0,0 +1,30 @@ +package com.amadeus.resources; + +import com.amadeus.Response; +import lombok.Getter; +import lombok.ToString; + +import java.util.HashMap; +import java.util.Map; + +/** + * A Dictionary Object which is mapped from the + * @see Response#getDictionaries() + */ +@ToString +public class Dictionary extends Resource { + protected Dictionary() {} + + private @Getter Map locations; + private @Getter Map aircraft; + private @Getter Map currencies; + private @Getter Map carriers; + + @ToString + public class LocationValue { + protected LocationValue() {} + + private @Getter String cityCode; + private @Getter String countryCode; + } +} diff --git a/src/main/java/com/amadeus/resources/Resource.java b/src/main/java/com/amadeus/resources/Resource.java index c0657194..9c3dea45 100644 --- a/src/main/java/com/amadeus/resources/Resource.java +++ b/src/main/java/com/amadeus/resources/Resource.java @@ -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, @@ -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; } @@ -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; } } diff --git a/src/test/java/com/amadeus/ResponseTest.java b/src/test/java/com/amadeus/ResponseTest.java index c265bb31..3e25c92e 100644 --- a/src/test/java/com/amadeus/ResponseTest.java +++ b/src/test/java/com/amadeus/ResponseTest.java @@ -47,15 +47,17 @@ public class ResponseTest { when(connection.getHeaderField("Content-Type")).thenReturn( "application/json"); when(connection.getInputStream()).thenReturn( - new ByteArrayInputStream("{ \"data\": []}".getBytes())); + 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 { @@ -63,15 +65,19 @@ public class ResponseTest { when(connection.getHeaderField("Content-Type")).thenReturn( "application/json"); when(connection.getInputStream()).thenReturn( - new ByteArrayInputStream("{ \"data\": { \"foo\": \"bar\"}}".getBytes())); + 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()); } @@ -89,6 +95,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 { @@ -105,6 +113,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 { @@ -121,6 +131,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 { @@ -136,6 +148,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 { @@ -154,6 +168,8 @@ public class ResponseTest { assertFalse(response.isParsed()); assertNull(response.getResult()); assertNull(response.getData()); + assertNull(response.getMeta()); + assertNull(response.getDictionaries()); } @Test public void testErrorResponse() throws IOException { @@ -162,15 +178,17 @@ public class ResponseTest { "application/json"); when(connection.getInputStream()).thenThrow(new IOException()); when(connection.getErrorStream()).thenReturn( - new ByteArrayInputStream("{ \"data\": []}".getBytes())); + 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) From 7087be6e802fcc36c7138371f6058f3595c43b63 Mon Sep 17 00:00:00 2001 From: xianqiliu Date: Fri, 18 Feb 2022 12:12:02 +0100 Subject: [PATCH 2/5] match check style rule --- src/main/java/com/amadeus/resources/CollectionMeta.java | 2 +- src/main/java/com/amadeus/resources/Dictionary.java | 6 ++---- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/amadeus/resources/CollectionMeta.java b/src/main/java/com/amadeus/resources/CollectionMeta.java index 1b61dc8d..20d9d054 100644 --- a/src/main/java/com/amadeus/resources/CollectionMeta.java +++ b/src/main/java/com/amadeus/resources/CollectionMeta.java @@ -5,7 +5,7 @@ import lombok.ToString; /** - * A CollectionMeta Object which is mapped from the + * A CollectionMeta Object Mapping. * @see Response#getMeta() */ @ToString diff --git a/src/main/java/com/amadeus/resources/Dictionary.java b/src/main/java/com/amadeus/resources/Dictionary.java index 61c52fce..4e8f86fb 100644 --- a/src/main/java/com/amadeus/resources/Dictionary.java +++ b/src/main/java/com/amadeus/resources/Dictionary.java @@ -1,14 +1,12 @@ package com.amadeus.resources; import com.amadeus.Response; +import java.util.Map; import lombok.Getter; import lombok.ToString; -import java.util.HashMap; -import java.util.Map; - /** - * A Dictionary Object which is mapped from the + * A Dictionary Object which is mapping. * @see Response#getDictionaries() */ @ToString From ffe5779b24f0f7d76663a187b45be5f1d0f43550 Mon Sep 17 00:00:00 2001 From: xianqiliu Date: Fri, 18 Feb 2022 12:25:17 +0100 Subject: [PATCH 3/5] match check style rule --- src/test/java/com/amadeus/ResponseTest.java | 22 ++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/src/test/java/com/amadeus/ResponseTest.java b/src/test/java/com/amadeus/ResponseTest.java index 3e25c92e..36b2a466 100644 --- a/src/test/java/com/amadeus/ResponseTest.java +++ b/src/test/java/com/amadeus/ResponseTest.java @@ -46,8 +46,8 @@ public class ResponseTest { when(connection.getResponseCode()).thenReturn(200); when(connection.getHeaderField("Content-Type")).thenReturn( "application/json"); - when(connection.getInputStream()).thenReturn( - new ByteArrayInputStream("{ \"meta\":{},\"data\": [], \"dictionaries\":{}}".getBytes())); + when(connection.getInputStream()).thenReturn(new ByteArrayInputStream( + "{ \"meta\":{},\"data\": [], \"dictionaries\":{}}".getBytes())); response.parse(client); @@ -64,15 +64,20 @@ public class ResponseTest { when(connection.getResponseCode()).thenReturn(200); when(connection.getHeaderField("Content-Type")).thenReturn( "application/json"); - when(connection.getInputStream()).thenReturn( - new ByteArrayInputStream( - ("{ \"meta\": { \"count\": 1}, \"data\": { \"foo\": \"bar\"}, \"dictionaries\": { \"locations\": { \"city\" : { \"code\": \"xxx\"}}}}").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(), - "{ \"meta\": { \"count\": 1}, \"data\": { \"foo\": \"bar\"}, \"dictionaries\": { \"locations\": { \"city\" : { \"code\": \"xxx\"}}}}"); + "{ \"meta\": { \"count\": 1}, " + + "\"data\": { \"foo\": \"bar\"}, " + + "\"dictionaries\": { " + + "\"locations\": { \"city\" : { \"code\": \"xxx\"}}}}"); assertTrue(response.isParsed()); assertNotNull(response.getResult()); assertNotNull(response.getData()); @@ -80,7 +85,6 @@ public class ResponseTest { assertNotNull(response.getDictionaries()); } - @Test public void testNoData() throws IOException { when(connection.getResponseCode()).thenReturn(200); when(connection.getHeaderField("Content-Type")).thenReturn( @@ -177,8 +181,8 @@ public class ResponseTest { when(connection.getHeaderField("Content-Type")).thenReturn( "application/json"); when(connection.getInputStream()).thenThrow(new IOException()); - when(connection.getErrorStream()).thenReturn( - new ByteArrayInputStream("{ \"meta\": {}, \"data\": [], \"dictionaries\": {}}".getBytes())); + when(connection.getErrorStream()).thenReturn(new ByteArrayInputStream( + "{ \"meta\": {}, \"data\": [], \"dictionaries\": {}}".getBytes())); response.parse(client); From 5e01a59eecdb78c2aa2744469b9ff28cd5e858d6 Mon Sep 17 00:00:00 2001 From: xianqiliu Date: Fri, 18 Feb 2022 12:34:30 +0100 Subject: [PATCH 4/5] fix the style check again --- src/test/java/com/amadeus/ResponseTest.java | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/test/java/com/amadeus/ResponseTest.java b/src/test/java/com/amadeus/ResponseTest.java index 36b2a466..97f158e5 100644 --- a/src/test/java/com/amadeus/ResponseTest.java +++ b/src/test/java/com/amadeus/ResponseTest.java @@ -65,19 +65,19 @@ public class ResponseTest { when(connection.getHeaderField("Content-Type")).thenReturn( "application/json"); when(connection.getInputStream()).thenReturn(new ByteArrayInputStream( - ("{ \"meta\": { \"count\": 1}, " + - "\"data\": { \"foo\": \"bar\"}, " + - "\"dictionaries\": { " + - "\"locations\": { \"city\" : { \"code\": \"xxx\"}}}}").getBytes())); + ("{ \"meta\": { \"count\": 1}, " + + "\"data\": { \"foo\": \"bar\"}, " + + "\"dictionaries\": { " + + "\"locations\": { \"city\" : { \"code\": \"xxx\"}}}}").getBytes())); response.parse(client); assertEquals(response.getStatusCode(), 200); assertEquals(response.getBody(), - "{ \"meta\": { \"count\": 1}, " + - "\"data\": { \"foo\": \"bar\"}, " + - "\"dictionaries\": { " + - "\"locations\": { \"city\" : { \"code\": \"xxx\"}}}}"); + "{ \"meta\": { \"count\": 1}, " + + "\"data\": { \"foo\": \"bar\"}, " + + "\"dictionaries\": { " + + "\"locations\": { \"city\" : { \"code\": \"xxx\"}}}}"); assertTrue(response.isParsed()); assertNotNull(response.getResult()); assertNotNull(response.getData()); From 18b891ab3581c806544ed9b6e88ce534ecdc06b7 Mon Sep 17 00:00:00 2001 From: xianqiliu Date: Fri, 18 Feb 2022 12:36:00 +0100 Subject: [PATCH 5/5] fix the style check again --- src/test/java/com/amadeus/ResponseTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/com/amadeus/ResponseTest.java b/src/test/java/com/amadeus/ResponseTest.java index 97f158e5..40f04bb8 100644 --- a/src/test/java/com/amadeus/ResponseTest.java +++ b/src/test/java/com/amadeus/ResponseTest.java @@ -74,7 +74,7 @@ public class ResponseTest { assertEquals(response.getStatusCode(), 200); assertEquals(response.getBody(), - "{ \"meta\": { \"count\": 1}, " + "{ \"meta\": { \"count\": 1}, " + "\"data\": { \"foo\": \"bar\"}, " + "\"dictionaries\": { " + "\"locations\": { \"city\" : { \"code\": \"xxx\"}}}}");