use of com.apollographql.apollo.api.Subscription in project aws-mobile-appsync-sdk-android by awslabs.
the class ApolloCallTracker method registerCall.
/**
* <p>Adds provided {@link GraphQLCall} that is currently in progress.</p>
*
* <p><b>Note</b>: This method needs to be called right before an apolloCall is executed.</p>
*/
void registerCall(@Nonnull GraphQLCall call) {
checkNotNull(call, "call == null");
Operation operation = call.operation();
if (operation instanceof Query) {
registerQueryCall((AppSyncQueryCall) call);
} else if (operation instanceof Mutation) {
registerMutationCall((AppSyncMutationCall) call);
} else if (operation instanceof Subscription) {
} else {
throw new IllegalArgumentException("Unknown call type");
}
}
use of com.apollographql.apollo.api.Subscription in project aws-mobile-appsync-sdk-android by awslabs.
the class ApolloCallTracker method unregisterCall.
/**
* <p>Removes provided {@link GraphQLCall} that finished his execution, if it is found, else throws an
* {@link AssertionError}.</p>
*
* If the removal operation is successful and no active running calls are found, then the registered
* {@link ApolloCallTracker#idleResourceCallback} is invoked.
*
* <p><b>Note</b>: This method needs to be called right after an apolloCall is completed (whether successful or
* failed).</p>
*/
void unregisterCall(@Nonnull GraphQLCall call) {
checkNotNull(call, "call == null");
Operation operation = call.operation();
if (operation instanceof Query) {
unregisterQueryCall((AppSyncQueryCall) call);
} else if (operation instanceof Mutation) {
unregisterMutationCall((AppSyncMutationCall) call);
} else if (operation instanceof Subscription) {
} else {
throw new IllegalArgumentException("Unknown call type");
}
}
use of com.apollographql.apollo.api.Subscription in project aws-mobile-appsync-sdk-android by awslabs.
the class AppSyncSubscriptionInterceptor method interceptAsync.
@Override
public void interceptAsync(@Nonnull final InterceptorRequest request, @Nonnull ApolloInterceptorChain chain, @Nonnull final Executor dispatcher, @Nonnull final CallBack callBack) {
final boolean isSubscription = request.operation instanceof Subscription;
if (!isSubscription) {
chain.proceedAsync(request, dispatcher, callBack);
return;
}
chain.proceedAsync(request, dispatcher, new CallBack() {
@Override
public void onResponse(@Nonnull final InterceptorResponse response) {
dispatcher.execute(new Runnable() {
@Override
public void run() {
// Declared here to allow error message to have scope
Map<String, Object> responseMap = null;
try {
ResponseJsonStreamReader responseStreamReader = ApolloJsonReader.responseJsonStreamReader(new BufferedSourceJsonReader(response.httpResponse.get().body().source()));
responseMap = responseStreamReader.toMap();
/* The response is of the form
extensions
subscription
mqttConnections
[ url, topics [], clientID ]
newSubscriptions
individualSub
topic, expireTime
data
individualSubscriptions
*/
// Get Subscription information by looking through the response
Map<String, LinkedHashMap> extensions = (Map) responseMap.get("extensions");
Map<String, Object> subscriptions = (Map) extensions.get("subscription");
List<Map<String, Object>> mqttConnections = (List) subscriptions.get("mqttConnections");
// Collect all the topics listed under newSubscriptions in the newTopics var.
List<String> newTopics = new ArrayList<>();
Collection<Map> newSubscriptions = ((Map) subscriptions.get("newSubscriptions")).values();
for (Map subscriptionInstance : newSubscriptions) {
if (subscriptionInstance.containsKey("topic")) {
newTopics.add((String) subscriptionInstance.get("topic"));
}
}
SubscriptionResponse subscriptionResponse = new SubscriptionResponse();
for (Map<String, Object> mqttConnection : mqttConnections) {
final String clientId = (String) mqttConnection.get("client");
final String wssURI = (String) mqttConnection.get("url");
final String[] preExistingTopics = ((List<String>) mqttConnection.get("topics")).toArray(new String[0]);
subscriptionResponse.add(new SubscriptionResponse.MqttInfo(clientId, wssURI, preExistingTopics));
}
AppSyncSubscriptionInterceptor.this.mSubscriptionManager.subscribe((Subscription) request.operation, newTopics, subscriptionResponse, mapResponseNormalizer);
Response parsedResponse = parseSubscription(request.operation, response);
callBack.onResponse(new InterceptorResponse(response.httpResponse.get(), parsedResponse, null));
} catch (Exception e) {
try {
callBack.onFailure(new ApolloException("Failed to parse subscription response: " + responseMap, e));
} catch (Exception e1) {
callBack.onFailure(new ApolloException("Failed to parse subscription response, failed to get body string", e));
}
} finally {
callBack.onCompleted();
}
}
});
}
@Override
public void onFetch(FetchSourceType sourceType) {
callBack.onFetch(sourceType);
}
@Override
public void onFailure(@Nonnull ApolloException e) {
callBack.onFailure(e);
}
@Override
public void onCompleted() {
/* call onCompleted in onResponse in case of error */
}
});
}
Aggregations