/** * Shows the initialization of a {@link SphereClient} with a custom {@link HttpClient} provider. */ private void demoCreateClient() { final SphereClient sphereClient = SphereClientFactory.of(CustomClientConfigDemoIntegrationTest::createCustomHttpClient) .createClient("your projectKey", "your clientId", "your clientSecret"); }
/** * 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(); }
public static SphereClientFactory of() { try { Class<?> clazz; try { clazz = Class.forName("io.sphere.sdk.client.SphereAsyncHttpClientFactory"); } catch (final ClassNotFoundException e) { clazz = Class.forName("io.sphere.sdk.client.SphereApacheHttpClientFactory"); } final Method create = clazz.getMethod("create"); final Supplier<HttpClient> supplier = () -> { try { return (HttpClient) create.invoke(null); } catch (IllegalAccessException | InvocationTargetException e) { throw new SphereException(e); } }; return of(supplier); } catch (ClassNotFoundException | NoSuchMethodException e) { throw new SphereException(e); } }
@Test public void useToken() { if (!"false".equals(System.getenv("JVM_SDK_IT_SSL_VALIDATION"))) { final SphereClientFactory sphereClientFactory = SphereClientFactory.of(); final SphereApiConfig sphereClientConfig = getSphereClientConfig(); try(final SphereClient client = sphereClientFactory.createClientOfApiConfigAndAccessToken(sphereClientConfig, accessToken())){ final Project project = client.execute(ProjectGet.of()).toCompletableFuture().join(); assertThat(project.getKey()).isEqualTo(getSphereClientConfig().getProjectKey()); } } }
/** * 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 standard client suitable for online shops. * * For the credentials consult <a href="https://admin.sphere.io">the Merchant Center</a>. * @param projectKey the project identifier * @param clientId username * @param clientSecret password * @return sphere client */ public SphereClient createClient(final String projectKey, final String clientId, final String clientSecret) { return createClient(SphereClientConfig.of(projectKey, clientId, clientSecret)); }
static SphereClientFactory of() { ServiceLoader<SphereHttpClientFactory> loader = ServiceLoader.load(SphereHttpClientFactory.class,SphereClientFactory.class.getClassLoader()); SphereHttpClientFactory httpClientFactory = loader.iterator().next(); if(httpClientFactory == null){ throw new SphereException(new NoClassDefFoundError(SphereHttpClientFactory.class.getCanonicalName())); } return of(httpClientFactory::getClient); } }
public static SphereClientFactory of() { return new SphereClientFactory(); }
@Test public void useTokenAndReuseClient() { if (!"false".equals(System.getenv("JVM_SDK_IT_SSL_VALIDATION"))) { final SphereClientFactory sphereClientFactory = SphereClientFactory.of(); final SphereApiConfig sphereClientConfig = getSphereClientConfig(); final StateHttpClient httpClient = new StateHttpClient(newHttpClient()); try(final SphereClient client = sphereClientFactory.createClientOfApiConfigAndAccessToken(sphereClientConfig, accessToken(), httpClient)){ final Project project = client.execute(ProjectGet.of()).toCompletableFuture().join(); assertThat(project.getKey()).isEqualTo(getSphereClientConfig().getProjectKey()); assertThat(httpClient.isClosed()).isFalse(); } assertThat(httpClient.isClosed()).isFalse(); httpClient.close(); assertThat(httpClient.isClosed()).isTrue(); } }
/** * 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 suitable for online shops. * * For the credentials consult <a href="https://admin.sphere.io">the Merchant Center</a>. * @param projectKey the project identifier * @param clientId username * @param clientSecret password * @return sphere client */ public SphereClient createClient(final String projectKey, final String clientId, final String clientSecret) { return createClient(SphereClientConfig.of(projectKey, clientId, clientSecret)); }
static SphereClientFactory of() { ServiceLoader<SphereHttpClientFactory> loader = ServiceLoader.load(SphereHttpClientFactory.class,SphereClientFactory.class.getClassLoader()); SphereHttpClientFactory httpClientFactory = loader.iterator().next(); if(httpClientFactory == null){ throw new SphereException(new NoClassDefFoundError(SphereHttpClientFactory.class.getCanonicalName())); } return of(httpClientFactory::getClient); } }
public void instantiate() { final SphereClientFactory factory = SphereClientFactory.of(); final SphereClient client = factory.createClient( "jvm-sdk-dev-1", //replace with your project key "ELqF0rykXD2fyS8s-IhIPKfQ", //replace with your client id "222222222222222222222222222222226"); //replace with your client secret } }
/** * 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(); }
/** * 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 suitable for online shops. * * For the credentials consult <a href="https://admin.sphere.io">the Merchant Center</a>. * @param projectKey the project identifier * @param clientId username * @param clientSecret password * @return sphere client */ default SphereClient createClient(String projectKey, String clientId, String clientSecret) { return createClient(SphereClientConfig.of(projectKey, clientId, clientSecret)); }
private BlockingSphereClient createSphereClient() { final SphereClientFactory factory = SphereClientFactory.of(); final SphereClient asyncSphereClient = factory.createClient( "jvm-sdk-dev-1", //replace with your project key "ELqF0rykXD2fyS8s-IhIPKfQ", //replace with your client id "222222222222222222222222222222226"); //replace with your client secret return BlockingSphereClient.of(asyncSphereClient, 20, TimeUnit.SECONDS); } }
/** * 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(); } }
/** * 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 suitable for online shops. * * For the credentials consult <a href="https://admin.sphere.io">the Merchant Center</a>. * @param projectKey the project identifier * @param clientId username * @param clientSecret password * @return sphere client */ default SphereClient createClient(String projectKey, String clientId, String clientSecret) { return createClient(SphereClientConfig.of(projectKey, clientId, clientSecret)); }