RAVENNAKIT
Loading...
Searching...
No Matches
rav::HttpClient Class Reference

A high level wrapper around boost::beast for making HTTP requests. More...

#include <http_client.hpp>

Inheritance diagram for rav::HttpClient:
rav::HttpClientBase

Public Member Functions

 HttpClient (boost::asio::io_context &io_context, std::chrono::milliseconds timeout_seconds=std::chrono::seconds(30))
 Constructs a new HttpClient using the given io_context, but no url.
 HttpClient (boost::asio::io_context &io_context, std::string_view url, std::chrono::milliseconds timeout_seconds=std::chrono::seconds(30))
 Constructs a new HttpClient using the given io_context and url.
 HttpClient (boost::asio::io_context &io_context, const boost::urls::url &url, std::chrono::milliseconds timeout_seconds=std::chrono::seconds(30))
 Constructs a new HttpClient using the given io_context and url.
 HttpClient (boost::asio::io_context &io_context, const boost::asio::ip::tcp::endpoint &endpoint, std::chrono::milliseconds timeout_seconds=std::chrono::seconds(30))
 Constructs a new HttpClient using the given io_context and url.
 HttpClient (boost::asio::io_context &io_context, const boost::asio::ip::address &address, uint16_t port, std::chrono::milliseconds timeout_seconds=std::chrono::seconds(30))
 Constructs a new HttpClient using the given io_context and url.
 ~HttpClient () override
 HttpClient (const HttpClient &)=delete
HttpClient & operator= (const HttpClient &)=delete
 HttpClient (HttpClient &&)=delete
HttpClient & operator= (HttpClient &&)=delete
void set_host (const boost::urls::url &url) override
 Sets the host to connect to.
void set_host (std::string_view url) override
 Sets the host to connect to.
void set_host (std::string_view host, std::string_view service) override
 Sets the host to connect to.
void get_async (std::string_view target, ResponseCallback callback) override
 Asynchronous GET request.
void post_async (std::string_view target, std::string body, ResponseCallback callback, std::string_view content_type) override
 Asynchronous POST request to the target of the URL, or the root if no target is specified.
void request_async (http::verb method, std::string_view target, std::string body, std::string_view content_type, ResponseCallback callback) override
 Asynchronous request.
void delete_async (std::string_view target, ResponseCallback callback) override
 Asynchronous DELETE request to the target of the URL, or the root if no target is specified.
void cancel_outstanding_requests () override
 Clears all scheduled requests if there are any.
const std::string & get_host () const override
const std::string & get_service () const override
Public Member Functions inherited from rav::HttpClientBase
virtual ~HttpClientBase ()=default

Additional Inherited Members

Public Types inherited from rav::HttpClientBase
using Request = http::request<http::string_body>
using Response = http::response<http::string_body>
using ResponseCallback = std::function<void(boost::system::result<http::response<http::string_body>> response)>
 Callback type for async requests.
Static Public Attributes inherited from rav::HttpClientBase
static constexpr auto k_default_port = "80"
 When no port is specified in the urls, the default port is used.

Detailed Description

A high level wrapper around boost::beast for making HTTP requests.

Definition at line 129 of file http_client.hpp.

Constructor & Destructor Documentation

◆ HttpClient() [1/7]

rav::HttpClient::HttpClient ( boost::asio::io_context & io_context,
std::chrono::milliseconds timeout_seconds = std::chrono::seconds(30) )
explicit

Constructs a new HttpClient using the given io_context, but no url.

Parameters
io_contextThe io_context to use for the request.
timeout_secondsThe timeout in seconds for the requests. Defaults to 30 seconds.

◆ HttpClient() [2/7]

rav::HttpClient::HttpClient ( boost::asio::io_context & io_context,
std::string_view url,
std::chrono::milliseconds timeout_seconds = std::chrono::seconds(30) )

Constructs a new HttpClient using the given io_context and url.

Parameters
io_contextThe io_context to use for the request.
urlThe url to request.
timeout_secondsThe timeout in seconds for the requests. Defaults to 30 seconds.

◆ HttpClient() [3/7]

rav::HttpClient::HttpClient ( boost::asio::io_context & io_context,
const boost::urls::url & url,
std::chrono::milliseconds timeout_seconds = std::chrono::seconds(30) )

Constructs a new HttpClient using the given io_context and url.

Parameters
io_contextThe io_context to use for the request.
urlThe url to request.
timeout_secondsThe timeout in seconds for the requests. Defaults to 30 seconds.

◆ HttpClient() [4/7]

rav::HttpClient::HttpClient ( boost::asio::io_context & io_context,
const boost::asio::ip::tcp::endpoint & endpoint,
std::chrono::milliseconds timeout_seconds = std::chrono::seconds(30) )

Constructs a new HttpClient using the given io_context and url.

Parameters
io_contextThe io_context to use for the request.
endpointThe endpoint to request.
timeout_secondsThe timeout in seconds for the requests. Defaults to 30 seconds.

◆ HttpClient() [5/7]

rav::HttpClient::HttpClient ( boost::asio::io_context & io_context,
const boost::asio::ip::address & address,
uint16_t port,
std::chrono::milliseconds timeout_seconds = std::chrono::seconds(30) )

Constructs a new HttpClient using the given io_context and url.

Parameters
io_contextThe io_context to use for the request.
addressThe address to request.
portThe port to request.
timeout_secondsThe timeout in seconds for the requests. Defaults to 30 seconds.

◆ ~HttpClient()

rav::HttpClient::~HttpClient ( )
override

◆ HttpClient() [6/7]

rav::HttpClient::HttpClient ( const HttpClient & )
delete

◆ HttpClient() [7/7]

rav::HttpClient::HttpClient ( HttpClient && )
delete

Member Function Documentation

◆ cancel_outstanding_requests()

void rav::HttpClient::cancel_outstanding_requests ( )
overridevirtual

Clears all scheduled requests if there are any.

Otherwise, this function has no effect.

Implements rav::HttpClientBase.

◆ delete_async()

void rav::HttpClient::delete_async ( std::string_view target,
ResponseCallback callback )
overridevirtual

Asynchronous DELETE request to the target of the URL, or the root if no target is specified.

Parameters
targetThe target to request.
callbackThe callback to call when the request is complete.

Implements rav::HttpClientBase.

◆ get_async()

void rav::HttpClient::get_async ( std::string_view target,
ResponseCallback callback )
overridevirtual

Asynchronous GET request.

The callback's lifetime will be tied to the io_context so make sure referenced objects are kept alive until the callback is called.

Parameters
targetThe target to request.
callbackThe callback to call when the request is complete.

Implements rav::HttpClientBase.

◆ get_host()

const std::string & rav::HttpClient::get_host ( ) const
nodiscardoverridevirtual

Returns
The host that is currently set for this client.

Implements rav::HttpClientBase.

◆ get_service()

const std::string & rav::HttpClient::get_service ( ) const
nodiscardoverridevirtual

Returns
The service (port) that this current client is set for this client.

Implements rav::HttpClientBase.

◆ operator=() [1/2]

HttpClient & rav::HttpClient::operator= ( const HttpClient & )
delete

◆ operator=() [2/2]

HttpClient & rav::HttpClient::operator= ( HttpClient && )
delete

◆ post_async()

void rav::HttpClient::post_async ( std::string_view target,
std::string body,
ResponseCallback callback,
std::string_view content_type )
overridevirtual

Asynchronous POST request to the target of the URL, or the root if no target is specified.

Parameters
targetThe target to request.
bodyThe body to send with the request.
callbackThe callback to call when the request is complete.
content_typeThe content type of the body, e.g. "application/json". If not specified, defaults to "application/json".

Implements rav::HttpClientBase.

◆ request_async()

void rav::HttpClient::request_async ( http::verb method,
std::string_view target,
std::string body,
std::string_view content_type,
ResponseCallback callback )
overridevirtual

Asynchronous request.

Parameters
methodThe HTTP method to use for the request.
targetThe target to request.
bodyThe optional body to send with the request.
content_type
callbackThe callback to call when the request is complete.

Implements rav::HttpClientBase.

◆ set_host() [1/3]

void rav::HttpClient::set_host ( const boost::urls::url & url)
overridevirtual

Sets the host to connect to.

Parameters
urlThe url with the host info.

Implements rav::HttpClientBase.

◆ set_host() [2/3]

void rav::HttpClient::set_host ( std::string_view host,
std::string_view service )
overridevirtual

Sets the host to connect to.

Parameters
urlThe url with the host info.

Implements rav::HttpClientBase.

◆ set_host() [3/3]

void rav::HttpClient::set_host ( std::string_view url)
overridevirtual

Sets the host to connect to.

Parameters
urlThe url with the host info.

Implements rav::HttpClientBase.


The documentation for this class was generated from the following file: