Search in sources :

Example 1 with RequestDefinition

use of com.github.ljtfreitas.julian.RequestDefinition in project julian-http-client by ljtfreitas.

the class DefaultHTTP method run.

@Override
public <T> Promise<HTTPResponse<T>> run(RequestDefinition definition) {
    URI uri = definition.path();
    HTTPHeaders headers = definition.headers().all().stream().map(h -> new HTTPHeader(h.name(), h.values())).reduce(HTTPHeaders.empty(), HTTPHeaders::join, (a, b) -> b);
    HTTPRequestBody body = definition.body().map(b -> body(b.content(), b.javaType(), headers)).orElse(null);
    HTTPMethod method = HTTPMethod.select(definition.method()).orElseThrow(() -> new IllegalArgumentException(format("Unsupported HTTP method: {0}", definition.method())));
    HTTPRequest<T> request = new DefaultHTTPRequest<>(uri, method, body, headers, definition.returnType(), httpClient, codecs, failure);
    return intercepts(Promise.pending(() -> request, executor)).bind(HTTPRequest::execute);
}
Also used : HTTPClient(com.github.ljtfreitas.julian.http.client.HTTPClient) CONTENT_TYPE(com.github.ljtfreitas.julian.http.HTTPHeader.CONTENT_TYPE) HTTPMessageCodecs(com.github.ljtfreitas.julian.http.codec.HTTPMessageCodecs) HTTPRequestWriterException(com.github.ljtfreitas.julian.http.codec.HTTPRequestWriterException) Charset(java.nio.charset.Charset) Executor(java.util.concurrent.Executor) Promise(com.github.ljtfreitas.julian.Promise) RequestDefinition(com.github.ljtfreitas.julian.RequestDefinition) URI(java.net.URI) Message.format(com.github.ljtfreitas.julian.Message.format) JavaType(com.github.ljtfreitas.julian.JavaType) StandardCharsets(java.nio.charset.StandardCharsets) URI(java.net.URI)

Aggregations

JavaType (com.github.ljtfreitas.julian.JavaType)1 Message.format (com.github.ljtfreitas.julian.Message.format)1 Promise (com.github.ljtfreitas.julian.Promise)1 RequestDefinition (com.github.ljtfreitas.julian.RequestDefinition)1 CONTENT_TYPE (com.github.ljtfreitas.julian.http.HTTPHeader.CONTENT_TYPE)1 HTTPClient (com.github.ljtfreitas.julian.http.client.HTTPClient)1 HTTPMessageCodecs (com.github.ljtfreitas.julian.http.codec.HTTPMessageCodecs)1 HTTPRequestWriterException (com.github.ljtfreitas.julian.http.codec.HTTPRequestWriterException)1 URI (java.net.URI)1 Charset (java.nio.charset.Charset)1 StandardCharsets (java.nio.charset.StandardCharsets)1 Executor (java.util.concurrent.Executor)1