Search in sources :

Example 1 with HttpService

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

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

the class RequestHelper method send.

public static void send(HttpMethod method, ContextPath contextPath, RequestOptions options, InputStream in, int length) {
    List<RequestHeader> h = cleanAndSupplementRequestHeaders(options, "minimal", true);
    ContextPath cp = contextPath.addQueries(options.getQueries());
    HttpService service = cp.context().service();
    final HttpResponse response = service.submit(method, cp.toUrl(), h, in, length, 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 3 with HttpService

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

the class RequestHelper method patchOrPut.

@SuppressWarnings("unused")
private static <T extends ODataEntityType> T patchOrPut(T entity, ContextPath contextPath, RequestOptions options, HttpMethod method) {
    Preconditions.checkArgument(method == HttpMethod.PUT || method == HttpMethod.PATCH);
    final String json;
    if (method == HttpMethod.PATCH) {
        json = Serializer.INSTANCE.serializeChangesOnly(entity);
    } else {
        json = Serializer.INSTANCE.serialize(entity);
    }
    // build the url
    ContextPath cp = contextPath.addQueries(options.getQueries());
    List<RequestHeader> h = cleanAndSupplementRequestHeaders(options, "minimal", true);
    final String url;
    String editLink = (String) entity.getUnmappedFields().get("@odata.editLink");
    // TODO get patch working when editLink present (does not work with MsGraph)
    if (editLink != null && false) {
        if (editLink.startsWith(HTTPS) || editLink.startsWith("http://")) {
            url = editLink;
        } else {
            // TOOD unit test relative url in editLink
            // from
            // http://docs.oasis-open.org/odata/odata-json-format/v4.01/cs01/odata-json-format-v4.01-cs01.html#_Toc499720582
            String context = (String) entity.getUnmappedFields().get("@odata.context");
            if (context != null) {
                try {
                    URL u = new URL(context);
                    String p = u.getPath();
                    String basePath = p.substring(0, p.lastIndexOf('/'));
                    url = basePath + "/" + editLink;
                } catch (MalformedURLException e) {
                    throw new ClientException(e);
                }
            } else {
                url = cp.context().service().getBasePath().toUrl() + "/" + editLink;
            }
        }
    } else {
        url = cp.toUrl();
    }
    // get the response
    HttpService service = cp.context().service();
    final HttpResponse response = service.submit(method, url, h, json, options);
    checkResponseCodeOk(cp, response);
    // original?
    return entity;
}
Also used : ContextPath(com.github.davidmoten.odata.client.ContextPath) MalformedURLException(java.net.MalformedURLException) HttpService(com.github.davidmoten.odata.client.HttpService) RequestHeader(com.github.davidmoten.odata.client.RequestHeader) HttpResponse(com.github.davidmoten.odata.client.HttpResponse) ClientException(com.github.davidmoten.odata.client.ClientException) URL(java.net.URL)

Example 4 with HttpService

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

the class MicrosoftClientBuilder method createService.

@SuppressWarnings("resource")
private static <T> T createService(String baseUrl, Authenticator authenticator, // 
long connectTimeoutMs, // 
long readTimeoutMs, // 
Optional<String> proxyHost, // 
Optional<Integer> proxyPort, Optional<String> proxyUsername, Optional<String> proxyPassword, Optional<Supplier<CloseableHttpClient>> supplier, Optional<Function<HttpClientBuilder, HttpClientBuilder>> httpClientBuilderExtras, // 
Creator<T> creator, // 
String authenticationEndpoint, // 
Function<? super HttpService, ? extends HttpService> httpServiceTransformer, List<SchemaInfo> schemas, PathStyle pathStyle) {
    final Supplier<CloseableHttpClient> clientSupplier = createClientSupplier(connectTimeoutMs, readTimeoutMs, proxyHost, proxyPort, proxyUsername, proxyPassword, supplier, httpClientBuilderExtras);
    Path basePath = new Path(baseUrl, pathStyle);
    HttpService httpService = new // 
    ApacheHttpClientHttpService(// 
    basePath, // 
    clientSupplier, authenticator::authenticate);
    httpService = httpServiceTransformer.apply(httpService);
    return creator.create(new Context(Serializer.INSTANCE, httpService, createProperties(), schemas));
}
Also used : Path(com.github.davidmoten.odata.client.Path) Context(com.github.davidmoten.odata.client.Context) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpService(com.github.davidmoten.odata.client.HttpService) ApacheHttpClientHttpService(com.github.davidmoten.odata.client.internal.ApacheHttpClientHttpService) ApacheHttpClientHttpService(com.github.davidmoten.odata.client.internal.ApacheHttpClientHttpService)

Aggregations

HttpService (com.github.davidmoten.odata.client.HttpService)4 ContextPath (com.github.davidmoten.odata.client.ContextPath)3 HttpResponse (com.github.davidmoten.odata.client.HttpResponse)3 RequestHeader (com.github.davidmoten.odata.client.RequestHeader)3 ClientException (com.github.davidmoten.odata.client.ClientException)1 Context (com.github.davidmoten.odata.client.Context)1 Path (com.github.davidmoten.odata.client.Path)1 ApacheHttpClientHttpService (com.github.davidmoten.odata.client.internal.ApacheHttpClientHttpService)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)1