patch method android

This commit is contained in:
Akshat Khatri - AK 2017-10-01 17:26:20 +11:00
parent 52ccfc6aca
commit 09da3c9a97
3 changed files with 86 additions and 7 deletions

View File

@ -236,6 +236,11 @@ public class HttpRequest {
*/ */
public static final String METHOD_POST = "POST"; public static final String METHOD_POST = "POST";
/**
* 'PATCH' request method
*/
public static final String METHOD_PATCH = "PATCH";
/** /**
* 'PUT' request method * 'PUT' request method
*/ */
@ -1176,6 +1181,70 @@ public class HttpRequest {
return post(encode ? encode(url) : url); 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 * Start a 'PUT' request to the given URL
* *

View File

@ -16,8 +16,8 @@ import com.github.kevinsawicki.http.HttpRequest;
import com.github.kevinsawicki.http.HttpRequest.HttpRequestException; import com.github.kevinsawicki.http.HttpRequest.HttpRequestException;
class CordovaHttpPatch extends CordovaHttp implements Runnable { class CordovaHttpPatch extends CordovaHttp implements Runnable {
public CordovaHttpPatch(String urlString, JSONObject data, String serializerName, JSONObject headers, CallbackContext callbackContext, int timeout) { public CordovaHttpPatch(String urlString, JSONObject params, String serializerName, JSONObject headers, CallbackContext callbackContext, int timeout) {
super(urlString, data, serializerName, headers, timeout, callbackContext); super(urlString, params, serializerName, headers, timeout, callbackContext);
} }
@Override @Override
@ -26,6 +26,7 @@ class CordovaHttpPatch extends CordovaHttp implements Runnable {
HttpRequest request = HttpRequest.patch(this.getUrlString()); HttpRequest request = HttpRequest.patch(this.getUrlString());
request.readTimeout(this.getRequestTimeout()); request.readTimeout(this.getRequestTimeout());
this.setupRedirect(request);
this.setupSecurity(request); this.setupSecurity(request);
request.acceptCharset(CHARSET); request.acceptCharset(CHARSET);
request.headers(this.getHeadersMap()); request.headers(this.getHeadersMap());
@ -43,13 +44,13 @@ class CordovaHttpPatch extends CordovaHttp implements Runnable {
JSONObject response = new JSONObject(); JSONObject response = new JSONObject();
this.addResponseHeaders(request, response); this.addResponseHeaders(request, response);
response.patch("status", code); response.put("status", code);
if (code >= 200 && code < 300) { if (code >= 200 && code < 300) {
response.patch("data", body); response.put("data", body);
this.getCallbackContext().success(response); this.getCallbackContext().success(response);
} else { } else {
response.patch("error", body); response.put("error", body);
this.getCallbackContext().error(response); this.getCallbackContext().error(response);
} }
} catch (JSONException e) { } catch (JSONException e) {

View File

@ -60,7 +60,16 @@ public class CordovaHttpPlugin extends CordovaPlugin {
CordovaHttpPut put = new CordovaHttpPut(urlString, params, serializerName, headers, callbackContext, timeoutInMilliseconds); CordovaHttpPut put = new CordovaHttpPut(urlString, params, serializerName, headers, callbackContext, timeoutInMilliseconds);
cordova.getThreadPool().execute(put); 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); String urlString = args.getString(0);
JSONObject params = args.getJSONObject(1); JSONObject params = args.getJSONObject(1);
JSONObject headers = args.getJSONObject(2); JSONObject headers = args.getJSONObject(2);