static SphereApiConfig of(final String projectKey) { return of(projectKey, API_URL); }
private static <T> void fillExceptionWithData(final SphereRequest<T> sphereRequest, final HttpResponse httpResponse, final SphereException exception, final SphereApiConfig config) { exception.setSphereRequest(sphereRequest); exception.setUnderlyingHttpResponse(httpResponse); exception.setProjectKey(config.getProjectKey()); final List<String> errorMessagesDueToMappingError = asList("SearchPhaseExecutionException", "query_fetch", "RemoteTransportException", "SearchParseException", "search/phase/query+fetch"); Optional.ofNullable(httpResponse.getResponseBody()) .map(bytes -> bytesToString(bytes)) .map(body -> errorMessagesDueToMappingError.stream().anyMatch(errorWord -> body.contains(errorWord)) && body.toLowerCase().contains("product")) .ifPresent(containsTerm -> { if (containsTerm) { exception.addNote(format("Maybe it helps to reindex the products https://admin.sphere.io/%s/developers/danger but this may take a while.", config.getProjectKey())); } }); }
private <T> HttpRequest createHttpRequest(final SphereRequest<T> sphereRequest, final String token) { return sphereRequest .httpRequestIntent() .plusHeader("User-Agent", "SPHERE.IO JVM SDK " + BuildInfo.version()) .plusHeader(HttpHeaders.AUTHORIZATION, "Bearer " + token) .prefixPath("/" + config.getProjectKey()) .toHttpRequest(config.getApiUrl()); }
private <T> HttpRequest createHttpRequest(final SphereRequest<T> sphereRequest, final String token) { return sphereRequest .httpRequestIntent() .plusHeader(HttpHeaders.USER_AGENT, BuildInfo.userAgent()) .plusHeader(HttpHeaders.ACCEPT_ENCODING, "gzip") .plusHeader(HttpHeaders.AUTHORIZATION, "Bearer " + token) .prefixPath("/" + config.getProjectKey()) .toHttpRequest(config.getApiUrl()); }
static SphereApiConfig of(final String projectKey) { return of(projectKey, API_URL); }
private <T> HttpRequest createHttpRequest(final SphereRequest<T> sphereRequest, final String token) { final String correlationId = correlationIdGenerator.get(); return sphereRequest .httpRequestIntent() .plusHeader(HttpHeaders.X_CORRELATION_ID, correlationId) .plusHeader(HttpHeaders.USER_AGENT, userAgent) .plusHeader(HttpHeaders.ACCEPT_ENCODING, "gzip") .plusHeader(HttpHeaders.AUTHORIZATION, "Bearer " + token) .prefixPath("/" + config.getProjectKey()) .toHttpRequest(config.getApiUrl()); }
/** * Raw client creation. * See also SphereClientFactory. * * @param config platform project and location * @param httpClient client to execute requests * @param tokenSupplier delivery of access tokens * @return sphere client */ static SphereClient of(final SphereApiConfig config, final HttpClient httpClient, final SphereAccessTokenSupplier tokenSupplier) { return SphereClientImpl.of(config, httpClient, tokenSupplier, CorrelationIdGenerator.of(config.getProjectKey())); }
private <T> HttpRequest createHttpRequest(final SphereRequest<T> sphereRequest, final String token) { final String correlationId = correlationIdGenerator.get(); return sphereRequest .httpRequestIntent() .plusHeader(HttpHeaders.X_CORRELATION_ID, correlationId) .plusHeader(HttpHeaders.USER_AGENT, userAgent) .plusHeader(HttpHeaders.ACCEPT_ENCODING, "gzip") .plusHeader(HttpHeaders.AUTHORIZATION, "Bearer " + token) .prefixPath("/" + config.getProjectKey()) .toHttpRequest(config.getApiUrl()); }
public static SphereClient of(final SphereApiConfig config, final HttpClient httpClient, final SphereAccessTokenSupplier tokenSupplier) { return new SphereClientImpl(config, tokenSupplier, httpClient, CorrelationIdGenerator.of(config.getProjectKey())); }
@Override public <T> CompletionStage<T> execute(final SphereRequest<T> sphereRequest) { final HttpRequestIntent httpRequest = sphereRequest.httpRequestIntent(); final HttpResponse httpResponse = function.apply(httpRequest); try { final T t = SphereClientImpl.parse(sphereRequest, objectMapper, SphereApiConfig.of("fake-project-key-for-testing", "https://createHttpTestDouble.tld"), httpResponse); return CompletableFutureUtils.successful(t); } catch (final Exception e) { return CompletableFutureUtils.failed(e); } }
when(sphereApiConfig.getProjectKey()).thenReturn(projectKey); when(sphereApiConfig.getApiUrl()).thenReturn("http://api.commercetools.de"); when(httpClient.getUserAgent()).thenReturn("user-agent"); when(sphereAccessTokenSupplier.get()).thenReturn(CompletableFutureUtils.successful("token"));
/** * Raw client creation. * See also SphereClientFactory. * * @param config platform project and location * @param httpClient client to execute requests * @param tokenSupplier delivery of access tokens * @return sphere client */ static SphereClient of(final SphereApiConfig config, final HttpClient httpClient, final SphereAccessTokenSupplier tokenSupplier) { return SphereClientImpl.of(config, httpClient, tokenSupplier, CorrelationIdGenerator.of(config.getProjectKey())); }
@Override public <T> CompletableFuture<T> execute(final SphereRequest<T> sphereRequest) { final HttpRequestIntent httpRequest = sphereRequest.httpRequestIntent(); final HttpResponse httpResponse = function.apply(httpRequest); try { final T t = SphereClientImpl.parse(httpResponse, sphereRequest, objectMapper, SphereApiConfig.of("createHttpTestDouble", "https://createHttpTestDouble.tld")); return CompletableFutureUtils.successful(t); } catch (final Exception e) { return CompletableFutureUtils.failed(e); } }
public static SphereClient of(final SphereApiConfig config, final HttpClient httpClient, final SphereAccessTokenSupplier tokenSupplier) { return new SphereClientImpl(config, tokenSupplier, httpClient, CorrelationIdGenerator.of(config.getProjectKey())); }
@Test public void validateApiUrl() throws Exception { assertThatThrownBy(() -> SphereApiConfig.of("foo", null)) .isInstanceOf(IllegalArgumentException.class) .hasMessageContaining("apiUrl"); assertThatThrownBy(() -> SphereApiConfig.of("foo", "")) .isInstanceOf(IllegalArgumentException.class) .hasMessageContaining("apiUrl"); } }
private static <T> void fillExceptionWithData(final SphereRequest<T> sphereRequest, final HttpResponse httpResponse, final SphereException exception, final SphereApiConfig config) { exception.setSphereRequest(sphereRequest); exception.setUnderlyingHttpResponse(httpResponse); exception.setProjectKey(config.getProjectKey()); }
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 static <T> void fillExceptionWithData(final SphereRequest<T> sphereRequest, final HttpResponse httpResponse, final SphereException exception, final SphereApiConfig config, final HttpRequest httpRequest) { exception.setSphereRequest(sphereRequest); exception.setUnderlyingHttpResponse(httpResponse); exception.setProjectKey(config.getProjectKey()); exception.setHttpRequest(httpRequest); }
private SphereClient getSphereClient(final HttpClient httpClient) { return SphereClient.of(SphereApiConfig.of("projectKey"), httpClient, SphereAccessTokenSupplier.ofConstantToken("accessToken")); }