Skip to content

Commit eb2d652

Browse files
committed
Add response to each resource
1 parent b609ee3 commit eb2d652

File tree

15 files changed

+59
-13
lines changed

15 files changed

+59
-13
lines changed

src/main/java/com/amadeus/Response.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.amadeus.exceptions.ParserException;
77
import com.amadeus.exceptions.ResponseException;
88
import com.amadeus.exceptions.ServerException;
9+
import com.google.gson.Gson;
910
import com.google.gson.JsonElement;
1011
import com.google.gson.JsonObject;
1112
import com.google.gson.JsonParser;

src/main/java/com/amadeus/referenceData/Location.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.amadeus.Response;
66
import com.amadeus.exceptions.ResponseException;
77
import com.amadeus.referenceData.locations.Airports;
8+
import com.amadeus.resources.Resource;
89
import com.google.gson.Gson;
910

1011
/**
@@ -51,7 +52,8 @@ public Location(Amadeus client, String locationId) {
5152
public com.amadeus.resources.Location get(Params params) throws ResponseException {
5253
String path = String.format("/v1/reference-data/locations/%s", locationId);
5354
Response response = client.get(path, params);
54-
return new Gson().fromJson(response.getData(), com.amadeus.resources.Location.class);
55+
return (com.amadeus.resources.Location)
56+
Resource.fromObject(response, com.amadeus.resources.Location.class);
5557
}
5658

5759
/**

src/main/java/com/amadeus/referenceData/Locations.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.amadeus.exceptions.ResponseException;
77
import com.amadeus.referenceData.locations.Airports;
88
import com.amadeus.resources.Location;
9+
import com.amadeus.resources.Resource;
910
import com.google.gson.Gson;
1011

1112
/**
@@ -64,7 +65,7 @@ public Locations(Amadeus client) {
6465
*/
6566
public Location[] get(Params params) throws ResponseException {
6667
Response response = client.get("/v1/reference-data/locations", params);
67-
return new Gson().fromJson(response.getData(), Location[].class);
68+
return (Location[]) Resource.fromArray(response, Location[].class);
6869
}
6970

7071
/**

src/main/java/com/amadeus/referenceData/locations/Airports.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.amadeus.Response;
66
import com.amadeus.exceptions.ResponseException;
77
import com.amadeus.resources.Location;
8+
import com.amadeus.resources.Resource;
89
import com.google.gson.Gson;
910

1011
/**
@@ -48,7 +49,7 @@ public Airports(Amadeus client) {
4849
*/
4950
public Location[] get(Params params) throws ResponseException {
5051
Response response = client.get("/v1/reference-data/locations/airports", params);
51-
return new Gson().fromJson(response.getData(), Location[].class);
52+
return (Location[]) Resource.fromArray(response, Location[].class);
5253
}
5354

5455
/**

src/main/java/com/amadeus/referenceData/urls/CheckinLinks.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.amadeus.Response;
66
import com.amadeus.exceptions.ResponseException;
77
import com.amadeus.resources.CheckinLink;
8+
import com.amadeus.resources.Resource;
89
import com.google.gson.Gson;
910

1011
/**
@@ -47,7 +48,7 @@ public CheckinLinks(Amadeus client) {
4748
*/
4849
public CheckinLink[] get(Params params) throws ResponseException {
4950
Response response = client.get("/v2/reference-data/urls/checkin-links", params);
50-
return new Gson().fromJson(response.getData(), CheckinLink[].class);
51+
return (CheckinLink[]) Resource.fromArray(response, CheckinLink[].class);
5152
}
5253

5354
/**
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,36 @@
11
package com.amadeus.resources;
22

3+
import com.amadeus.Response;
4+
import com.google.gson.Gson;
5+
import lombok.Getter;
6+
37
public class Resource {
8+
/**
9+
* The original response that this object is populated from.
10+
*/
11+
private @Getter Response response;
12+
13+
/**
14+
* Turns a response into a Gson deserialized array of resources,
15+
* attaching the response to each resource.
16+
* @hide as only used internally
17+
*/
18+
public static Resource[] fromArray(Response response, Class klass) {
19+
Resource[] resources = (Resource[]) new Gson().fromJson(response.getData(), klass);
20+
for (Resource resource : resources) {
21+
resource.response = response;
22+
}
23+
return resources;
24+
}
25+
26+
/**
27+
* Turns a response into a Gson deserialized resource,
28+
* attaching the response to the resource.
29+
* @hide as only used internally
30+
*/
31+
public static Resource fromObject(Response response, Class klass) {
32+
Resource resource = (Resource) new Gson().fromJson(response.getData(), klass);
33+
resource.response = response;
34+
return resource;
35+
}
436
}

src/main/java/com/amadeus/shopping/FlightDates.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.amadeus.Response;
66
import com.amadeus.exceptions.ResponseException;
77
import com.amadeus.resources.FlightDate;
8+
import com.amadeus.resources.Resource;
89
import com.google.gson.Gson;
910

1011
/**
@@ -49,7 +50,7 @@ public FlightDates(Amadeus client) {
4950
*/
5051
public FlightDate[] get(Params params) throws ResponseException {
5152
Response response = client.get("/v1/shopping/flight-dates", params);
52-
return new Gson().fromJson(response.getData(), FlightDate[].class);
53+
return (FlightDate[]) Resource.fromArray(response, FlightDate[].class);
5354
}
5455

5556
/**

src/main/java/com/amadeus/shopping/FlightDestinations.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.amadeus.Response;
66
import com.amadeus.exceptions.ResponseException;
77
import com.amadeus.resources.FlightDestination;
8+
import com.amadeus.resources.Resource;
89
import com.google.gson.Gson;
910

1011
/**
@@ -48,7 +49,7 @@ public FlightDestinations(Amadeus client) {
4849
*/
4950
public FlightDestination[] get(Params params) throws ResponseException {
5051
Response response = client.get("/v1/shopping/flight-destinations", params);
51-
return new Gson().fromJson(response.getData(), FlightDestination[].class);
52+
return (FlightDestination[]) Resource.fromArray(response, FlightDestination[].class);
5253
}
5354

5455
/**

src/main/java/com/amadeus/shopping/FlightOffers.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.amadeus.Response;
66
import com.amadeus.exceptions.ResponseException;
77
import com.amadeus.resources.FlightOffer;
8+
import com.amadeus.resources.Resource;
89
import com.google.gson.Gson;
910

1011
/**
@@ -49,7 +50,7 @@ public FlightOffers(Amadeus client) {
4950
*/
5051
public FlightOffer[] get(Params params) throws ResponseException {
5152
Response response = client.get("/v1/shopping/flight-offers", params);
52-
return new Gson().fromJson(response.getData(), FlightOffer[].class);
53+
return (FlightOffer[]) Resource.fromArray(response, FlightOffer[].class);
5354
}
5455

5556
/**

src/main/java/com/amadeus/shopping/HotelOffers.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.amadeus.Response;
66
import com.amadeus.exceptions.ResponseException;
77
import com.amadeus.resources.HotelOffer;
8+
import com.amadeus.resources.Resource;
89
import com.google.gson.Gson;
910

1011
/**
@@ -47,7 +48,7 @@ public HotelOffers(Amadeus client) {
4748
*/
4849
public HotelOffer[] get(Params params) throws ResponseException {
4950
Response response = client.get("/v1/shopping/hotel-offers", params);
50-
return new Gson().fromJson(response.getData(), HotelOffer[].class);
51+
return (HotelOffer[]) Resource.fromArray(response, HotelOffer[].class);
5152
}
5253

5354
/**

0 commit comments

Comments
 (0)