55import com .amadeus .exceptions .NetworkException ;
66import com .amadeus .exceptions .ResponseException ;
77import com .amadeus .resources .Resource ;
8+ import com .google .gson .JsonObject ;
89import java .io .BufferedWriter ;
910import java .io .IOException ;
1011import java .io .OutputStream ;
@@ -36,7 +37,7 @@ protected HTTPClient(Configuration configuration) {
3637 * @see Amadeus#get(String, Params)
3738 */
3839 public Response get (String path ) throws ResponseException {
39- return request (Constants .GET , path , null );
40+ return request (Constants .GET , path , null , null );
4041 }
4142
4243 /**
@@ -63,7 +64,7 @@ public Response get(String path) throws ResponseException {
6364 * @return a Response object containing the status code, body, and parsed data.
6465 */
6566 public Response get (String path , Params params ) throws ResponseException {
66- return request (Constants .GET , path , params );
67+ return request (Constants .GET , path , params , null );
6768 }
6869
6970 /**
@@ -73,7 +74,7 @@ public Response get(String path, Params params) throws ResponseException {
7374 * @see Amadeus#post(String, Params)
7475 */
7576 public Response post (String path ) throws ResponseException {
76- return request (Constants .POST , path , null );
77+ return request (Constants .POST , path , null , null );
7778 }
7879
7980 /**
@@ -100,7 +101,57 @@ public Response post(String path) throws ResponseException {
100101 * @return a Response object containing the status code, body, and parsed data.
101102 */
102103 public Response post (String path , Params params ) throws ResponseException {
103- return request (Constants .POST , path , params );
104+ return request (Constants .POST , path , params , null );
105+ }
106+
107+ /**
108+ * <p>
109+ * A helper module for making generic POST requests calls. It is used by
110+ * every namespaced API POST method.
111+ * </p>
112+ *
113+ * <pre>
114+ * amadeus.foo.bar.post(Params.with("airline", "1X"));
115+ * </pre>
116+ *
117+ * <p>
118+ * It can be used to make any generic API call that is automatically
119+ * authenticated using your API credentials:
120+ * </p>
121+ *
122+ * <pre>
123+ * amadeus.post("/v1/foo/bar", Params.with("airline", "1X"));
124+ * </pre>
125+ *
126+ * @param path The full path for the API call
127+ * @param body The optional POST params to pass to the API
128+ * @return a Response object containing the status code, body, and parsed data.
129+ */
130+ public Response post (String path , String body ) throws ResponseException {
131+ return request (Constants .POST , path , null , body );
132+ }
133+
134+ /**
135+ * <p>
136+ * A helper module for making generic POST requests calls. It is used by
137+ * every namespaced API POST method.
138+ * </p>
139+ *
140+ * <p>
141+ * It can be used to make any generic API call that is automatically
142+ * authenticated using your API credentials:
143+ * </p>
144+ *
145+ * <pre>
146+ * amadeus.post("/v1/foo/bar", { "foo" : "bar" })
147+ * </pre>
148+ *
149+ * @param path The full path for the API call
150+ * @param body The POST JsonObject body to pass to the API
151+ * @return a Response object containing the status code, body, and parsed data.
152+ */
153+ public Response post (String path , JsonObject body ) throws ResponseException {
154+ return request (Constants .POST , path , null , body .toString ());
104155 }
105156
106157 /**
@@ -110,9 +161,9 @@ public Response post(String path, Params params) throws ResponseException {
110161 *
111162 * @hides as only used internally
112163 */
113- public Response unauthenticatedRequest (String verb , String path , Params params ,
114- String bearerToken ) throws ResponseException {
115- Request request = buildRequest (verb , path , params , bearerToken );
164+ public Response unauthenticatedRequest (String verb , String path , Params params , String body ,
165+ String bearerToken ) throws ResponseException {
166+ Request request = buildRequest (verb , path , params , body , bearerToken );
116167 log (request );
117168 return execute (request );
118169 }
@@ -198,13 +249,15 @@ public Resource[] last(Resource resource) throws ResponseException {
198249 }
199250
200251 // A generic method for making requests of any verb.
201- protected Response request (String verb , String path , Params params ) throws ResponseException {
202- return unauthenticatedRequest (verb , path , params , accessToken .getBearerToken ());
252+ protected Response request (String verb , String path , Params params , String body )
253+ throws ResponseException {
254+ return unauthenticatedRequest (verb , path , params , body , accessToken .getBearerToken ());
203255 }
204256
205257 // Builds a request
206- protected Request buildRequest (String verb , String path , Params params , String bearerToken ) {
207- return new Request (verb , path , params , bearerToken , this );
258+ protected Request buildRequest (String verb , String path , Params params , String body ,
259+ String bearerToken ) {
260+ return new Request (verb , path , params , body , bearerToken , this );
208261 }
209262
210263 // A simple log that only triggers if we are in debug mode
@@ -238,6 +291,8 @@ private Request fetch(Request request) throws NetworkException {
238291
239292 // Writes the parameters to the request.
240293 private void write (Request request ) throws IOException {
294+
295+
241296 if (request .getVerb () == Constants .POST && request .getParams () != null ) {
242297 OutputStream os = request .getConnection ().getOutputStream ();
243298 BufferedWriter writer = new BufferedWriter (new OutputStreamWriter (os , "UTF-8" ));
@@ -246,6 +301,16 @@ private void write(Request request) throws IOException {
246301 writer .close ();
247302 os .close ();
248303 }
304+ if (request .getVerb () == Constants .POST && request .getParams () == null ) {
305+ OutputStream os = request .getConnection ().getOutputStream ();
306+ BufferedWriter writer = new BufferedWriter (new OutputStreamWriter (os , "UTF-8" ));
307+ if (request .getBody () != null ) {
308+ writer .write (request .getBody ());
309+ }
310+ writer .flush ();
311+ writer .close ();
312+ os .close ();
313+ }
249314 }
250315
251316 /**
@@ -263,7 +328,7 @@ protected Response page(String pageName, Response response) throws ResponseExcep
263328 Params params = (Params ) request .getParams ().clone ();
264329 params .put ("page[offset]" , pageNumber );
265330
266- return request (request .getVerb (), request .getPath (), params );
331+ return request (request .getVerb (), request .getPath (), params , "emptyBody" );
267332 } catch (NullPointerException e ) {
268333 return null ;
269334 }
@@ -277,4 +342,4 @@ protected Resource[] page(String pageName, Resource resource) throws ResponseExc
277342 Response response = page (pageName , resource .getResponse ());
278343 return Resource .fromArray (response , resource .getDeSerializationClass ());
279344 }
280- }
345+ }
0 commit comments