use of com.amplifyframework.api.ApiException in project amplify-android by aws-amplify.
the class SynchronousApi method onCreate.
/**
* Subscribe to create mutations, by forming a GraphQL subscription request.
*
* @param apiName One of the configured APIs
* @param request A GraphQL subscription request
* @param <T> Type of object for which creation notifications are generated
* @return An observable with which creations may be observed
*/
@NonNull
public <T> Observable<GraphQLResponse<T>> onCreate(@NonNull String apiName, @NonNull GraphQLRequest<T> request) {
return Observable.create(emitter -> {
CompositeDisposable disposable = new CompositeDisposable();
emitter.setDisposable(disposable);
Await.<String, ApiException>result(OPERATION_TIMEOUT_MS, (onSubscriptionStarted, onError) -> {
Cancelable cancelable = asyncDelegate.subscribe(apiName, request, onSubscriptionStarted, emitter::onNext, onError, emitter::onComplete);
if (cancelable != null) {
disposable.add(Disposable.fromAction(cancelable::cancel));
}
});
});
}
use of com.amplifyframework.api.ApiException in project amplify-android by aws-amplify.
the class AWSApiPluginTest method headerInterceptorsAreConfigured.
/**
* Validates that the plugin adds custom headers into the outgoing OkHttp request.
* @throws ApiException Thrown from the query() call.
* @throws InterruptedException Possible thrown from takeRequest()
*/
@Test
public void headerInterceptorsAreConfigured() throws ApiException, InterruptedException {
// Arrange some response. This isn't the point of the test,
// but it keeps the mock web server from freezing up.
webServer.enqueue(new MockResponse().setBody(Resources.readAsString("blog-owners-query-results.json")));
// Fire off a request
Await.<GraphQLResponse<PaginatedResult<BlogOwner>>, ApiException>result((onResult, onError) -> plugin.query(ModelQuery.list(BlogOwner.class), onResult, onError));
RecordedRequest recordedRequest = webServer.takeRequest(5, TimeUnit.MILLISECONDS);
assertNotNull(recordedRequest);
assertEquals("specialValue", recordedRequest.getHeader("specialKey"));
}
use of com.amplifyframework.api.ApiException in project amplify-android by aws-amplify.
the class AWSApiPluginTest method requestUsesIamForAuth.
/**
* Ensure the auth mode used for the request is AWS_IAM. We verify this by
* @throws AmplifyException Not expected.
* @throws InterruptedException Not expected.
*/
@Test
public void requestUsesIamForAuth() throws AmplifyException, InterruptedException {
webServer.enqueue(new MockResponse().setBody(Resources.readAsString("blog-owners-query-results.json")));
AppSyncGraphQLRequest<PaginatedResult<BlogOwner>> appSyncGraphQLRequest = createQueryRequestWithAuthMode(BlogOwner.class, AuthorizationType.AWS_IAM);
GraphQLResponse<PaginatedResult<BlogOwner>> actualResponse = Await.<GraphQLResponse<PaginatedResult<BlogOwner>>, ApiException>result((onResult, onError) -> plugin.query(appSyncGraphQLRequest, onResult, onError));
RecordedRequest recordedRequest = webServer.takeRequest();
assertNull(recordedRequest.getHeader("x-api-key"));
assertNotNull(recordedRequest.getHeader("authorization"));
assertTrue(recordedRequest.getHeader("authorization").startsWith("AWS4-HMAC-SHA256"));
assertEquals(Arrays.asList("Curly", "Moe", "Larry"), Observable.fromIterable(actualResponse.getData()).map(BlogOwner::getName).toList().blockingGet());
}
use of com.amplifyframework.api.ApiException in project amplify-android by aws-amplify.
the class AWSApiPluginTest method requestUsesCognitoForAuth.
/**
* If the auth mode is set for the individual request, ensure that the resulting request
* to AppSync has the correct auth header.
* @throws AmplifyException Not expected.
* @throws InterruptedException Not expected.
*/
@Test
public void requestUsesCognitoForAuth() throws AmplifyException, InterruptedException {
webServer.enqueue(new MockResponse().setBody(Resources.readAsString("blog-owners-query-results.json")));
AppSyncGraphQLRequest<PaginatedResult<BlogOwner>> appSyncGraphQLRequest = createQueryRequestWithAuthMode(BlogOwner.class, AuthorizationType.AMAZON_COGNITO_USER_POOLS);
GraphQLResponse<PaginatedResult<BlogOwner>> actualResponse = Await.<GraphQLResponse<PaginatedResult<BlogOwner>>, ApiException>result((onResult, onError) -> plugin.query(appSyncGraphQLRequest, onResult, onError));
RecordedRequest recordedRequest = webServer.takeRequest();
assertNull(recordedRequest.getHeader("x-api-key"));
assertNotNull(recordedRequest.getHeader("authorization"));
assertEquals("FAKE_TOKEN", recordedRequest.getHeader("authorization"));
assertEquals(Arrays.asList("Curly", "Moe", "Larry"), Observable.fromIterable(actualResponse.getData()).map(BlogOwner::getName).toList().blockingGet());
}
use of com.amplifyframework.api.ApiException in project amplify-android by aws-amplify.
the class AWSApiPluginTest method setup.
/**
* Sets up the test.
* @throws ApiException On failure to configure plugin
* @throws IOException On failure to start web server
* @throws JSONException On failure to arrange configuration JSON
*/
@Before
public void setup() throws ApiException, IOException, JSONException {
webServer = new MockWebServer();
webServer.start(8080);
baseUrl = webServer.url("/");
JSONObject configuration = new JSONObject().put("graphQlApi", new JSONObject().put("endpointType", "GraphQL").put("endpoint", baseUrl.url()).put("region", "us-east-1").put("authorizationType", "API_KEY").put("apiKey", "FAKE-API-KEY"));
ApiAuthProviders apiAuthProviders = ApiAuthProviders.builder().awsCredentialsProvider(new AWSCredentialsProvider() {
@Override
public AWSCredentials getCredentials() {
return new BasicAWSCredentials("DUMMY_ID", "DUMMY_SECRET");
}
@Override
public void refresh() {
}
}).cognitoUserPoolsAuthProvider(new CognitoUserPoolsAuthProvider() {
@Override
public String getLatestAuthToken() throws ApiException {
return "FAKE_TOKEN";
}
@Override
public String getUsername() {
return "FAKE_USER";
}
}).build();
this.plugin = AWSApiPlugin.builder().configureClient("graphQlApi", builder -> {
builder.addInterceptor(chain -> {
return chain.proceed(chain.request().newBuilder().addHeader("specialKey", "specialValue").build());
});
builder.connectTimeout(10, TimeUnit.SECONDS);
}).apiAuthProviders(apiAuthProviders).build();
this.plugin.configure(configuration, ApplicationProvider.getApplicationContext());
}
Aggregations