use of io.crnk.client.TransportException in project crnk-framework by crnk-project.
the class ClientStubBase method execute.
protected Object execute(String url, ResponseType responseType, HttpMethod method, String requestBody) {
try {
HttpAdapter httpAdapter = client.getHttpAdapter();
HttpAdapterRequest request = httpAdapter.newRequest(url, method, requestBody);
LOGGER.debug("requesting {} {}", method, url);
if (requestBody != null) {
LOGGER.debug("request body: {}", requestBody);
}
if (method == HttpMethod.POST || method == HttpMethod.PATCH) {
request.header("Content-Type", HttpHeaders.JSONAPI_CONTENT_TYPE + "; charset=" + HttpHeaders.DEFAULT_CHARSET);
}
request.header("Accept", HttpHeaders.JSONAPI_CONTENT_TYPE);
HttpAdapterResponse response = request.execute();
if (!response.isSuccessful()) {
throw handleError(response);
}
String body = response.body();
LOGGER.debug("response body: {}", body);
ObjectMapper objectMapper = client.getObjectMapper();
if (responseType != ResponseType.NONE && Resource.class.equals(resourceClass)) {
Document document = objectMapper.readValue(body, Document.class);
return toResourceResponse(document, objectMapper);
} else if (responseType != ResponseType.NONE) {
Document document = objectMapper.readValue(body, Document.class);
ClientDocumentMapper documentMapper = client.getDocumentMapper();
return documentMapper.fromDocument(document, responseType == ResponseType.RESOURCES);
}
return null;
} catch (IOException e) {
throw new TransportException(e);
}
}
Aggregations