@Override public <T> CompletionStage<T> execute(final SphereRequest<T> sphereRequest) { return delegate.execute(sphereRequest); } }
/** * Creates a client with a custom service to provide access tokens. * @param config the configuration to use the API * @param tokenSupplier a service which provides tokens * @return client */ default SphereClient createClient(SphereApiConfig config, SphereAccessTokenSupplier tokenSupplier) { return SphereClient.of(config, createHttpClient(), tokenSupplier); }
@Test public void itWorks() throws Exception { //we cannot check this in TeamCity, this test is rather reserved for Travis ci if (!"false".equals(System.getenv("JVM_SDK_IT_SSL_VALIDATION"))) { final SphereClientConfig config = getSphereClientConfig(); final HttpClient httpClient = ApacheHttpClientAdapter.of(HttpAsyncClients.createDefault()); final SphereAccessTokenSupplier supplier = SphereAccessTokenSupplier.ofOneTimeFetchingToken(config, httpClient, false); final SphereClient client = SphereClient.of(config, httpClient, supplier); final Project project = client.execute(ProjectGet.of()).toCompletableFuture().join(); assertThat(project.getKey()).isEqualTo(getSphereClientConfig().getProjectKey()); } }
@Test public void autoLoadHttpClientWithoutExplicitSpecification() { if (!"false".equals(System.getenv("JVM_SDK_IT_SSL_VALIDATION"))) { final SphereClientFactory sphereClientFactory = SphereClientFactory.of(); final SphereClient client = sphereClientFactory.createClient(getSphereClientConfig()); final PagedQueryResult<Category> queryResult = client.execute(CategoryQuery.of()).toCompletableFuture().join(); assertThat(queryResult).isNotNull(); client.close(); } }
@Test public void requestsArePossible() throws Exception { withCategory(client(), category -> { final CategoryQuery categoryQuery = CategoryQuery.of(); final SphereApiConfig apiConfig = SphereApiConfig.of(getSphereClientConfig().getProjectKey(), getSphereClientConfig().getApiUrl()); final SphereAuthConfig authConfig = SphereAuthConfig.of(getSphereClientConfig().getProjectKey(), getSphereClientConfig().getClientId(), getSphereClientConfig().getClientSecret(), getSphereClientConfig().getAuthUrl()); final SphereAccessTokenSupplier fixedTokenSupplier = SphereAccessTokenSupplier.ofOneTimeFetchingToken(authConfig, newHttpClient(), true); final SphereClient oneTokenClient = SphereClient.of(apiConfig, newHttpClient(), fixedTokenSupplier); final Long actual = oneTokenClient.execute(categoryQuery).toCompletableFuture().join().getTotal(); assertThat(actual).isGreaterThanOrEqualTo(1); oneTokenClient.close(); }); } }
@Override public void close() { delegate.close(); }
@Override public SphereApiConfig getConfig() { return delegate.getConfig(); } }
@Test public void retryOnInvalidToken() throws Exception { final RetryInvalidTokenHttpClient httpClient = new RetryInvalidTokenHttpClient(getSphereClientConfig()); final SphereAccessTokenSupplier tokenSupplier = SphereAccessTokenSupplier.ofAutoRefresh(getSphereClientConfig(), httpClient, false); try(final SphereClient client = SphereClient.of(getSphereClientConfig(), httpClient, tokenSupplier)) { assertThat(httpClient.isTokenValid()).isTrue(); assertThat(client.execute(CategoryByIdGet.of("cat-id")).toCompletableFuture().join()).isNull(); //here is the point where the token expires assertThat(httpClient.isTokenValid()).isFalse(); assertThat(client.execute(ChannelByIdGet.of("channel-id")).toCompletableFuture().join()).isNull(); assertThat(httpClient.isTokenValid()).isTrue(); } }
@Test public void clientAlreadyClosed() throws Exception { if (!"false".equals(System.getenv("JVM_SDK_IT_SSL_VALIDATION"))) { try (final SphereClient client = SphereClientFactory.of().createClient(getSphereClientConfig())) { client.close(); assertThatThrownBy(() -> client.execute(ProjectGet.of())) .isInstanceOf(IllegalStateException.class); } } }
@Override public void close() { delegate.close(); }
@Override public SphereApiConfig getConfig() { return delegate.getConfig(); } }
@Override public <T> CompletableFuture<T> execute(final SphereRequest<T> sphereRequest) { return delegate.execute(sphereRequest); } }
final SphereClient sphereClient = SphereClient.of(sphereApiConfig, httpClient, sphereAccessTokenSupplier); final DummySphereRequest sphereRequest = DummySphereRequest.of(); final CompletableFuture<String> completionStage = (CompletableFuture<String>) sphereClient.execute(sphereRequest); final String response = completionStage.get(); assertThat(response).isEqualTo(DummySphereRequest.DEFAULT_RESPONSE_OBJECT);
@Override public SphereClient get() { final MetricHttpClient metricHttpClient = MetricHttpClient.of(httpClient, context); logger.info("Provide RequestScopedSphereClient: MetricHttpClient"); return SphereClient.of(sphereClientConfig, metricHttpClient, sphereAccessTokenSupplier); }
/** * This is a test if no threads are blocking the the termination of the application. Needs to be executed on the ci server. * In case it has a leak, it just hangs. * This is necessary, since a unit test cannot cover this termination problem. * @param args unused command line parameters */ public static void main(String[] args) throws InterruptedException, ExecutionException, TimeoutException { final SphereClient client = SphereClientFactory.of() .createClient(SphereClientConfig.ofEnvironmentVariables("JVM_SDK_IT")); final CompletionStage<PagedQueryResult<ProductProjection>> stage = client.execute(ProductProjectionQuery.ofStaged()); stage.toCompletableFuture().get(10, TimeUnit.SECONDS); client.close(); } }
@Override public void close() { delegate.close(); }
@Override public SphereApiConfig getConfig() { return this.delegate.getConfig(); } }
@Override public <T> CompletionStage<T> execute(final SphereRequest<T> sphereRequest) { return delegate.execute(sphereRequest); }
@Test public void apiRequestWithWrongToken() throws Throwable { client(); final SphereApiConfig config = SphereApiConfig.of(getSphereClientConfig().getProjectKey(), getSphereClientConfig().getApiUrl()); final SphereClient client = SphereClient.of(config, newHttpClient(),SphereAccessTokenSupplier.ofConstantToken("invalid-token")); expectExceptionAndClose(client, InvalidTokenException.class, client.execute(CategoryQuery.of())); }