Search in sources :

Example 1 with ParseHttpBody

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

the class ParseHttpRequestTest method testParseHttpRequestBuildWithParseHttpRequest.

@Test
public void testParseHttpRequestBuildWithParseHttpRequest() throws IOException {
    String url = "www.parse.com";
    ParseHttpRequest.Method method = ParseHttpRequest.Method.POST;
    Map<String, String> headers = new HashMap<>();
    String name = "name";
    String value = "value";
    headers.put(name, value);
    String content = "content";
    String contentType = "application/json";
    ParseByteArrayHttpBody body = new ParseByteArrayHttpBody(content, contentType);
    ParseHttpRequest request = new ParseHttpRequest.Builder().setUrl(url).addHeader(name, value).setMethod(method).setBody(body).build();
    String newURL = "www.api.parse.com";
    ParseHttpRequest newRequest = new ParseHttpRequest.Builder(request).setUrl(newURL).build();
    assertEquals(newURL, newRequest.getUrl());
    assertEquals(method.toString(), newRequest.getMethod().toString());
    assertEquals(1, newRequest.getAllHeaders().size());
    assertEquals(value, newRequest.getHeader(name));
    ParseHttpBody bodyAgain = newRequest.getBody();
    assertEquals(contentType, bodyAgain.getContentType());
    assertArrayEquals(content.getBytes(), ParseIOUtils.toByteArray(body.getContent()));
}
Also used : ParseHttpRequest(com.parse.http.ParseHttpRequest) HashMap(java.util.HashMap) ParseHttpBody(com.parse.http.ParseHttpBody) Test(org.junit.Test)

Example 2 with ParseHttpBody

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

the class ParseOkHttpClient method getRequest.

@Override
/* package */
Request getRequest(ParseHttpRequest parseRequest) throws IOException {
    Request.Builder okHttpRequestBuilder = new Request.Builder();
    ParseHttpRequest.Method method = parseRequest.getMethod();
    // Set method
    switch(method) {
        case GET:
            okHttpRequestBuilder.get();
            break;
        case DELETE:
            okHttpRequestBuilder.delete();
            break;
        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;
    }
    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)

Example 3 with ParseHttpBody

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

the class ParseHttpRequestTest method testParseHttpRequestBuilderInitialization.

@Test
public void testParseHttpRequestBuilderInitialization() throws IOException {
    String url = "www.parse.com";
    ParseHttpRequest.Method method = ParseHttpRequest.Method.POST;
    Map<String, String> headers = new HashMap<>();
    String name = "name";
    String value = "value";
    headers.put(name, value);
    String content = "content";
    String contentType = "application/json";
    ParseByteArrayHttpBody body = new ParseByteArrayHttpBody(content, contentType);
    ParseHttpRequest request = new ParseHttpRequest.Builder().setUrl(url).addHeader(name, value).setMethod(method).setBody(body).build();
    ParseHttpRequest requestAgain = new ParseHttpRequest.Builder(request).build();
    assertEquals(url, requestAgain.getUrl());
    assertEquals(method.toString(), requestAgain.getMethod().toString());
    assertEquals(1, requestAgain.getAllHeaders().size());
    assertEquals(value, requestAgain.getHeader(name));
    ParseHttpBody bodyAgain = requestAgain.getBody();
    assertEquals(contentType, bodyAgain.getContentType());
    assertArrayEquals(content.getBytes(), ParseIOUtils.toByteArray(body.getContent()));
}
Also used : ParseHttpRequest(com.parse.http.ParseHttpRequest) HashMap(java.util.HashMap) ParseHttpBody(com.parse.http.ParseHttpBody) Test(org.junit.Test)

Example 4 with ParseHttpBody

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

the class ParseHttpRequestTest method testParseHttpRequestGetMethod.

@Test
public void testParseHttpRequestGetMethod() throws IOException {
    String url = "www.parse.com";
    ParseHttpRequest.Method method = ParseHttpRequest.Method.POST;
    Map<String, String> headers = new HashMap<>();
    String name = "name";
    String value = "value";
    headers.put(name, value);
    String content = "content";
    String contentType = "application/json";
    ParseByteArrayHttpBody body = new ParseByteArrayHttpBody(content, contentType);
    ParseHttpRequest request = new ParseHttpRequest.Builder().setUrl(url).addHeader(name, value).setMethod(method).setBody(body).build();
    assertEquals(url, request.getUrl());
    assertEquals(method.toString(), request.getMethod().toString());
    assertEquals(1, request.getAllHeaders().size());
    assertEquals(value, request.getHeader(name));
    ParseHttpBody bodyAgain = request.getBody();
    assertEquals(contentType, bodyAgain.getContentType());
    assertArrayEquals(content.getBytes(), ParseIOUtils.toByteArray(body.getContent()));
}
Also used : ParseHttpRequest(com.parse.http.ParseHttpRequest) HashMap(java.util.HashMap) ParseHttpBody(com.parse.http.ParseHttpBody) Test(org.junit.Test)

Example 5 with ParseHttpBody

use of com.parse.http.ParseHttpBody 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

ParseHttpBody (com.parse.http.ParseHttpBody)5 ParseHttpRequest (com.parse.http.ParseHttpRequest)5 HashMap (java.util.HashMap)5 Test (org.junit.Test)3 Map (java.util.Map)2 Headers (okhttp3.Headers)2 Request (okhttp3.Request)2