Search in sources :

Example 21 with ParseHttpRequest

use of com.parse.http.ParseHttpRequest in project Parse-SDK-Android by ParsePlatform.

the class ParseRESTCommand method newRequest.

@Override
protected ParseHttpRequest newRequest(ParseHttpRequest.Method method, String url, ProgressCallback uploadProgressCallback) {
    ParseHttpRequest request;
    if (jsonParameters != null && method != ParseHttpRequest.Method.POST && method != ParseHttpRequest.Method.PUT) {
        // The request URI may be too long to include parameters in the URI.
        // To avoid this problem we send the parameters in a POST request json-encoded body
        // and add a http method override parameter in newBody.
        request = super.newRequest(ParseHttpRequest.Method.POST, url, uploadProgressCallback);
    } else {
        request = super.newRequest(method, url, uploadProgressCallback);
    }
    ParseHttpRequest.Builder requestBuilder = new ParseHttpRequest.Builder(request);
    addAdditionalHeaders(requestBuilder);
    return requestBuilder.build();
}
Also used : ParseHttpRequest(com.parse.http.ParseHttpRequest)

Example 22 with ParseHttpRequest

use of com.parse.http.ParseHttpRequest in project Parse-SDK-Android by ParsePlatform.

the class ParseHttpClient method getRequest.

Request getRequest(ParseHttpRequest parseRequest) {
    Request.Builder okHttpRequestBuilder = new Request.Builder();
    ParseHttpRequest.Method method = parseRequest.getMethod();
    // Set method
    switch(method) {
        case GET:
            okHttpRequestBuilder.get();
            break;
        case DELETE:
        case POST:
        case PUT:
            // the following.
            break;
        default:
            // ParseRequest.newRequest().
            throw new IllegalStateException("Unsupported http method " + method.toString());
    }
    // Set url
    okHttpRequestBuilder.url(parseRequest.getUrl());
    // Set Header
    Headers.Builder okHttpHeadersBuilder = new Headers.Builder();
    for (Map.Entry<String, String> entry : parseRequest.getAllHeaders().entrySet()) {
        okHttpHeadersBuilder.add(entry.getKey(), entry.getValue());
    }
    // OkHttp automatically add gzip header so we do not need to deal with it
    Headers okHttpHeaders = okHttpHeadersBuilder.build();
    okHttpRequestBuilder.headers(okHttpHeaders);
    // Set Body
    ParseHttpBody parseBody = parseRequest.getBody();
    ParseOkHttpRequestBody okHttpRequestBody = null;
    if (parseBody != null) {
        okHttpRequestBody = new ParseOkHttpRequestBody(parseBody);
    }
    switch(method) {
        case PUT:
            okHttpRequestBuilder.put(okHttpRequestBody);
            break;
        case POST:
            okHttpRequestBuilder.post(okHttpRequestBody);
            break;
        case DELETE:
            okHttpRequestBuilder.delete(okHttpRequestBody);
    }
    return okHttpRequestBuilder.build();
}
Also used : ParseHttpRequest(com.parse.http.ParseHttpRequest) Headers(okhttp3.Headers) ParseHttpBody(com.parse.http.ParseHttpBody) Request(okhttp3.Request) ParseHttpRequest(com.parse.http.ParseHttpRequest) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

ParseHttpRequest (com.parse.http.ParseHttpRequest)22 ParseHttpResponse (com.parse.http.ParseHttpResponse)12 HashMap (java.util.HashMap)10 Test (org.junit.Test)10 Request (okhttp3.Request)8 ParseHttpBody (com.parse.http.ParseHttpBody)6 ParseNetworkInterceptor (com.parse.http.ParseNetworkInterceptor)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 IOException (java.io.IOException)5 OkHttpClient (okhttp3.OkHttpClient)5 Map (java.util.Map)4 GZIPOutputStream (java.util.zip.GZIPOutputStream)4 Headers (okhttp3.Headers)4 MockResponse (okhttp3.mockwebserver.MockResponse)4 Buffer (okio.Buffer)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 Response (okhttp3.Response)3 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)3 Semaphore (java.util.concurrent.Semaphore)2 Call (okhttp3.Call)2