Search in sources :

Example 31 with ApiException

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

the class SubscriptionEndpoint method releaseSubscription.

synchronized void releaseSubscription(String subscriptionId) throws ApiException {
    // First thing we should do is remove it from the pending subscription collection so
    // the other methods can't grab a hold of the subscription.
    final Subscription<?> subscription = subscriptions.get(subscriptionId);
    boolean wasSubscriptionPending = pendingSubscriptionIds.remove(subscriptionId);
    // If the subscription was not in the either of the subscriptions collections.
    if (subscription == null && !wasSubscriptionPending) {
        throw new ApiException("No existing subscription with the given id.", AmplifyException.TODO_RECOVERY_SUGGESTION);
    }
    if (!wasSubscriptionPending && !webSocketListener.isDisconnectedState()) {
        try {
            webSocket.send(new JSONObject().put("type", "stop").put("id", subscriptionId).toString());
        } catch (JSONException jsonException) {
            throw new ApiException("Failed to construct subscription release message.", jsonException, AmplifyException.TODO_RECOVERY_SUGGESTION);
        }
        subscription.awaitSubscriptionCompleted();
    }
    subscriptions.remove(subscriptionId);
    // If we have zero subscriptions, close the WebSocket
    if (subscriptions.size() == 0) {
        LOG.info("No more active subscriptions. Closing web socket.");
        timeoutWatchdog.stop();
        webSocket.close(NORMAL_CLOSURE_STATUS, "No active subscriptions");
    }
}
Also used : JSONObject(org.json.JSONObject) JSONException(org.json.JSONException) ApiException(com.amplifyframework.api.ApiException)

Example 32 with ApiException

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

the class AWSRestOperation method start.

@Override
public void start() {
    // No-op if start() is called post-execution
    if (ongoingCall != null && ongoingCall.isExecuted()) {
        return;
    }
    try {
        URL url = RestRequestFactory.createURL(endpoint, getRequest().getPath(), getRequest().getQueryParameters());
        Request request = RestRequestFactory.createRequest(url, getRequest().getData(), getRequest().getHeaders(), getRequest().getHttpMethod());
        ongoingCall = client.newCall(request);
        ongoingCall.enqueue(new AWSRestOperation.OkHttpCallback());
    } catch (Exception error) {
        // Cancel if possible
        if (ongoingCall != null) {
            ongoingCall.cancel();
        }
        onFailure.accept(new ApiException("OkHttp client failed to make a successful request.", error, AmplifyException.TODO_RECOVERY_SUGGESTION));
    }
}
Also used : Request(okhttp3.Request) RestOperationRequest(com.amplifyframework.api.rest.RestOperationRequest) URL(java.net.URL) AmplifyException(com.amplifyframework.AmplifyException) IOException(java.io.IOException) ApiException(com.amplifyframework.api.ApiException) ApiException(com.amplifyframework.api.ApiException)

Example 33 with ApiException

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

the class AppSyncGraphQLOperation method dispatchRequest.

private void dispatchRequest() {
    try {
        LOG.debug("Request: " + getRequest().getContent());
        RequestDecorator requestDecorator = apiRequestDecoratorFactory.fromGraphQLRequest(getRequest());
        Request okHttpRequest = new Request.Builder().url(endpoint).addHeader("accept", CONTENT_TYPE).addHeader("content-type", CONTENT_TYPE).post(RequestBody.create(getRequest().getContent(), MediaType.parse(CONTENT_TYPE))).build();
        ongoingCall = client.newCall(requestDecorator.decorate(okHttpRequest));
        ongoingCall.enqueue(new OkHttpCallback());
    } catch (Exception error) {
        // Cancel if possible
        if (ongoingCall != null) {
            ongoingCall.cancel();
        }
        onFailure.accept(new ApiException("OkHttp client failed to make a successful request.", error, AmplifyException.TODO_RECOVERY_SUGGESTION));
    }
}
Also used : Request(okhttp3.Request) GraphQLRequest(com.amplifyframework.api.graphql.GraphQLRequest) RequestDecorator(com.amplifyframework.api.aws.auth.RequestDecorator) AmplifyException(com.amplifyframework.AmplifyException) IOException(java.io.IOException) ApiException(com.amplifyframework.api.ApiException) ApiException(com.amplifyframework.api.ApiException)

Aggregations

ApiException (com.amplifyframework.api.ApiException)33 Test (org.junit.Test)12 GraphQLResponse (com.amplifyframework.api.graphql.GraphQLResponse)11 GraphQLRequest (com.amplifyframework.api.graphql.GraphQLRequest)9 AmplifyException (com.amplifyframework.AmplifyException)8 PaginatedResult (com.amplifyframework.api.graphql.PaginatedResult)7 JSONObject (org.json.JSONObject)7 Consumer (com.amplifyframework.core.Consumer)6 IOException (java.io.IOException)6 Request (okhttp3.Request)6 MockResponse (okhttp3.mockwebserver.MockResponse)6 JSONException (org.json.JSONException)6 ApiAuthException (com.amplifyframework.api.ApiException.ApiAuthException)5 BlogOwner (com.amplifyframework.testmodels.commentsblog.BlogOwner)5 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)5 NonNull (androidx.annotation.NonNull)4 RestOperationRequest (com.amplifyframework.api.rest.RestOperationRequest)4 RestResponse (com.amplifyframework.api.rest.RestResponse)4 Action (com.amplifyframework.core.Action)4 Cancelable (com.amplifyframework.core.async.Cancelable)4