Search in sources :

Example 1 with RestOperationRequest

use of com.amplifyframework.api.rest.RestOperationRequest in project amplify-android by aws-amplify.

the class AWSApiPlugin method createRestOperation.

/**
 * Creates a HTTP REST operation.
 * @param type     Operation type
 * @param options  Request options
 * @param onResponse Called when a response is available
 * @param onFailure  Called when no response is available
 * @return A REST Operation
 */
private RestOperation createRestOperation(String apiName, HttpMethod type, RestOptions options, Consumer<RestResponse> onResponse, Consumer<ApiException> onFailure) throws ApiException {
    final ClientDetails clientDetails = apiDetails.get(apiName);
    if (clientDetails == null) {
        throw new ApiException("No client information for API named " + apiName, "Check your amplify configuration to make sure there " + "is a correctly configured section for " + apiName);
    }
    RestOperationRequest operationRequest;
    switch(type) {
        // These ones are special, they don't use any data.
        case HEAD:
        case GET:
        case DELETE:
            if (options.hasData()) {
                throw new ApiException("HTTP method does not support data object! " + type, "Try sending the request without any data in the options.");
            }
            operationRequest = new RestOperationRequest(type, options.getPath(), options.getHeaders(), options.getQueryParameters());
            break;
        case PUT:
        case POST:
        case PATCH:
            operationRequest = new RestOperationRequest(type, options.getPath(), options.getData() == null ? new byte[0] : options.getData(), options.getHeaders(), options.getQueryParameters());
            break;
        default:
            throw new ApiException("Unknown REST operation type: " + type, "Send support type for the request.");
    }
    AWSRestOperation operation = new AWSRestOperation(operationRequest, clientDetails.apiConfiguration.getEndpoint(), clientDetails.okHttpClient, onResponse, onFailure);
    operation.start();
    return operation;
}
Also used : AWSRestOperation(com.amplifyframework.api.aws.operation.AWSRestOperation) RestOperationRequest(com.amplifyframework.api.rest.RestOperationRequest) ApiException(com.amplifyframework.api.ApiException)

Example 2 with RestOperationRequest

use of com.amplifyframework.api.rest.RestOperationRequest in project amplify-android by aws-amplify.

the class AWSRestOperationTest method noErrorEmittedIfOperationIsCancelled.

/**
 * If the user calls {@link AWSRestOperation#cancel()}, then the operation
 * will not fire any callback. This behavior is consistent with iOS's REST operation.
 */
@Test
public void noErrorEmittedIfOperationIsCancelled() {
    long timeToWaitForResponse = 300L;
    RestOperationRequest request = new RestOperationRequest(HttpMethod.GET, baseUrl.uri().getPath(), emptyMap(), emptyMap());
    assertTimedOut(() -> Await.<RestResponse, ApiException>result(timeToWaitForResponse, (onResult, onError) -> {
        AWSRestOperation operation = new AWSRestOperation(request, baseUrl.url().toString(), client, onResult, onError);
        operation.start();
        operation.cancel();
    }));
}
Also used : Collections.emptyMap(java.util.Collections.emptyMap) Assert.assertThrows(org.junit.Assert.assertThrows) RunWith(org.junit.runner.RunWith) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) IOException(java.io.IOException) HashMap(java.util.HashMap) Callable(java.util.concurrent.Callable) ApiException(com.amplifyframework.api.ApiException) RestOperationRequest(com.amplifyframework.api.rest.RestOperationRequest) RestResponse(com.amplifyframework.api.rest.RestResponse) RobolectricTestRunner(org.robolectric.RobolectricTestRunner) HttpMethod(com.amplifyframework.api.rest.HttpMethod) JSONException(org.json.JSONException) JSONObject(org.json.JSONObject) OkHttpClient(okhttp3.OkHttpClient) Await(com.amplifyframework.testutils.Await) After(org.junit.After) Map(java.util.Map) MockWebServer(okhttp3.mockwebserver.MockWebServer) HttpUrl(okhttp3.HttpUrl) MockResponse(okhttp3.mockwebserver.MockResponse) Assert.assertEquals(org.junit.Assert.assertEquals) Before(org.junit.Before) RestOperationRequest(com.amplifyframework.api.rest.RestOperationRequest) Test(org.junit.Test)

Example 3 with RestOperationRequest

use of com.amplifyframework.api.rest.RestOperationRequest in project amplify-android by aws-amplify.

the class AWSRestOperationTest method responseEmittedWhenOperationSucceeds.

/**
 * Tests the happy path, wherein the server returns a response, the
 * operation hasn't been canceled, and we get a {@link RestResponse}
 * at the end of it.
 * @throws ApiException
 *         A possible outcome of the operation. This is not
 *         expected, and would constitute a test failure.
 */
@Test
public void responseEmittedWhenOperationSucceeds() throws ApiException {
    RestOperationRequest request = new RestOperationRequest(HttpMethod.GET, baseUrl.uri().getPath(), emptyMap(), emptyMap());
    RestResponse response = Await.<RestResponse, ApiException>result((onResult, onError) -> {
        AWSRestOperation operation = new AWSRestOperation(request, baseUrl.url().toString(), client, onResult, onError);
        operation.start();
    });
    assertTrue(response.getCode().isSuccessful());
    Map<String, String> expected = new HashMap<>();
    expected.put("foo", "bar,baz");
    expected.put("qux", "quux");
    expected.put("content-length", "21");
    assertEquals(expected, response.getHeaders());
}
Also used : HashMap(java.util.HashMap) RestResponse(com.amplifyframework.api.rest.RestResponse) RestOperationRequest(com.amplifyframework.api.rest.RestOperationRequest) ApiException(com.amplifyframework.api.ApiException) Test(org.junit.Test)

Aggregations

ApiException (com.amplifyframework.api.ApiException)3 RestOperationRequest (com.amplifyframework.api.rest.RestOperationRequest)3 RestResponse (com.amplifyframework.api.rest.RestResponse)2 HashMap (java.util.HashMap)2 Test (org.junit.Test)2 AWSRestOperation (com.amplifyframework.api.aws.operation.AWSRestOperation)1 HttpMethod (com.amplifyframework.api.rest.HttpMethod)1 Await (com.amplifyframework.testutils.Await)1 IOException (java.io.IOException)1 Collections.emptyMap (java.util.Collections.emptyMap)1 Map (java.util.Map)1 Callable (java.util.concurrent.Callable)1 HttpUrl (okhttp3.HttpUrl)1 OkHttpClient (okhttp3.OkHttpClient)1 MockResponse (okhttp3.mockwebserver.MockResponse)1 MockWebServer (okhttp3.mockwebserver.MockWebServer)1 JSONException (org.json.JSONException)1 JSONObject (org.json.JSONObject)1 After (org.junit.After)1 Assert.assertEquals (org.junit.Assert.assertEquals)1