33import com .amadeus .client .AccessToken ;
44import com .amadeus .exceptions .NetworkException ;
55import com .amadeus .exceptions .ResponseException ;
6+ import com .amadeus .resources .Resource ;
67import java .io .BufferedWriter ;
78import java .io .IOException ;
89import 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}
0 commit comments