1212 */
1313package com .ibm .watson .developer_cloud .http ;
1414
15+ import java .io .InputStream ;
16+ import java .util .ArrayList ;
17+ import java .util .List ;
18+
1519import com .google .gson .JsonObject ;
20+ import com .ibm .watson .developer_cloud .service .WatsonService ;
21+ import com .ibm .watson .developer_cloud .util .GsonSingleton ;
1622import com .ibm .watson .developer_cloud .util .Validator ;
23+
1724import okhttp3 .FormBody ;
1825import okhttp3 .HttpUrl ;
1926import okhttp3 .MediaType ;
2027import okhttp3 .Request ;
2128import okhttp3 .RequestBody ;
2229
23- import java .util .ArrayList ;
24- import java .util .List ;
25-
2630/**
2731 * Convenience class for constructing HTTP/HTTPS requests.
2832 */
2933public class RequestBuilder {
3034
3135 private enum HTTPMethod {
32- DELETE , GET , POST , PUT
36+ DELETE , GET , POST , PUT , PATCH
3337 }
3438
3539 /**
@@ -55,8 +59,8 @@ public static RequestBuilder get(HttpUrl url) {
5559 }
5660
5761 /**
58- * The POST request method is designed to request that a web server accept the data enclosed in the request message's
59- * body for storage. It is often used when uploading a file or submitting a completed web form.
62+ * The POST request method is designed to request that a web server accept the data enclosed in the request
63+ * message's body for storage. It is often used when uploading a file or submitting a completed web form.
6064 *
6165 * @param url the URL
6266 *
@@ -77,6 +81,17 @@ public static RequestBuilder put(HttpUrl url) {
7781 return new RequestBuilder (HTTPMethod .PUT , url );
7882 }
7983
84+ /**
85+ * The PUT method requests that the enclosed entity be stored under the supplied Request-URI.
86+ *
87+ * @param url the URL
88+ *
89+ * @return this
90+ */
91+ public static RequestBuilder patch (HttpUrl url ) {
92+ return new RequestBuilder (HTTPMethod .PATCH , url );
93+ }
94+
8095 /**
8196 * Creates a properly encoded HttpUrl object with no path parameters.
8297 *
@@ -146,8 +161,8 @@ private RequestBuilder(HTTPMethod method, HttpUrl url) {
146161 *
147162 * @param params the parameters
148163 * @param name the parameter name
149- * @param value the value to set, will be obtained via {@link String#valueOf(boolean)}. If null, only the parameter is
150- * set. It can also be a collection or array, in which case all elements are added as query parameters
164+ * @param value the value to set, will be obtained via {@link String#valueOf(boolean)}. If null, only the parameter
165+ * is set. It can also be a collection or array, in which case all elements are added as query parameters
151166 *
152167 * @return this
153168 */
@@ -221,8 +236,8 @@ public Request build() {
221236 */
222237 @ Override
223238 public String toString () {
224- return "RequestBuilder [method=" + method + ", formParams=" + formParams + ", headers=" + headers + ", queryParams="
225- + queryParams + ", httpUrl=" + httpUrl .toString () + "]" ;
239+ return "RequestBuilder [method=" + method + ", formParams=" + formParams + ", headers=" + headers
240+ + ", queryParams=" + queryParams + ", httpUrl=" + httpUrl .toString () + "]" ;
226241 }
227242
228243 /**
@@ -277,13 +292,52 @@ public RequestBuilder body(RequestBody body) {
277292 * @return this
278293 */
279294 public RequestBuilder bodyContent (String content , String contentType ) {
280- body = RequestBody .create (MediaType .parse (contentType ), content );
295+ return body (RequestBody .create (MediaType .parse (contentType ), content ));
296+ }
297+
298+ /**
299+ * Sets the file content (InputStream) to the request (used with POST/PUT).
300+ *
301+ * @param stream the InputStream to read the request body content from
302+ * @param contentType the contentType associated with the data read from the InputStream
303+ * @return this
304+ */
305+ public RequestBuilder bodyContent (InputStream stream , String contentType ) {
306+ return body (InputStreamRequestBody .create (MediaType .parse (contentType ), stream ));
307+ }
308+
309+ /**
310+ * Sets the request body content from one of three different sources, based on the content type.
311+ *
312+ * @param contentType
313+ * the value of the "Content-Type" header associated with the outgoing request
314+ * @param jsonContent
315+ * the body content to be used if the content type indicates JSON
316+ * @param jsonPatchContent
317+ * the body content to be used if the content type indicates JsonPatch
318+ * @param nonJsonContent
319+ * the body content to be used if the content type indicates non-JSON content
320+ * @return this
321+ */
322+ public RequestBuilder bodyContent (String contentType , Object jsonContent , Object jsonPatchContent ,
323+ InputStream nonJsonContent ) {
324+ if (contentType != null ) {
325+ if (WatsonService .isJsonMimeType (contentType )) {
326+ this .bodyContent (
327+ GsonSingleton .getGson ().toJsonTree (jsonContent ).getAsJsonObject ().toString (), contentType );
328+ } else if (WatsonService .isJsonPatchMimeType (contentType )) {
329+ this .bodyContent (
330+ GsonSingleton .getGson ().toJsonTree (jsonPatchContent ).getAsJsonObject ().toString (), contentType );
331+ } else {
332+ this .bodyContent (nonJsonContent , contentType );
333+ }
334+ }
281335 return this ;
282336 }
283337
284338 /**
285- * Adds a JSON content to the request (used with POST/PUT). This will encapsulate the json into a {@link RequestBody}
286- * encoded with UTF-8 and use {@code "application/json"} as Content-Type
339+ * Adds a JSON content to the request (used with POST/PUT). This will encapsulate the json into a
340+ * {@link RequestBody} encoded with UTF-8 and use {@code "application/json"} as Content-Type
287341 *
288342 * @param json the JsonObject json
289343 *
@@ -293,6 +347,19 @@ public RequestBuilder bodyJson(JsonObject json) {
293347 return bodyContent (json .toString (), HttpMediaType .APPLICATION_JSON );
294348 }
295349
350+ /**
351+ * Adds a JSON content to the request (used with POST/PUT/PATCH). This will encapsulate the json into a
352+ * {@link RequestBody} encoded with UTF-8 and use {@code "application/json"} as Content-Type
353+ *
354+ * @param json the JsonObject json
355+ * @param mediaType the contentType value
356+ *
357+ * @return this
358+ */
359+ public RequestBuilder bodyJson (JsonObject json , String mediaType ) {
360+ return bodyContent (json .toString (), mediaType );
361+ }
362+
296363 /**
297364 * Adds form parameters.
298365 *
0 commit comments