Search in sources :

Example 1 with AWSRestOperation

use of com.amplifyframework.api.aws.operation.AWSRestOperation 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)

Aggregations

ApiException (com.amplifyframework.api.ApiException)1 AWSRestOperation (com.amplifyframework.api.aws.operation.AWSRestOperation)1 RestOperationRequest (com.amplifyframework.api.rest.RestOperationRequest)1