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;
}
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();
}));
}
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());
}
Aggregations