private Object typeResponse(Object responseBody) { if (typedResponse() == null || responseBody instanceof InputStream) return responseBody; return gson.fromJson((String) responseBody, typedResponse()); }
/** * Invoke the HTTP REST call. will not return a response */ public void withNoResult() { withResult(); }
private T doWork() { try { setupErrorHandlers(); WorkingContext workingContext = workingContext(); HttpRequest builtRequest = buildUnirest(workingContext) .queryString(workingContext.getQueryParams()) .headers(workingContext.getHeaders()).header("Ocp-Apim-Subscription-Key", cognitiveContext.subscriptionKey); if (!workingContext.getHttpMethod().equals(HttpMethod.GET) && workingContext().getPayload().size() > 0) { buildBody((HttpRequestWithBody) builtRequest); } HttpResponse response; if (typedResponse() == InputStream.class) response = builtRequest.asBinary(); else response = builtRequest.asString(); checkForError(response); return postProcess(typeResponse(response.getBody())); } catch (UnirestException | IOException e) { throw new CognitiveException(e); } }
private void buildBody(HttpRequestWithBody builtRequest) throws IOException { if (workingContext().getPayload().containsKey(IMAGE_INPUT_STREAM_KEY)) { builtRequest.header("content-type", "application/octet-stream").body(IOUtils.toByteArray((InputStream) workingContext().getPayload().get(IMAGE_INPUT_STREAM_KEY))); } else { builtRequest.header("content-type", "application/json").body(new JSONObject(workingContext().getPayload())); } }
private void setupErrorHandlers() { errorHandlers.put(HttpStatus.SC_UNAUTHORIZED, new UnAuthorizedErrorHandler()); errorHandlers.put(HttpStatus.SC_FORBIDDEN, new ForbiddenErrorHandler()); errorHandlers.put(429, new RateLimitExceededErrorHandler()); errorHandlers.put(409, new ConcurrentOperationConflictErrorHandler()); customErrorHandlers(errorHandlers); }