Skip to content

Commit af19f12

Browse files
committed
Add pagination
1 parent eb2d652 commit af19f12

File tree

14 files changed

+574
-44
lines changed

14 files changed

+574
-44
lines changed

src/main/java/com/amadeus/HTTPClient.java

Lines changed: 116 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.amadeus.client.AccessToken;
44
import com.amadeus.exceptions.NetworkException;
55
import com.amadeus.exceptions.ResponseException;
6+
import com.amadeus.resources.Resource;
67
import java.io.BufferedWriter;
78
import java.io.IOException;
89
import java.io.OutputStream;
@@ -101,15 +102,6 @@ public Response post(String path, Params params) throws ResponseException {
101102
return request("POST", path, params);
102103
}
103104

104-
// A generic method for making requests of any verb.
105-
protected Response request(String verb, String path, Params params) throws ResponseException {
106-
return unauthenticatedRequest(verb, path, params, accessToken.getBearerToken());
107-
}
108-
109-
// A generic method for making any authenticated or unauthenticated request,
110-
// passing in the bearer token explicitly. Used primarily by the
111-
// AccessToken to get the first AccessToken.
112-
113105
/**
114106
* A generic method for making any authenticated or unauthenticated request,
115107
* passing in the bearer token explicitly. Used primarily by the
@@ -124,6 +116,91 @@ public Response unauthenticatedRequest(String verb, String path, Params params,
124116
return execute(request);
125117
}
126118

119+
/**
120+
* Fetches the previous page for a given response.
121+
* @param response a response object previously received for which includes an array of data
122+
* @return a new response of data
123+
* @throws ResponseException if the page could not be found
124+
*/
125+
public Response previous(Response response) throws ResponseException {
126+
return page("previous", response);
127+
}
128+
129+
/**
130+
* Fetches the previous page for a given response.
131+
* @param resource one of the responses previously received from an API call
132+
* @return a new array of resources of the same type
133+
* @throws ResponseException if the page could not be found
134+
*/
135+
public Resource[] previous(Resource resource) throws ResponseException {
136+
return page("previous", resource);
137+
}
138+
139+
/**
140+
* Fetches the next page for a given response.
141+
* @param response a response object previously received for which includes an array of data
142+
* @return a new response of data
143+
* @throws ResponseException if the page could not be found
144+
*/
145+
public Response next(Response response) throws ResponseException {
146+
return page("next", response);
147+
}
148+
149+
/**
150+
* Fetches the next page for a given response.
151+
* @param resource one of the responses previously received from an API call
152+
* @return a new array of resources of the same type
153+
* @throws ResponseException if the page could not be found
154+
*/
155+
public Resource[] next(Resource resource) throws ResponseException {
156+
return page("next", resource);
157+
}
158+
159+
/**
160+
* Fetches the first page for a given response.
161+
* @param response a response object previously received for which includes an array of data
162+
* @return a new response of data
163+
* @throws ResponseException if the page could not be found
164+
*/
165+
public Response first(Response response) throws ResponseException {
166+
return page("first", response);
167+
}
168+
169+
/**
170+
* Fetches the first page for a given response.
171+
* @param resource one of the responses previously received from an API call
172+
* @return a new array of resources of the same type
173+
* @throws ResponseException if the page could not be found
174+
*/
175+
public Resource[] first(Resource resource) throws ResponseException {
176+
return page("first", resource);
177+
}
178+
179+
/**
180+
* Fetches the last page for a given response.
181+
* @param response a response object previously received for which includes an array of data
182+
* @return a new response of data
183+
* @throws ResponseException if the page could not be found
184+
*/
185+
public Response last(Response response) throws ResponseException {
186+
return page("last", response);
187+
}
188+
189+
/**
190+
* Fetches the last page for a given response.
191+
* @param resource one of the responses previously received from an API call
192+
* @return a new array of resources of the same type
193+
* @throws ResponseException if the page could not be found
194+
*/
195+
public Resource[] last(Resource resource) throws ResponseException {
196+
return page("last", resource);
197+
}
198+
199+
// A generic method for making requests of any verb.
200+
protected Response request(String verb, String path, Params params) throws ResponseException {
201+
return unauthenticatedRequest(verb, path, params, accessToken.getBearerToken());
202+
}
203+
127204
// Builds a request
128205
protected Request buildRequest(String verb, String path, Params params, String bearerToken) {
129206
return new Request(verb, path, params, bearerToken, this);
@@ -169,4 +246,34 @@ private void write(Request request) throws IOException {
169246
os.close();
170247
}
171248
}
249+
250+
/**
251+
* Fetches the response for another page.
252+
* @hide as ony used internally
253+
*/
254+
protected Response page(String pageName, Response response) throws ResponseException {
255+
try {
256+
String[] parts = response.getResult().get("meta").getAsJsonObject()
257+
.get("links").getAsJsonObject().get(pageName).getAsString().split("=");
258+
259+
String pageNumber = parts[parts.length - 1];
260+
261+
Request request = response.getRequest();
262+
Params params = (Params) request.getParams().clone();
263+
params.put("page[offset]", pageNumber);
264+
265+
return request(request.getVerb(), request.getPath(), params);
266+
} catch (NullPointerException e) {
267+
return null;
268+
}
269+
}
270+
271+
/**
272+
* Fetches the response for another page.
273+
* @hide as ony used internally
274+
*/
275+
protected Resource[] page(String pageName, Resource resource) throws ResponseException {
276+
Response response = page(pageName, resource.getResponse());
277+
return Resource.fromArray(response, resource.getDeSerializationClass());
278+
}
172279
}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import com.amadeus.exceptions.ParserException;
77
import com.amadeus.exceptions.ResponseException;
88
import com.amadeus.exceptions.ServerException;
9-
import com.google.gson.Gson;
109
import com.google.gson.JsonElement;
1110
import com.google.gson.JsonObject;
1211
import com.google.gson.JsonParser;

src/main/java/com/amadeus/exceptions/ResponseException.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ private static StringBuffer determineLongDescription(Response response) {
6262
if (response.getResult().has("error_description")) {
6363
description.append(getErrorDescription(response));
6464
}
65-
if (response.getResult().has("exceptions")) {
65+
if (response.getResult().has("errors")) {
6666
description.append(getErrorsDescription(response));
6767
}
6868
}
@@ -81,7 +81,7 @@ private static StringBuffer getErrorDescription(Response response) {
8181

8282
private static StringBuffer getErrorsDescription(Response response) {
8383
StringBuffer message = new StringBuffer();
84-
for (JsonElement error : response.getResult().get("exceptions").getAsJsonArray()) {
84+
for (JsonElement error : response.getResult().get("errors").getAsJsonArray()) {
8585
JsonObject json = error.getAsJsonObject();
8686
message.append("\n");
8787
if (json.has("source")) {

src/main/java/com/amadeus/resources/AirTraffic.java

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,52 @@
11
package com.amadeus.resources;
22

3+
import com.amadeus.travel.analytics.airTraffic.Traveled;
34
import lombok.Getter;
45
import lombok.ToString;
56

7+
/**
8+
* An AirTraffic object as returned by the AirTraffic API.
9+
* @see Traveled#get()
10+
*/
611
@ToString
712
public class AirTraffic extends Resource {
13+
protected AirTraffic() {}
14+
815
private @Getter String type;
916
private @Getter String subType;
1017
private @Getter String destination;
1118
private @Getter Analytics analytics;
1219

20+
/**
21+
* An AirTraffic-related object as returned by the AirTraffic API.
22+
* @see Traveled#get()
23+
*/
1324
@ToString
14-
private class Analytics {
25+
public class Analytics {
26+
protected Analytics() {}
27+
1528
private @Getter Flights flights;
1629
private @Getter Travellers travellers;
1730

31+
/**
32+
* An AirTraffic-related object as returned by the AirTraffic API.
33+
* @see Traveled#get()
34+
*/
1835
@ToString
19-
private class Flights {
36+
public class Flights {
37+
protected Flights() {}
38+
2039
private @Getter Double score;
2140
}
2241

42+
/**
43+
* An AirTraffic-related object as returned by the AirTraffic API.
44+
* @see Traveled#get()
45+
*/
2346
@ToString
24-
private class Travellers {
47+
public class Travellers {
48+
protected Travellers() {}
49+
2550
private @Getter Double score;
2651
}
2752
}

src/main/java/com/amadeus/resources/CheckinLink.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,14 @@
33
import lombok.Getter;
44
import lombok.ToString;
55

6+
/**
7+
* An CheckinLink object as returned by the CheckinLink API.
8+
* @see com.amadeus.referenceData.urls.CheckinLinks#get()
9+
*/
610
@ToString
711
public class CheckinLink extends Resource {
12+
protected CheckinLink() {}
13+
814
private @Getter String type;
915
private @Getter String id;
1016
private @Getter String href;

src/main/java/com/amadeus/resources/FareSearch.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,28 @@
44
import lombok.Getter;
55
import lombok.ToString;
66

7+
/**
8+
* An FareSearch object as returned by the FareSearch API.
9+
* @see com.amadeus.travel.analytics.FareSearches#get()
10+
*/
711
@ToString
812
public class FareSearch extends Resource {
13+
protected FareSearch() {}
14+
915
private @Getter String type;
1016
private @Getter String period;
1117
private @Getter String origin;
1218
private @Getter String sourceCountry;
1319
private @Getter NumberOfSearches numberOfSearches;
1420

21+
/**
22+
* An FareSearch-related object as returned by the FareSearch API.
23+
* @see com.amadeus.travel.analytics.FareSearches#get()
24+
*/
1525
@ToString
16-
private class NumberOfSearches {
26+
public class NumberOfSearches {
27+
protected NumberOfSearches() {}
28+
1729
private @Getter HashMap<String, String> perDestination;
1830
private @Getter HashMap<String, Integer> perTripDuration;
1931
private @Getter HashMap<String, Integer> perDaysInAdvance;

src/main/java/com/amadeus/resources/FlightDate.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,29 @@
44
import lombok.Getter;
55
import lombok.ToString;
66

7+
/**
8+
* An FlightDate object as returned by the FlightDates API.
9+
* @see com.amadeus.shopping.FlightDates#get()
10+
*/
711
@ToString
812
public class FlightDate extends Resource {
13+
protected FlightDate() {}
14+
915
private @Getter String type;
1016
private @Getter String origin;
1117
private @Getter String destination;
1218
private @Getter Date departureDate;
1319
private @Getter Date returnDate;
1420
private @Getter Price price;
1521

22+
/**
23+
* An FlightDate-related object as returned by the FlightDates API.
24+
* @see com.amadeus.shopping.FlightDates#get()
25+
*/
1626
@ToString
17-
private class Price {
27+
public class Price {
28+
protected Price() {}
29+
1830
private @Getter double total;
1931
}
2032
}

src/main/java/com/amadeus/resources/FlightDestination.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,29 @@
44
import lombok.Getter;
55
import lombok.ToString;
66

7+
/**
8+
* An FlightDestination object as returned by the FlightDestinations API.
9+
* @see com.amadeus.shopping.FlightDestinations#get()
10+
*/
711
@ToString
812
public class FlightDestination extends Resource {
13+
protected FlightDestination() {}
14+
915
private @Getter String type;
1016
private @Getter String origin;
1117
private @Getter String destination;
1218
private @Getter Date departureDate;
1319
private @Getter Date returnDate;
1420
private @Getter Price price;
1521

22+
/**
23+
* An FlightDestination-related object as returned by the FlightDestinations API.
24+
* @see com.amadeus.shopping.FlightDestinations#get()
25+
*/
1626
@ToString
17-
private class Price {
27+
public class Price {
28+
protected Price() {}
29+
1830
private @Getter double total;
1931
}
2032
}

0 commit comments

Comments
 (0)