Search in sources :

Example 1 with HttpResponse

use of com.github.davidmoten.odata.client.HttpResponse in project odata-client by davidmoten.

the class RequestHelper method get.

/**
 * Returns the json from an HTTP GET of the url built from the contextPath and
 * options. In the case where the returned object is actually a sub-class of T
 * we lookup the sub-class from context schemaInfos based on the namespaced type
 * of the return object.
 *
 * @param <T>         return object type
 * @param contextPath context and current path
 * @param returnCls   return class
 * @param options     request options
 * @return object hydrated from json
 */
public static <T> T get(ContextPath contextPath, Class<T> returnCls, RequestOptions options) {
    // build the url
    ContextPath cp = contextPath.addQueries(options.getQueries());
    List<RequestHeader> h = cleanAndSupplementRequestHeaders(options, "minimal", false);
    // get the response
    HttpResponse response = cp.context().service().get(cp.toUrl(), h, options);
    checkResponseCode(cp, response, HttpURLConnection.HTTP_OK);
    // deserialize
    // Though cls might be Class<Attachment> we might actually want to return a
    // sub-class like FileAttachment (which extends Attachment). This method returns
    // the actual sub-class by inspecting the json response.
    Class<? extends T> c = getSubClass(cp, contextPath.context().schemas(), returnCls, response.getText());
    // FileAttachment which is a subclass of Attachment)
    return cp.context().serializer().deserialize(response.getText(), c, contextPath, false);
}
Also used : ContextPath(com.github.davidmoten.odata.client.ContextPath) RequestHeader(com.github.davidmoten.odata.client.RequestHeader) HttpResponse(com.github.davidmoten.odata.client.HttpResponse)

Example 2 with HttpResponse

use of com.github.davidmoten.odata.client.HttpResponse in project odata-client by davidmoten.

the class RequestHelper method delete.

public static <T extends ODataEntityType> void delete(ContextPath cp, RequestOptions options) {
    String url = cp.toUrl();
    List<RequestHeader> h = cleanAndSupplementRequestHeaders(options, "minimal", true);
    HttpResponse response = cp.context().service().delete(url, h, options);
    checkResponseCode(cp, response, HttpURLConnection.HTTP_NO_CONTENT);
}
Also used : RequestHeader(com.github.davidmoten.odata.client.RequestHeader) HttpResponse(com.github.davidmoten.odata.client.HttpResponse)

Example 3 with HttpResponse

use of com.github.davidmoten.odata.client.HttpResponse in project odata-client by davidmoten.

the class RequestHelper method post.

public static void post(Map<String, Object> parameters, ContextPath contextPath, RequestOptions options) {
    String json = Serializer.INSTANCE.serialize(parameters);
    // build the url
    ContextPath cp = contextPath.addQueries(options.getQueries());
    List<RequestHeader> h = cleanAndSupplementRequestHeaders(options, "minimal", true);
    final String url = cp.toUrl();
    // get the response
    HttpService service = cp.context().service();
    final HttpResponse response = service.post(url, h, json, options);
    checkResponseCodeOk(cp, response);
}
Also used : ContextPath(com.github.davidmoten.odata.client.ContextPath) HttpService(com.github.davidmoten.odata.client.HttpService) RequestHeader(com.github.davidmoten.odata.client.RequestHeader) HttpResponse(com.github.davidmoten.odata.client.HttpResponse)

Example 4 with HttpResponse

use of com.github.davidmoten.odata.client.HttpResponse in project odata-client by davidmoten.

the class RequestHelper method postAnyWithParametricType.

public static <T, S> T postAnyWithParametricType(Object object, ContextPath contextPath, Class<T> cls, Class<S> parametricTypeClass, RequestOptions options) {
    // build the url
    ContextPath cp = contextPath.addQueries(options.getQueries());
    String json = Serializer.INSTANCE.serialize(object);
    List<RequestHeader> h = cleanAndSupplementRequestHeaders(options, "minimal", true);
    // get the response
    HttpResponse response = cp.context().service().post(cp.toUrl(), h, json, options);
    checkResponseCode(cp, response, HttpURLConnection.HTTP_CREATED);
    String text = response.getText();
    // deserialize
    Class<? extends T> c = getSubClass(cp, contextPath.context().schemas(), cls, text);
    // FileAttachment which is a subclass of Attachment)
    return cp.context().serializer().deserializeWithParametricType(text, c, parametricTypeClass, contextPath, false);
}
Also used : ContextPath(com.github.davidmoten.odata.client.ContextPath) RequestHeader(com.github.davidmoten.odata.client.RequestHeader) HttpResponse(com.github.davidmoten.odata.client.HttpResponse)

Example 5 with HttpResponse

use of com.github.davidmoten.odata.client.HttpResponse in project odata-client by davidmoten.

the class GraphServiceTest method testDriveIssue173Put.

@Test
public void testDriveIssue173Put() {
    GraphService client = clientBuilder().expectRequest("/drives/123/items/456:/filename.txt:/content").withMethod(// 
    HttpMethod.PUT).withPayload(// 
    "/request-upload-bytes.txt").withRequestHeaders(RequestHeader.CONTENT_TYPE_TEXT_PLAIN).withResponseStatusCode(// 
    201).withResponse(// 
    "/response-drive-item-put.json").build();
    String url = "https://graph.microsoft.com/v1.0/drives/123/items/456:/filename.txt:/content";
    String content = "hello";
    {
        // use HttpService
        List<RequestHeader> headers = Collections.singletonList(RequestHeader.CONTENT_TYPE_TEXT_PLAIN);
        HttpResponse u = // 
        client._service().submit(HttpMethod.PUT, url, headers, content, HttpRequestOptions.EMPTY);
        assertTrue(u.getText().contains("0123456789abc"));
    }
    {
        // use CustomRequest (with bytes content this time)
        String json = // 
        client._custom().submitBytesReturnsString(HttpMethod.PUT, url, content.getBytes(StandardCharsets.UTF_8), HttpRequestOptions.EMPTY, RequestHeader.CONTENT_TYPE_TEXT_PLAIN);
        assertTrue(json.contains("0123456789abc"));
    }
}
Also used : GraphService(odata.msgraph.client.container.GraphService) HttpResponse(com.github.davidmoten.odata.client.HttpResponse) List(java.util.List) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Aggregations

HttpResponse (com.github.davidmoten.odata.client.HttpResponse)13 RequestHeader (com.github.davidmoten.odata.client.RequestHeader)12 ContextPath (com.github.davidmoten.odata.client.ContextPath)7 ClientException (com.github.davidmoten.odata.client.ClientException)3 HttpService (com.github.davidmoten.odata.client.HttpService)3 IOException (java.io.IOException)2 URL (java.net.URL)2 ArrayList (java.util.ArrayList)2 OutputStream (java.io.OutputStream)1 HttpURLConnection (java.net.HttpURLConnection)1 MalformedURLException (java.net.MalformedURLException)1 ProtocolException (java.net.ProtocolException)1 List (java.util.List)1 GraphService (odata.msgraph.client.container.GraphService)1 HttpEntity (org.apache.http.HttpEntity)1 HttpEntityEnclosingRequest (org.apache.http.HttpEntityEnclosingRequest)1 RequestConfig (org.apache.http.client.config.RequestConfig)1 Builder (org.apache.http.client.config.RequestConfig.Builder)1 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)1 InputStreamEntity (org.apache.http.entity.InputStreamEntity)1