@Override public <T> CompletableFuture<T> execute(final ClientRequest<T> clientRequest) { final SphereInternalLogger logger = getLogger(clientRequest); logger.debug(() -> clientRequest); logger.trace(() -> { final Optional<String> requestBody = clientRequest.httpRequest().getBody(); return requestBody.map(body -> JsonUtils.prettyPrintJsonStringSecure(body)).orElse("no request body present"); }); return requestExecutor. execute(clientRequest). thenApply(preProcess(clientRequest, clientRequest.resultMapper())); }
@Override public T apply(final HttpResponse httpResponse) { final SphereInternalLogger logger = getLogger(httpResponse); logger.debug(() -> httpResponse.withoutRequest()); logger.trace(() -> httpResponse.getStatusCode() + "\n" + JsonUtils.prettyPrintJsonStringSecure(httpResponse.getResponseBody()) + "\n"); final int status = httpResponse.getStatusCode(); final String body = httpResponse.getResponseBody(); final boolean hasError = status / 100 != 2; if (hasError) { SphereErrorResponse errorResponse; try { if (Strings.isNullOrEmpty(body)) {//the /model/id endpoint does not return JSON on 404 errorResponse = new SphereErrorResponse(status, "<no body>", Collections.<SphereError>emptyList()); } else { errorResponse = objectMapper.readValue(body, errorResponseJsonTypeRef); } } catch (final Exception e) { // This can only happen when the backend and SDK don't match. final SphereException exception = new SphereException("Can't parse backend response", e); fillExceptionWithData(httpResponse, exception, clientRequest); throw exception; } final SphereBackendException exception = new SphereBackendException(clientRequest.httpRequest().getPath(), errorResponse); fillExceptionWithData(httpResponse, exception, clientRequest); throw exception; } else { return underlying.apply(httpResponse); } } };