/** * Provides a token generator which just returns a fixed token, so the client is usable * for the live time of this token. * * @param token the token which will be passed to the client * @return token service */ public SphereAccessTokenSupplier createSupplierOfFixedToken(final String token) { return SphereAccessTokenSupplier.ofConstantToken(token); }
/** * Provides a token generator which just returns a fixed token, so the client is usable * for the live time of this token. * * @param token the token which will be passed to the client * @return token service */ public static SphereAccessTokenSupplier createSupplierOfFixedToken(final String token) { return SphereAccessTokenSupplier.ofConstantToken(token); }
/** * Creates a client which relies on an access token and does not refresh it, it reuses an existing http client. * * A possible use case is keeping the clientSecret secret and/or instantiate a client which is used for few requests for a limited time. * The underlying http client does not get closed by closing the client * * @param config the configuration to use the API * @param accessToken the token belonging to the project in {@code config} * @param httpClient the http client used for performing requests * @return client */ default SphereClient createClientOfApiConfigAndAccessToken(SphereApiConfig config, String accessToken, HttpClient httpClient) { final HttpClient uncloseableHttpClient = new HttpClient() {//httpClient should be closed from outside @Override public CompletionStage<HttpResponse> execute(final HttpRequest httpRequest) { return httpClient.execute(httpRequest); } @Override public void close() { } }; return SphereClient.of(config, uncloseableHttpClient, SphereAccessTokenSupplier.ofConstantToken(accessToken)); }
/** * Creates a client which relies on an access token and does not refresh it, it reuses an existing http client. * * A possible use case is keeping the clientSecret secret and/or instantiate a client which is used for few requests for a limited time. * The underlying http client does not get closed by closing the client * * @param config the configuration to use the API * @param accessToken the token belonging to the project in {@code config} * @param httpClient the http client used for performing requests * @return client */ public SphereClient createClientOfApiConfigAndAccessToken(final SphereApiConfig config, final String accessToken, final HttpClient httpClient) { final HttpClient uncloseableHttpClient = new HttpClient() { @Override public CompletionStage<HttpResponse> execute(final HttpRequest httpRequest) { return httpClient.execute(httpRequest); } @Override public void close() { } }; return SphereClient.of(config, uncloseableHttpClient, SphereAccessTokenSupplier.ofConstantToken(accessToken)); }
/** * Creates a client which relies on an access token and does not refresh it, it reuses an existing http client. * * A possible use case is keeping the clientSecret secret and/or instantiate a client which is used for few requests for a limited time. * The underlying http client does not get closed by closing the client * * @param config the configuration to use the API * @param accessToken the token belonging to the project in {@code config} * @param httpClient the http client used for performing requests * @return client */ default SphereClient createClientOfApiConfigAndAccessToken(SphereApiConfig config, String accessToken, HttpClient httpClient) { final HttpClient uncloseableHttpClient = new HttpClient() {//httpClient should be closed from outside @Override public CompletionStage<HttpResponse> execute(final HttpRequest httpRequest) { return httpClient.execute(httpRequest); } @Override public void close() { } }; return SphereClient.of(config, uncloseableHttpClient, 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 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)); }
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")); }
private SphereClient getSphereClient(final HttpClient httpClient) { return SphereClient.of(SphereApiConfig.of("projectKey"), httpClient, SphereAccessTokenSupplier.ofConstantToken("accessToken")); }
private void scopedTokenBody() { withApiClient(client(), asList(SphereProjectScope.VIEW_CUSTOMERS,SphereProjectScope.VIEW_ORDERS), apiClient -> { final SphereAuthConfig config = toSphereAuthConfig(getSphereClientConfig(),apiClient); assertThat(config.getScopes()).containsExactly("view_customers", "view_orders"); final CompletionStage<String> stringCompletionStage = TokensFacade.fetchAccessToken(config); final String accessToken = blockingWait(stringCompletionStage, 2, SECONDS); assertThat(accessToken).isNotEmpty(); try (final SphereClient client = SphereClientFactory.of() .createClient(getSphereClientConfig(), SphereAccessTokenSupplier.ofConstantToken(accessToken))) { final PagedQueryResult<Customer> customerPagedQueryResult = blockingWait(client.execute(CustomerQuery.of().withLimit(1)), 2, SECONDS); assertThat(customerPagedQueryResult).isNotNull(); } }); }
private void passwordFlowDemo() { withApiClient(client(), singletonList(SphereProjectScope.VIEW_PRODUCTS), apiClient -> { withCustomer(client(), (Customer customer) -> { final SphereAuthConfig authConfig = toSphereAuthConfig(getSphereClientConfig(),apiClient); final String email = customer.getEmail(); // final String pw = customer.getPassword();//won;t work since it is obfusciated final String pw = CustomerFixtures.PASSWORD; final Tokens tokens = blockingWait(TokensFacade. fetchCustomerPasswordFlowTokens(authConfig, email, pw), 2, SECONDS); final String accessToken = tokens.getAccessToken(); assertThat(accessToken).isNotEmpty(); try (final SphereClient client = SphereClientFactory.of() .createClient(getSphereClientConfig(), SphereAccessTokenSupplier.ofConstantToken(accessToken))) { final PagedQueryResult<ProductProjection> customerPagedQueryResult = blockingWait(client.execute(ProductProjectionQuery.ofCurrent().withLimit(1)), 2, SECONDS); assertThat(customerPagedQueryResult).isNotNull(); } }); }); } }
@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())); }