/** * 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); }
@Override public SphereClient get() { final MetricHttpClient metricHttpClient = MetricHttpClient.of(httpClient, context); logger.info("Provide RequestScopedSphereClient: MetricHttpClient"); return SphereClient.of(sphereClientConfig, metricHttpClient, sphereAccessTokenSupplier); }
/** * 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 */ public SphereClient createClient(final SphereApiConfig config, final SphereAccessTokenSupplier tokenSupplier) { return SphereClient.of(config, createHttpClient(), tokenSupplier); }
/** * 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 */ public SphereClient createClient(final SphereApiConfig config, final SphereAccessTokenSupplier tokenSupplier) { return SphereClient.of(config, defaultHttpClient(), tokenSupplier); }
/** * 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); }
@Override public SphereClient get() { SphereClient sphereClient = SphereClient.of(sphereClientConfig, httpClient, sphereAccessTokenSupplier); logger.info("Provide SphereClient"); applicationLifecycle.addStopHook(() -> { sphereClient.close(); return completedFuture(null); }); return sphereClient; }
/** * Creates a client which relies on an access token and does not refresh it. * * A possible use case is keeping the clientSecret secret and/or instantiate a client which is used for few requests for a limited time. * * @param config the configuration to use the API * @param accessToken the token belonging to the project in {@code config} * @return client */ default SphereClient createClientOfApiConfigAndAccessToken(SphereApiConfig config, String accessToken) { return SphereClient.of(config, createHttpClient(), SphereAccessTokenSupplier.ofConstantToken(accessToken)); }
/** * Creates a standard client with configurable service URLs. Intended for commercetools staff * developing with a custom platform instance. * * @param config configuration for the client * @return client */ default SphereClient createClient(SphereClientConfig config) { final HttpClient httpClient = createHttpClient(); final SphereAccessTokenSupplier tokenSupplier = SphereAccessTokenSupplier.ofAutoRefresh(config, httpClient, false); return SphereClient.of(config, httpClient, tokenSupplier); }
/** * Creates a client which relies on an access token and does not refresh it. * * A possible use case is keeping the clientSecret secret and/or instantiate a client which is used for few requests for a limited time. * * @param config the configuration to use the API * @param accessToken the token belonging to the project in {@code config} * @return client */ default SphereClient createClientOfApiConfigAndAccessToken(SphereApiConfig config, String accessToken) { return SphereClient.of(config, createHttpClient(), SphereAccessTokenSupplier.ofConstantToken(accessToken)); }
/** * Creates a client which relies on an access token and does not refresh it. * * A possible use case is keeping the clientSecret secret and/or instantiate a client which is used for few requests for a limited time. * * @param config the configuration to use the API * @param accessToken the token belonging to the project in {@code config} * @return client */ public SphereClient createClientOfApiConfigAndAccessToken(final SphereApiConfig config, final String accessToken) { return SphereClient.of(config, createHttpClient(), SphereAccessTokenSupplier.ofConstantToken(accessToken)); }
/** * Creates a standard client with configurable service URLs. Intended for commercetools staff * developing with a custom SPHERE.IO instance. * * @param config configuration for the client * @return client */ public SphereClient createClient(final SphereClientConfig config) { final HttpClient httpClient = createHttpClient(); final SphereAccessTokenSupplier tokenSupplier = SphereAccessTokenSupplier.ofAutoRefresh(config, httpClient, false); return SphereClient.of(config, httpClient, tokenSupplier); }
/** * Creates a standard client with configurable service URLs. Intended for commercetools staff * developing with a custom platform instance. * * @param config configuration for the client * @return client */ default SphereClient createClient(SphereClientConfig config) { final HttpClient httpClient = createHttpClient(); final SphereAccessTokenSupplier tokenSupplier = SphereAccessTokenSupplier.ofAutoRefresh(config, httpClient, false); return SphereClient.of(config, httpClient, tokenSupplier); }
/** * Creates a standard client with configurable service URLs. Intended for commercetools staff * developing with a custom SPHERE.IO instance. * * @param config configuration for the client * @return client */ public SphereClient createClient(final SphereClientConfig config) { final HttpClient httpClient = defaultHttpClient(); final SphereAccessTokenSupplier tokenSupplier = SphereAccessTokenSupplier.ofAutoRefresh(config, httpClient, false); return SphereClient.of(config, defaultHttpClient(), tokenSupplier); }
private SphereClient clientWithResponse(final HttpResponse response) { return SphereClient.of(SphereApiConfig.of("test"), new HttpClient() { @Override public CompletionStage<HttpResponse> execute(final HttpRequest httpRequest) { return CompletableFutureUtils.successful(response); } @Override public void close() { } }, SphereAccessTokenSupplier.ofConstantToken("foo")); } }
private SphereClient getSphereClient(final HttpClient httpClient) { return SphereClient.of(SphereApiConfig.of("projectKey"), httpClient, SphereAccessTokenSupplier.ofConstantToken("accessToken")); }
public static void setupClient() { if (client == null) { final SphereClientConfig config = getSphereClientConfig(); final HttpClient httpClient = newHttpClient(); final SphereAccessTokenSupplier tokenSupplier = SphereAccessTokenSupplier.ofAutoRefresh(config, httpClient, false); final SphereClient underlying = SphereClient.of(config, httpClient, tokenSupplier); final SphereClient underlying1 = withMaybeDeprecationWarnTool(underlying); client = BlockingSphereClient.of(underlying1, 20, TimeUnit.SECONDS); assertProjectSettingsAreFine(client); } }
public static void setupClient() { if (client == null) { final SphereClientConfig config = getSphereClientConfig(); final HttpClient httpClient = newHttpClient(); final SphereAccessTokenSupplier tokenSupplier = SphereAccessTokenSupplier.ofAutoRefresh(config, httpClient, false); final SphereClient underlying = SphereClient.of(config, httpClient, tokenSupplier); final SphereClient underlying1 = withMaybeDeprecationWarnTool(underlying); client = BlockingSphereClient.of(underlying1, 30, TimeUnit.SECONDS); assertProjectSettingsAreFine(client); } } //
@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 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(); }); } }
@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())); }