Search in sources :

Example 1 with RequestHeader

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

the class DefaultHttpService method getStream.

@Override
public InputStream getStream(HttpMethod method, String url, List<RequestHeader> requestHeaders, HttpRequestOptions options) {
    try {
        URL u = new URL(url);
        HttpURLConnection c = (HttpURLConnection) u.openConnection();
        c.setInstanceFollowRedirects(true);
        c.setRequestMethod(method.toString());
        for (RequestHeader header : requestHeadersModifier.apply(requestHeaders)) {
            c.setRequestProperty(header.name(), header.value());
        }
        c.setDoInput(true);
        c.setDoOutput(false);
        // apply just before connection established so further configuration can take
        // place like timeouts
        consumer.accept(c);
        // TODO check error code and throw message read from input stream
        return c.getInputStream();
    } catch (IOException e) {
        throw new ClientException(e);
    }
}
Also used : HttpURLConnection(java.net.HttpURLConnection) RequestHeader(com.github.davidmoten.odata.client.RequestHeader) IOException(java.io.IOException) ClientException(com.github.davidmoten.odata.client.ClientException) URL(java.net.URL)

Example 2 with RequestHeader

use of com.github.davidmoten.odata.client.RequestHeader 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 3 with RequestHeader

use of com.github.davidmoten.odata.client.RequestHeader 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 4 with RequestHeader

use of com.github.davidmoten.odata.client.RequestHeader 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 5 with RequestHeader

use of com.github.davidmoten.odata.client.RequestHeader 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)

Aggregations

RequestHeader (com.github.davidmoten.odata.client.RequestHeader)16 HttpResponse (com.github.davidmoten.odata.client.HttpResponse)13 ContextPath (com.github.davidmoten.odata.client.ContextPath)8 ClientException (com.github.davidmoten.odata.client.ClientException)6 HttpService (com.github.davidmoten.odata.client.HttpService)4 IOException (java.io.IOException)4 URL (java.net.URL)4 HttpURLConnection (java.net.HttpURLConnection)3 RequestOptions (com.github.davidmoten.odata.client.RequestOptions)2 InputStream (java.io.InputStream)2 MalformedURLException (java.net.MalformedURLException)2 ArrayList (java.util.ArrayList)2 Preconditions (com.github.davidmoten.guavamini.Preconditions)1 AuthenticationEndpoint (com.github.davidmoten.microsoft.authentication.AuthenticationEndpoint)1 Context (com.github.davidmoten.odata.client.Context)1 HttpMethod (com.github.davidmoten.odata.client.HttpMethod)1 HttpRequestOptions (com.github.davidmoten.odata.client.HttpRequestOptions)1 ODataEntityType (com.github.davidmoten.odata.client.ODataEntityType)1 ODataType (com.github.davidmoten.odata.client.ODataType)1 Path (com.github.davidmoten.odata.client.Path)1