LuciadCPillar 2024.0.08
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
luciad::HttpClient Class Referencefinal

An HTTP Client, implementation of the luciad::IHttpClient interface. More...

#include <luciad/net/http/HttpClient.h>

Inheritance diagram for luciad::HttpClient:
luciad::IHttpClient

Classes

class  Builder
 Builder for the luciad::HttpClient class. More...
 

Public Member Functions

 ~HttpClient () override
 
void clearHttpRequestOptions ()
 Clears the HTTP request options sent with every request, if any. More...
 
const std::optional< HttpRequestOptions > & getHttpRequestOptions () const
 Returns the HTTP request options sent with every request. More...
 
luciad::expected< HttpResponse, ErrorInfosend (const HttpRequest &request)
 Returns the HTTP response for the given HTTP request. More...
 
luciad::expected< HttpResponse, ErrorInfosend (const HttpRequest &request, const CancellationToken &token) override
 Returns the HTTP response for the given HTTP request. More...
 
void setHttpRequestOptions (HttpRequestOptions requestOptions)
 Sets the HTTP request options sent with every request. More...
 
- Public Member Functions inherited from luciad::IHttpClient
virtual ~IHttpClient ()=default
 
virtual luciad::expected< HttpResponse, ErrorInfosend (const HttpRequest &request, const CancellationToken &token)=0
 Returns the HTTP response for the given HTTP request. More...
 

Static Public Member Functions

static Builder newBuilder ()
 Creates the new HttpClient builder. More...
 

Detailed Description

An HTTP Client, implementation of the luciad::IHttpClient interface.

An HttpClient can be used to send requests and retrieve their responses. An HttpClient is created through a builder. The builder can be used to configure client states, like: a proxy selector, an authenticator provider, etc.

Example

HttpRequest request = HttpRequest::newBuilder().uri("https://foo.com/").method(HttpRequestMethod::POST).body(DataEntity(buffer, "application/json")).build();
std::shared_ptr<HttpClient> client = HttpClient::newBuilder().proxySelector(proxySelector).credentialProvider(credentialsProvider).build();
luciad::expected<HttpResponse, ErrorInfo> response = client->send(request);
if (response.has_value()) {
std::cout << response->getStatusCode() << std::endl;
}
Represents a blob of data.
Definition: DataEntity.h:19
Builder & credentialProvider(std::shared_ptr< ICredentialsProvider > credentialsProvider)
Sets the credentials provider used by the HttpClient.
Builder & proxySelector(std::shared_ptr< IProxySelector > proxySelector)
Sets the proxy selector used by the HttpClient.
std::shared_ptr< HttpClient > build() const
Returns an luciad::HttpClient, based on the properties set on this builder.
static Builder newBuilder()
Creates the new HttpClient builder.
An HTTP request.
Definition: HttpRequest.h:26
static Builder newBuilder()
Creates a new HttpRequest builder.
@ POST
Request method POST.

This class is thread-safe, i.e. it can handle multiple concurrent requests.

Since
2023.0

Constructor & Destructor Documentation

◆ ~HttpClient()

luciad::HttpClient::~HttpClient ( )
override

Member Function Documentation

◆ clearHttpRequestOptions()

void luciad::HttpClient::clearHttpRequestOptions ( )

Clears the HTTP request options sent with every request, if any.

◆ getHttpRequestOptions()

const std::optional< HttpRequestOptions > & luciad::HttpClient::getHttpRequestOptions ( ) const

Returns the HTTP request options sent with every request.

Returns
the HTTP request options sent with every request.

◆ newBuilder()

static Builder luciad::HttpClient::newBuilder ( )
static

Creates the new HttpClient builder.

Returns
the new HttpClient builder.

◆ send() [1/2]

luciad::expected< HttpResponse, ErrorInfo > luciad::HttpClient::send ( const HttpRequest request)

Returns the HTTP response for the given HTTP request.

This method is synchronous and blocks until the request has been sent and the response has been received.

Parameters
requestHTTP request.
Returns
the HTTP response, error information otherwise.

◆ send() [2/2]

luciad::expected< HttpResponse, ErrorInfo > luciad::HttpClient::send ( const HttpRequest request,
const CancellationToken token 
)
overridevirtual

Returns the HTTP response for the given HTTP request.

This method is synchronous and blocks until the request has been sent and the response has been received.

The request process can be canceled by the means of the cancellation token. It is up to the implementer to verify that the cancellation token state is set to true during the operation. When set to true this indicates that a HTTP response is no longer needed. The return value then can be an ErrorInfo object with the error code Canceled.

Parameters
requestHTTP request.
tokenCancellation token.
Returns
the HTTP response, error information otherwise.

Implements luciad::IHttpClient.

◆ setHttpRequestOptions()

void luciad::HttpClient::setHttpRequestOptions ( HttpRequestOptions  requestOptions)

Sets the HTTP request options sent with every request.

Parameters
requestOptionsthe request options.