/** * 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 */ default SphereClient createClient(SphereApiConfig config, 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 */ default SphereClient createClient(SphereApiConfig config, SphereAccessTokenSupplier tokenSupplier) { return SphereClient.of(config, createHttpClient(), 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 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 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 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); }
@Override protected void configure() { applyJavaMoneyHack(); bindScope(RequestScoped.class, new RequestScope()); bind(HttpClient.class).toInstance(SphereClientFactory.of().createHttpClient()); bind(SphereClientConfig.class).toProvider(SphereClientConfigProvider.class).in(Singleton.class); bind(SphereAccessTokenSupplier.class).toProvider(SphereAccessTokenSupplierProvider.class).in(Singleton.class); bind(SphereClient.class).annotatedWith(Names.named("global")).toProvider(SphereClientProvider.class).in(Singleton.class); bind(ProductDataConfig.class).toProvider(ProductDataConfigProvider.class).in(Singleton.class); bind(RequestContext.class).toProvider(RequestContextProvider.class).in(RequestScoped.class); bind(RequestHookContext.class).to(RequestHookContextImpl.class).in(RequestScoped.class); bind(MultiControllerComponentResolver.class) .annotatedWith(Names.named("controllers")) .toProvider(RoutesMultiControllerComponentResolverProvider.class) .in(Singleton.class); }
@Override public HttpRequestResult sendHttpGetRequest(String url) { HttpRequest request = HttpRequest.of(HttpMethod.GET, url); try(HttpClient client = SphereClientFactory.of().createHttpClient()) { HttpResponse response = client.execute(request).toCompletableFuture().get(10000, TimeUnit.MILLISECONDS); return HttpRequestResult.of(request, response, null); } catch (InterruptedException | ExecutionException | TimeoutException e) { return HttpRequestResult.of(request, null, e); } } }
/** * Fetches a new access token using the client credentials flow. * * {@include.example io.sphere.sdk.client.TokensFacadeIntegrationTest#fetchAccessToken()} * * @param authConfig the commercetools project which the token should belong to * @return token */ public static CompletionStage<Tokens> fetchTokens(final SphereAuthConfig authConfig) { return TokensSupplier.of(authConfig, SphereClientFactory.of().createHttpClient(), true).get(); }
/** * Fetches a new access token using the client credentials flow. * * {@include.example io.sphere.sdk.client.TokensFacadeIntegrationTest#fetchAccessToken()} * * @param authConfig the commercetools project which the token should belong to * @return token */ public static CompletionStage<Tokens> fetchTokens(final SphereAuthConfig authConfig) { return TokensSupplier.of(authConfig, SphereClientFactory.of().createHttpClient(), true).get(); }
/** * Fetches a new access token using the customer password flow. * * {@include.example io.sphere.sdk.client.TokensFacadeIntegrationTest#passwordFlowDemo()} * * @param authConfig authConfig * @param email email * @param password password * @return token */ public static CompletionStage<Tokens> fetchCustomerPasswordFlowTokens(final SphereAuthConfig authConfig, final String email, final String password) { return TokensSupplier.ofCustomerPasswordFlowTokens(authConfig, email, password, SphereClientFactory.of().createHttpClient(), true).get(); } }
/** * Fetches a new access token using the customer password flow. * * {@include.example io.sphere.sdk.client.TokensFacadeIntegrationTest#passwordFlowDemo()} * * @param authConfig authConfig * @param email email * @param password password * @return token */ public static CompletionStage<Tokens> fetchCustomerPasswordFlowTokens(final SphereAuthConfig authConfig, final String email, final String password) { return TokensSupplier.ofCustomerPasswordFlowTokens(authConfig, email, password, SphereClientFactory.of().createHttpClient(), true).get(); } }
@Override public CompletionStage<Payment> requestHandling(String paymentId) { try (final HttpClient httpClient = SphereClientFactory.of().createHttpClient()) { return httpClient.execute(HttpRequest.of(HttpMethod.GET, buildUrl(paymentId))) .thenCompose(response -> { if(response.getStatusCode().equals(HttpStatusCode.OK_200)) { return sphereClient.execute(PaymentByIdGet.of(paymentId)); } return null; }); } }
@Override @Nonnull public CompletionStage<HttpRequestResult> sendHttpGetRequest(String url) { HttpRequest request = HttpRequest.of(HttpMethod.GET, url); HttpClient client = SphereClientFactory.of().createHttpClient(); return client.execute(request) .thenApplyAsync(response -> HttpRequestResult.of(request, response, null)) .exceptionally(throwable -> HttpRequestResult.of(request, null, throwable)) .whenCompleteAsync((response, throwable) -> client.close()); } }