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