-
Notifications
You must be signed in to change notification settings - Fork 130
Expand file tree
/
Copy pathMultiAuthAppSyncGraphQLOperation.java
More file actions
280 lines (250 loc) · 10.7 KB
/
MultiAuthAppSyncGraphQLOperation.java
File metadata and controls
280 lines (250 loc) · 10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*
* Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
package com.amplifyframework.api.aws;
import android.annotation.SuppressLint;
import androidx.annotation.NonNull;
import com.amplifyframework.AmplifyException;
import com.amplifyframework.annotations.InternalAmplifyApi;
import com.amplifyframework.api.ApiException;
import com.amplifyframework.api.ApiException.ApiAuthException;
import com.amplifyframework.api.aws.auth.ApiRequestDecoratorFactory;
import com.amplifyframework.api.aws.auth.RequestDecorator;
import com.amplifyframework.api.graphql.GraphQLRequest;
import com.amplifyframework.api.graphql.GraphQLResponse;
import com.amplifyframework.core.Amplify;
import com.amplifyframework.core.Consumer;
import com.amplifyframework.core.category.CategoryType;
import com.amplifyframework.core.model.auth.AuthorizationTypeIterator;
import com.amplifyframework.datastore.appsync.AppSyncExtensions;
import com.amplifyframework.logging.Logger;
import com.amplifyframework.util.Empty;
import java.io.IOException;
import java.util.Objects;
import java.util.concurrent.ExecutorService;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.ResponseBody;
/**
* An operation to enqueue a GraphQL request to OkHttp client,
* with the goal of obtaining a list of responses. For example,
* this is used for a LIST query vs. a GET query or most mutations.
* @param <R> Casted type of GraphQL result data
*/
public final class MultiAuthAppSyncGraphQLOperation<R> extends AWSGraphQLOperation<R> {
private static final Logger LOG = Amplify.Logging.logger(CategoryType.API, "amplify:aws-api");
private static final String CONTENT_TYPE = "application/json";
private final String endpoint;
private final OkHttpClient client;
private final Consumer<GraphQLResponse<R>> onResponse;
private final Consumer<ApiException> onFailure;
private final ApiRequestDecoratorFactory apiRequestDecoratorFactory;
private final ExecutorService executorService;
private AuthorizationTypeIterator authTypes;
private Call ongoingCall;
/**
* Constructs a new AppSyncGraphQLOperation.
* @param builder An instance of the {@link Builder} object.
*/
private MultiAuthAppSyncGraphQLOperation(Builder<R> builder) {
super(builder.request, builder.responseFactory, builder.apiName);
this.apiRequestDecoratorFactory = builder.apiRequestDecoratorFactory;
this.endpoint = builder.endpoint;
this.client = builder.client;
this.onResponse = builder.onResponse;
this.onFailure = builder.onFailure;
this.executorService = builder.executorService;
if (!(getRequest() instanceof AppSyncGraphQLRequest)) {
onFailure.accept(new ApiException(
"Multiauth only supported with AppSyncGraphQLRequest<T>.",
AmplifyException.TODO_RECOVERY_SUGGESTION
));
return;
}
AppSyncGraphQLRequest<R> appSyncRequest = (AppSyncGraphQLRequest<R>) getRequest();
this.authTypes = MultiAuthModeStrategy.getInstance()
.authTypesFor(appSyncRequest.getModelSchema(),
appSyncRequest.getAuthRuleOperation());
}
@Override
public void start() {
// No-op if start() is called post-execution
if (ongoingCall != null && ongoingCall.isExecuted()) {
return;
}
executorService.submit(this::dispatchRequest);
}
private void dispatchRequest() {
if (authTypes.hasNext()) {
AuthorizationType authType = authTypes.next();
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();
Request decoratedOkHttpRequest;
try {
RequestDecorator requestDecorator = apiRequestDecoratorFactory.forAuthType(authType);
decoratedOkHttpRequest = requestDecorator.decorate(okHttpRequest);
} catch (ApiException apiException) {
LOG.warn("Failed to make a successful request with " + authType, apiException);
// Only queue up a retry if it's an auth-related exception.
if (apiException instanceof ApiAuthException && authTypes.hasNext()) {
executorService.submit(this::dispatchRequest);
} else {
onFailure.accept(apiException);
}
return;
}
LOG.debug("Request: " + getRequest().getContent());
ongoingCall = client.newCall(decoratedOkHttpRequest);
ongoingCall.enqueue(new OkHttpCallback());
} else {
onFailure.accept(new ApiAuthException(
"Unable to successfully complete request with any of the compatible auth types.",
"Check your application logs for detail."
));
}
}
@Override
public void cancel() {
if (ongoingCall != null) {
ongoingCall.cancel();
}
}
private boolean hasAuthRelatedErrors(GraphQLResponse<R> response) {
for (GraphQLResponse.Error error : response.getErrors()) {
if (!Empty.check(error.getExtensions())) {
AppSyncExtensions extensions = new AppSyncExtensions(error.getExtensions());
return extensions.isUnauthorizedErrorType();
}
}
return false;
}
@InternalAmplifyApi
public static <R> Builder<R> builder() {
return new Builder<>();
}
@SuppressLint("SyntheticAccessor")
class OkHttpCallback implements Callback {
@Override
public void onResponse(@NonNull Call call, @NonNull Response response) {
final ResponseBody responseBody = response.body();
String jsonResponse = null;
if (responseBody != null) {
try {
jsonResponse = responseBody.string();
} catch (IOException exception) {
onFailure.accept(new ApiException(
"Could not retrieve the response body from the returned JSON",
exception, AmplifyException.TODO_RECOVERY_SUGGESTION
));
return;
}
}
try {
GraphQLResponse<R> graphQLResponse = wrapResponse(jsonResponse);
if (graphQLResponse.hasErrors() && hasAuthRelatedErrors(graphQLResponse)) {
if (authTypes.hasNext()) {
executorService.submit(MultiAuthAppSyncGraphQLOperation.this::dispatchRequest);
} else {
onFailure.accept(new ApiAuthException(
"Unable to successfully complete request with any of the compatible auth types.",
"Check your application logs for detail."
));
}
} else {
onResponse.accept(graphQLResponse);
}
//TODO: Dispatch to hub
} catch (ApiException exception) {
onFailure.accept(exception);
}
}
@Override
public void onFailure(@NonNull Call call, @NonNull IOException exception) {
onFailure.accept(new ApiException(
"OkHttp client request failed.", exception, "See attached exception for more details."
));
}
}
@InternalAmplifyApi
public static final class Builder<R> {
private String endpoint;
private OkHttpClient client;
private GraphQLRequest<R> request;
private GraphQLResponse.Factory responseFactory;
private ApiRequestDecoratorFactory apiRequestDecoratorFactory;
private Consumer<GraphQLResponse<R>> onResponse;
private Consumer<ApiException> onFailure;
private ExecutorService executorService;
private String apiName;
@InternalAmplifyApi
public Builder<R> endpoint(@NonNull String endpoint) {
this.endpoint = Objects.requireNonNull(endpoint);
return this;
}
@InternalAmplifyApi
public Builder<R> client(@NonNull OkHttpClient client) {
this.client = Objects.requireNonNull(client);
return this;
}
@InternalAmplifyApi
public Builder<R> request(@NonNull GraphQLRequest<R> request) {
this.request = Objects.requireNonNull(request);
return this;
}
@InternalAmplifyApi
public Builder<R> responseFactory(@NonNull GraphQLResponse.Factory responseFactory) {
this.responseFactory = Objects.requireNonNull(responseFactory);
return this;
}
@InternalAmplifyApi
public Builder<R> onResponse(@NonNull Consumer<GraphQLResponse<R>> onResponse) {
this.onResponse = Objects.requireNonNull(onResponse);
return this;
}
@InternalAmplifyApi
public Builder<R> onFailure(@NonNull Consumer<ApiException> onFailure) {
this.onFailure = Objects.requireNonNull(onFailure);
return this;
}
@InternalAmplifyApi
public Builder<R> apiRequestDecoratorFactory(ApiRequestDecoratorFactory apiRequestDecoratorFactory) {
this.apiRequestDecoratorFactory = apiRequestDecoratorFactory;
return this;
}
@InternalAmplifyApi
public Builder<R> executorService(ExecutorService executorService) {
this.executorService = executorService;
return this;
}
@InternalAmplifyApi
public Builder<R> apiName(String apiName) {
this.apiName = apiName;
return this;
}
@InternalAmplifyApi
@SuppressLint("SyntheticAccessor")
public MultiAuthAppSyncGraphQLOperation<R> build() {
return new MultiAuthAppSyncGraphQLOperation<>(this);
}
}
}