Search in sources :

Example 1 with HttpAdapterResponse

use of io.crnk.client.http.HttpAdapterResponse in project crnk-framework by crnk-project.

the class OkHttpRequest method execute.

@Override
public HttpAdapterResponse execute() throws IOException {
    Request request = builder.build();
    Response response = client.newCall(request).execute();
    return new OkHttpResponse(response);
}
Also used : HttpAdapterResponse(io.crnk.client.http.HttpAdapterResponse) Response(okhttp3.Response) Request(okhttp3.Request) HttpAdapterRequest(io.crnk.client.http.HttpAdapterRequest)

Example 2 with HttpAdapterResponse

use of io.crnk.client.http.HttpAdapterResponse in project crnk-framework by crnk-project.

the class OperationsCall method execute.

public void execute() {
    List<Operation> operations = new ArrayList<>();
    for (QueuedOperation queuedOperation : queuedOperations) {
        operations.add(queuedOperation.operation);
    }
    HttpAdapter adapter = client.getCrnk().getHttpAdapter();
    ObjectMapper mapper = client.getCrnk().getObjectMapper();
    try {
        String operationsJson = mapper.writer().writeValueAsString(operations.toArray(new Operation[operations.size()]));
        String url = client.getCrnk().getServiceUrlProvider().getUrl() + "/operations";
        HttpAdapterRequest request = adapter.newRequest(url, HttpMethod.PATCH, operationsJson);
        request.header(HttpHeaders.HTTP_CONTENT_TYPE, OperationsRequestProcessor.JSONPATCH_CONTENT_TYPE);
        request.header(HttpHeaders.HTTP_HEADER_ACCEPT, OperationsRequestProcessor.JSONPATCH_CONTENT_TYPE);
        HttpAdapterResponse response = request.execute();
        int status = response.code();
        if (status != 200) {
            // general issue, status of individual operations is important.
            throw new InternalServerErrorException("patch execution failed with status " + status);
        }
        String json = response.body();
        responses = Arrays.asList(mapper.readValue(json, OperationResponse[].class));
    } catch (IOException e) {
        throw new IllegalStateException(e);
    }
}
Also used : HttpAdapterRequest(io.crnk.client.http.HttpAdapterRequest) HttpAdapter(io.crnk.client.http.HttpAdapter) HttpAdapterResponse(io.crnk.client.http.HttpAdapterResponse) ArrayList(java.util.ArrayList) Operation(io.crnk.operations.Operation) IOException(java.io.IOException) InternalServerErrorException(io.crnk.core.exception.InternalServerErrorException) OperationResponse(io.crnk.operations.OperationResponse) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 3 with HttpAdapterResponse

use of io.crnk.client.http.HttpAdapterResponse in project crnk-framework by crnk-project.

the class ClientStubBaseTest method checkCheckedException.

@Test
public void checkCheckedException() throws IOException {
    HttpAdapterResponse response = Mockito.mock(HttpAdapterResponse.class);
    Mockito.when(response.body()).thenReturn("");
    Mockito.when(response.code()).thenReturn(599);
    RuntimeException exception = stub.handleError(response);
    Assert.assertTrue(exception instanceof ClientException);
    Assert.assertTrue(exception.getCause() instanceof IOException);
}
Also used : HttpAdapterResponse(io.crnk.client.http.HttpAdapterResponse) ClientException(io.crnk.client.ClientException) IOException(java.io.IOException) Test(org.junit.Test)

Example 4 with HttpAdapterResponse

use of io.crnk.client.http.HttpAdapterResponse in project crnk-framework by crnk-project.

the class ClientStubBaseTest method checkBodyWithErrors.

@Test
public void checkBodyWithErrors() throws IOException {
    Document document = new Document();
    ErrorData errorData = new ErrorDataBuilder().setCode("404").setDetail("detail").build();
    document.setErrors(Arrays.asList(errorData));
    String body = client.getObjectMapper().writeValueAsString(document);
    HttpAdapterResponse response = Mockito.mock(HttpAdapterResponse.class);
    Mockito.when(response.body()).thenReturn(body);
    Mockito.when(response.getResponseHeader(HttpHeaders.HTTP_CONTENT_TYPE)).thenReturn(HttpHeaders.JSONAPI_CONTENT_TYPE);
    Mockito.when(response.code()).thenReturn(404);
    RuntimeException exception = stub.handleError(response);
    Assert.assertTrue(exception instanceof ResourceNotFoundException);
    Assert.assertEquals("detail", exception.getMessage());
}
Also used : ErrorDataBuilder(io.crnk.core.engine.document.ErrorDataBuilder) HttpAdapterResponse(io.crnk.client.http.HttpAdapterResponse) Document(io.crnk.core.engine.document.Document) ResourceNotFoundException(io.crnk.core.exception.ResourceNotFoundException) ErrorData(io.crnk.core.engine.document.ErrorData) Test(org.junit.Test)

Example 5 with HttpAdapterResponse

use of io.crnk.client.http.HttpAdapterResponse in project crnk-framework by crnk-project.

the class ClientStubBaseTest method checkBodyWithErrorsButInvalidContentType.

@Test
public void checkBodyWithErrorsButInvalidContentType() throws IOException {
    Document document = new Document();
    ErrorData errorData = new ErrorDataBuilder().setCode("404").setDetail("detail").build();
    document.setErrors(Arrays.asList(errorData));
    String body = client.getObjectMapper().writeValueAsString(document);
    HttpAdapterResponse response = Mockito.mock(HttpAdapterResponse.class);
    Mockito.when(response.body()).thenReturn(body);
    Mockito.when(response.getResponseHeader(HttpHeaders.HTTP_CONTENT_TYPE)).thenReturn("not json api");
    Mockito.when(response.code()).thenReturn(404);
    RuntimeException exception = stub.handleError(response);
    Assert.assertTrue(exception instanceof ResourceNotFoundException);
    Assert.assertNull(exception.getMessage());
}
Also used : ErrorDataBuilder(io.crnk.core.engine.document.ErrorDataBuilder) HttpAdapterResponse(io.crnk.client.http.HttpAdapterResponse) Document(io.crnk.core.engine.document.Document) ResourceNotFoundException(io.crnk.core.exception.ResourceNotFoundException) ErrorData(io.crnk.core.engine.document.ErrorData) Test(org.junit.Test)

Aggregations

HttpAdapterResponse (io.crnk.client.http.HttpAdapterResponse)9 Test (org.junit.Test)6 Document (io.crnk.core.engine.document.Document)4 HttpAdapterRequest (io.crnk.client.http.HttpAdapterRequest)3 ErrorData (io.crnk.core.engine.document.ErrorData)3 InternalServerErrorException (io.crnk.core.exception.InternalServerErrorException)3 ResourceNotFoundException (io.crnk.core.exception.ResourceNotFoundException)3 IOException (java.io.IOException)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 HttpAdapter (io.crnk.client.http.HttpAdapter)2 ErrorDataBuilder (io.crnk.core.engine.document.ErrorDataBuilder)2 ClientException (io.crnk.client.ClientException)1 TransportException (io.crnk.client.TransportException)1 Resource (io.crnk.core.engine.document.Resource)1 Operation (io.crnk.operations.Operation)1 OperationResponse (io.crnk.operations.OperationResponse)1 ArrayList (java.util.ArrayList)1 Request (okhttp3.Request)1 Response (okhttp3.Response)1