diff --git a/src/android/com/github/kevinsawicki/http/HttpRequest.java b/src/android/com/github/kevinsawicki/http/HttpRequest.java index ab63820..f0c0f33 100644 --- a/src/android/com/github/kevinsawicki/http/HttpRequest.java +++ b/src/android/com/github/kevinsawicki/http/HttpRequest.java @@ -236,6 +236,11 @@ public class HttpRequest { */ public static final String METHOD_POST = "POST"; + /** + * 'PATCH' request method + */ + public static final String METHOD_PATCH = "PATCH"; + /** * 'PUT' request method */ @@ -1176,6 +1181,70 @@ public class HttpRequest { return post(encode ? encode(url) : url); } +/** + * Start a 'PATCH' request to the given URL + * + * @param url + * @return request + * @throws HttpRequestException + */ + public static HttpRequest patch(final CharSequence url) + throws HttpRequestException { + return new HttpRequest(url, METHOD_PATCH); + } + + /** + * Start a 'PATCH' request to the given URL + * + * @param url + * @return request + * @throws HttpRequestException + */ + public static HttpRequest patch(final URL url) throws HttpRequestException { + return new HttpRequest(url, METHOD_PATCH); + } + + /** + * Start a 'PATCH' request to the given URL along with the query params + * + * @param baseUrl + * @param params + * the query parameters to include as part of the baseUrl + * @param encode + * true to encode the full URL + * + * @see #append(CharSequence, Map) + * @see #encode(CharSequence) + * + * @return request + */ + public static HttpRequest patch(final CharSequence baseUrl, + final Map params, final boolean encode) { + String url = append(baseUrl, params); + return patch(encode ? encode(url) : url); + } + + /** + * Start a 'PATCH' request to the given URL along with the query params + * + * @param baseUrl + * @param encode + * true to encode the full URL + * @param params + * the name/value query parameter pairs to include as part of the + * baseUrl + * + * @see #append(CharSequence, Object...) + * @see #encode(CharSequence) + * + * @return request + */ + public static HttpRequest patch(final CharSequence baseUrl, + final boolean encode, final Object... params) { + String url = append(baseUrl, params); + return patch(encode ? encode(url) : url); + } + /** * Start a 'PUT' request to the given URL * diff --git a/src/android/com/synconset/cordovahttp/CordovaHttpPatch.java b/src/android/com/synconset/cordovahttp/CordovaHttpPatch.java index a97fff0..bcd60fb 100644 --- a/src/android/com/synconset/cordovahttp/CordovaHttpPatch.java +++ b/src/android/com/synconset/cordovahttp/CordovaHttpPatch.java @@ -16,8 +16,8 @@ import com.github.kevinsawicki.http.HttpRequest; import com.github.kevinsawicki.http.HttpRequest.HttpRequestException; class CordovaHttpPatch extends CordovaHttp implements Runnable { - public CordovaHttpPatch(String urlString, JSONObject data, String serializerName, JSONObject headers, CallbackContext callbackContext, int timeout) { - super(urlString, data, serializerName, headers, timeout, callbackContext); + public CordovaHttpPatch(String urlString, JSONObject params, String serializerName, JSONObject headers, CallbackContext callbackContext, int timeout) { + super(urlString, params, serializerName, headers, timeout, callbackContext); } @Override @@ -26,6 +26,7 @@ class CordovaHttpPatch extends CordovaHttp implements Runnable { HttpRequest request = HttpRequest.patch(this.getUrlString()); request.readTimeout(this.getRequestTimeout()); + this.setupRedirect(request); this.setupSecurity(request); request.acceptCharset(CHARSET); request.headers(this.getHeadersMap()); @@ -43,18 +44,18 @@ class CordovaHttpPatch extends CordovaHttp implements Runnable { JSONObject response = new JSONObject(); this.addResponseHeaders(request, response); - response.patch("status", code); + response.put("status", code); if (code >= 200 && code < 300) { - response.patch("data", body); + response.put("data", body); this.getCallbackContext().success(response); } else { - response.patch("error", body); + response.put("error", body); this.getCallbackContext().error(response); } } catch (JSONException e) { this.respondWithError("There was an error generating the response"); - } catch (HttpRequestException e) { + } catch (HttpRequestException e) { if (e.getCause() instanceof UnknownHostException) { this.respondWithError(0, "The host could not be resolved"); } else if (e.getCause() instanceof SocketTimeoutException) { diff --git a/src/android/com/synconset/cordovahttp/CordovaHttpPlugin.java b/src/android/com/synconset/cordovahttp/CordovaHttpPlugin.java index d7837b7..b69897e 100644 --- a/src/android/com/synconset/cordovahttp/CordovaHttpPlugin.java +++ b/src/android/com/synconset/cordovahttp/CordovaHttpPlugin.java @@ -60,7 +60,16 @@ public class CordovaHttpPlugin extends CordovaPlugin { CordovaHttpPut put = new CordovaHttpPut(urlString, params, serializerName, headers, callbackContext, timeoutInMilliseconds); cordova.getThreadPool().execute(put); - } else if (action.equals("delete")) { + } else if (action.equals("patch")) { + String urlString = args.getString(0); + JSONObject params = args.getJSONObject(1); + String serializerName = args.getString(2); + JSONObject headers = args.getJSONObject(3); + CordovaHttpPatch patch = new CordovaHttpPatch(urlString, params, serializerName, headers, callbackContext); + + cordova.getThreadPool().execute(patch); + } + else if (action.equals("delete")) { String urlString = args.getString(0); JSONObject params = args.getJSONObject(1); JSONObject headers = args.getJSONObject(2);