Search in sources :

Example 1 with RestRequest

use of com.wordpress.rest.RestRequest in project WordPress-Android by wordpress-mobile.

the class RestClientUtils method getSynchronous.

/**
     * Make Synchronous GET request with params
     *
     * @throws TimeoutException
     * @throws ExecutionException
     * @throws InterruptedException
     */
public JSONObject getSynchronous(String path, Map<String, String> params, RetryPolicy retryPolicy) throws InterruptedException, ExecutionException, TimeoutException {
    RequestFuture<JSONObject> future = RequestFuture.newFuture();
    HashMap<String, String> paramsWithLocale = getRestLocaleParams(mContext);
    if (params != null) {
        paramsWithLocale.putAll(params);
    }
    String realPath = getSanitizedPath(path);
    if (TextUtils.isEmpty(realPath)) {
        realPath = path;
    }
    paramsWithLocale.putAll(getSanitizedParameters(path));
    RestRequest request = mRestClient.makeRequest(Method.GET, mRestClient.getAbsoluteURL(realPath, paramsWithLocale), null, future, future);
    if (retryPolicy == null) {
        retryPolicy = new DefaultRetryPolicy(REST_TIMEOUT_MS, REST_MAX_RETRIES_GET, REST_BACKOFF_MULT);
    }
    request.setRetryPolicy(retryPolicy);
    AuthenticatorRequest authCheck = new AuthenticatorRequest(request, null, mRestClient, mAuthenticator);
    //this insert the request into the queue. //TODO: Verify that everything is OK on REST calls without a valid token
    authCheck.send();
    JSONObject response = future.get();
    return response;
}
Also used : RestRequest(com.wordpress.rest.RestRequest) JsonRestRequest(com.wordpress.rest.JsonRestRequest) JSONObject(org.json.JSONObject) DefaultRetryPolicy(com.android.volley.DefaultRetryPolicy)

Example 2 with RestRequest

use of com.wordpress.rest.RestRequest in project WordPress-Android by wordpress-mobile.

the class RestClientUtils method get.

/**
     * Make GET request with params
     */
public Request<JSONObject> get(String path, Map<String, String> params, RetryPolicy retryPolicy, Listener listener, ErrorListener errorListener) {
    // turn params into querystring
    HashMap<String, String> paramsWithLocale = getRestLocaleParams(mContext);
    if (params != null) {
        paramsWithLocale.putAll(params);
    }
    String realPath = getSanitizedPath(path);
    if (TextUtils.isEmpty(realPath)) {
        realPath = path;
    }
    paramsWithLocale.putAll(getSanitizedParameters(path));
    RestRequest request = mRestClient.makeRequest(Method.GET, mRestClient.getAbsoluteURL(realPath, paramsWithLocale), null, listener, errorListener);
    if (retryPolicy == null) {
        retryPolicy = new DefaultRetryPolicy(REST_TIMEOUT_MS, REST_MAX_RETRIES_GET, REST_BACKOFF_MULT);
    }
    request.setRetryPolicy(retryPolicy);
    AuthenticatorRequest authCheck = new AuthenticatorRequest(request, errorListener, mRestClient, mAuthenticator);
    authCheck.send();
    return request;
}
Also used : RestRequest(com.wordpress.rest.RestRequest) JsonRestRequest(com.wordpress.rest.JsonRestRequest) DefaultRetryPolicy(com.android.volley.DefaultRetryPolicy)

Example 3 with RestRequest

use of com.wordpress.rest.RestRequest in project WordPress-Android by wordpress-mobile.

the class RestClientCustomizableMock method makeRequest.

public RestRequest makeRequest(int method, String url, java.util.Map<String, String> params, RestRequest.Listener listener, RestRequest.ErrorListener errorListener) {
    AppLog.v(T.TESTS, this.getClass() + ": makeRequest(" + url + ")");
    RestRequest dummyReturnValue = new RestRequest(method, url, params, listener, errorListener);
    // URL example: https://public-api.wordpress.com/rest/v1/me
    // Filename: default-public-api-wordpress-com-rest-v1-me.json
    String filename = mPrefix + "-" + url.replace("https://", "").replace("/", "-").replace(".", "-").replace("?", "-") + ".json";
    if ("password-invalid".equals(mPrefix) && errorListener != null) {
        errorListener.onErrorResponse(forgeVolleyErrorFromFilename(filename));
        return dummyReturnValue;
    }
    if ("username-exists".equals(mPrefix) && errorListener != null) {
        errorListener.onErrorResponse(forgeVolleyErrorFromFilename(filename));
        return dummyReturnValue;
    }
    if ("timeout".equals(mPrefix) && errorListener != null) {
        errorListener.onErrorResponse(forgeVolleyTimeoutError());
        return dummyReturnValue;
    }
    if ("site-reserved".equals(mPrefix) && errorListener != null) {
        errorListener.onErrorResponse(forgeVolleyErrorFromFilename(filename));
        return dummyReturnValue;
    }
    String data = fileToString(filename);
    if (data == null) {
        AppLog.e(T.TESTS, "Can't read file: " + filename);
        throw new RuntimeException("Can't read file: " + filename);
    }
    try {
        JSONObject jsonObj = new JSONObject(data);
        listener.onResponse(jsonObj);
    } catch (JSONException je) {
        AppLog.e(T.TESTS, je.toString());
    }
    return dummyReturnValue;
}
Also used : RestRequest(com.wordpress.rest.RestRequest) JSONObject(org.json.JSONObject) JSONException(org.json.JSONException)

Example 4 with RestRequest

use of com.wordpress.rest.RestRequest in project WordPress-Android by wordpress-mobile.

the class RestClientUtils method post.

/**
     * Make POST request with params
     */
public void post(final String path, Map<String, String> params, RetryPolicy retryPolicy, Listener listener, ErrorListener errorListener) {
    final RestRequest request = mRestClient.makeRequest(Method.POST, mRestClient.getAbsoluteURL(path, getRestLocaleParams(mContext)), params, listener, errorListener);
    if (retryPolicy == null) {
        retryPolicy = new DefaultRetryPolicy(REST_TIMEOUT_MS, REST_MAX_RETRIES_POST, //Do not retry on failure
        REST_BACKOFF_MULT);
    }
    request.setRetryPolicy(retryPolicy);
    AuthenticatorRequest authCheck = new AuthenticatorRequest(request, errorListener, mRestClient, mAuthenticator);
    authCheck.send();
}
Also used : RestRequest(com.wordpress.rest.RestRequest) JsonRestRequest(com.wordpress.rest.JsonRestRequest) DefaultRetryPolicy(com.android.volley.DefaultRetryPolicy)

Aggregations

RestRequest (com.wordpress.rest.RestRequest)4 DefaultRetryPolicy (com.android.volley.DefaultRetryPolicy)3 JsonRestRequest (com.wordpress.rest.JsonRestRequest)3 JSONObject (org.json.JSONObject)2 JSONException (org.json.JSONException)1