AnCH Framework 0.2
Another C++ Hack: C++23 utility libraries
Loading...
Searching...
No Matches
anch::rest::Client Class Reference

#include <client.hpp>

Collaboration diagram for anch::rest::Client:

Public Member Functions

 Client (std::unique_ptr< anch::rest::Transport > transport, std::string_view baseUrl)
Client & header (std::string_view name, std::string_view value)
Client & filter (anch::rest::Filter filter)
template<anch::rest::ParameterValue... P>
anch::rest::Call request (anch::rest::Method method, std::string_view path, const P &... parameters)
template<anch::rest::ParameterValue... P>
anch::rest::Call get (std::string_view path, const P &... parameters)
template<anch::rest::ParameterValue... P>
anch::rest::Call post (std::string_view path, const P &... parameters)
template<anch::rest::ParameterValue... P>
anch::rest::Call put (std::string_view path, const P &... parameters)
template<anch::rest::ParameterValue... P>
anch::rest::Call patch (std::string_view path, const P &... parameters)
template<anch::rest::ParameterValue... P>
anch::rest::Call del (std::string_view path, const P &... parameters)
template<anch::rest::ParameterValue... P>
anch::rest::Call head (std::string_view path, const P &... parameters)
anch::rest::Response send (anch::rest::Request &request)
std::string url (const anch::rest::Request &request) const

Static Public Member Functions

static std::string formatPath (std::string_view path, const std::vector< std::string > &parameters)

Detailed Description

HTTP client: builds requests and sends them through a Transport .

config.timeout = std::chrono::seconds(5);
anch::rest::Client client(std::make_unique<anch::rest::CurlTransport>(config), "https://api.example.com/v1");
client.filter(anch::rest::bearerAuth(token));
Pet pet = client.get("/pets/{id}", 42).accept<anch::codec::Json>().as<Pet>(); // HttpError when status >= 400
anch::rest::Response created = client.post("/pets").body<anch::codec::Json>(pet).send();
client.del("/pets/{id}", 42).execute();

Path parameters ({name} placeholders) are replaced in order and percent-encoded.
Default headers (User-Agent is anch-rest ) are set when the request does not set them, then filters are executed in declaration order before the transport.
A client is as thread safe as its transport.

Author
Vincent Lachenal
Since
0.2

Constructor & Destructor Documentation

◆ Client()

anch::rest::Client::Client ( std::unique_ptr< anch::rest::Transport > transport,
std::string_view baseUrl )

Client constructor

Parameters
transportthe transport
baseUrlthe base URL (https://api.example.com/v1 )

Member Function Documentation

◆ del()

template<anch::rest::ParameterValue... P>
anch::rest::Call anch::rest::Client::del ( std::string_view path,
const P &... parameters )

Start a DELETE request (see request )

Parameters
paththe path
parametersthe path parameters
Returns
the call

◆ filter()

Client & anch::rest::Client::filter ( anch::rest::Filter filter)

Add filter

Parameters
filterthe filter
Returns
this
Here is the call graph for this function:

◆ formatPath()

std::string anch::rest::Client::formatPath ( std::string_view path,
const std::vector< std::string > & parameters )
static

Replace path placeholders ({name} ) with percent-encoded parameters

Parameters
paththe path
parametersthe parameters, in placeholders order
Returns
the path
Exceptions
std::invalid_argumentwhen the number of parameters does not match placeholders
Here is the call graph for this function:

◆ get()

template<anch::rest::ParameterValue... P>
anch::rest::Call anch::rest::Client::get ( std::string_view path,
const P &... parameters )

Start a GET request (see request )

Parameters
paththe path
parametersthe path parameters
Returns
the call

◆ head()

template<anch::rest::ParameterValue... P>
anch::rest::Call anch::rest::Client::head ( std::string_view path,
const P &... parameters )

Start a HEAD request (see request )

Parameters
paththe path
parametersthe path parameters
Returns
the call

◆ header()

Client & anch::rest::Client::header ( std::string_view name,
std::string_view value )

Set default header

Parameters
namethe header name
valuethe header value
Returns
this
Here is the call graph for this function:

◆ patch()

template<anch::rest::ParameterValue... P>
anch::rest::Call anch::rest::Client::patch ( std::string_view path,
const P &... parameters )

Start a PATCH request (see request )

Parameters
paththe path
parametersthe path parameters
Returns
the call

◆ post()

template<anch::rest::ParameterValue... P>
anch::rest::Call anch::rest::Client::post ( std::string_view path,
const P &... parameters )

Start a POST request (see request )

Parameters
paththe path
parametersthe path parameters
Returns
the call

◆ put()

template<anch::rest::ParameterValue... P>
anch::rest::Call anch::rest::Client::put ( std::string_view path,
const P &... parameters )

Start a PUT request (see request )

Parameters
paththe path
parametersthe path parameters
Returns
the call

◆ request()

template<anch::rest::ParameterValue... P>
anch::rest::Call anch::rest::Client::request ( anch::rest::Method method,
std::string_view path,
const P &... parameters )

Start a request

Parameters
methodthe method
paththe path relative to base URL, with {name} placeholders
parametersthe path parameters, in placeholders order
Returns
the call
Exceptions
std::invalid_argumentwhen the number of parameters does not match placeholders

◆ send()

anch::rest::Response anch::rest::Client::send ( anch::rest::Request & request)

Send request: default headers, filters, then transport

Parameters
requestthe request
Returns
the response
Exceptions
anch::rest::TransportErrorwhen request fails
Here is the call graph for this function:

◆ url()

std::string anch::rest::Client::url ( const anch::rest::Request & request) const

Get request URL: base URL, path and encoded query parameters

Parameters
requestthe request
Returns
the URL
Here is the call graph for this function:

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