Official websites use .gov
A .gov website belongs to an official government organization in the United States.

Secure .gov websites use HTTPS
A lock ( ) or https:// means you’ve safely connected to the .gov website. Share sensitive information only on official, secure websites.

Isis 3 Programmer Reference
restincurl::RequestBuilder Class Reference

#include <restincurl.h>

Collaboration diagram for restincurl::RequestBuilder:
Collaboration graph

Public Types

using ptr_t = std::unique_ptr<RequestBuilder>
 

Public Member Functions

 RequestBuilder (Worker &worker)
 
bool CanSendFile () const noexcept
 

Protected Member Functions

RequestBuilderPrepare (RequestType rt, const std::string &url)
 

Static Private Member Functions

static size_t write_callback (char *ptr, size_t size, size_t nitems, void *userdata)
 
static int debug_callback (CURL *handle, curl_infotype type, char *data, size_t size, void *userp)
 

Type of request

Use one of these functions to declare what HTTP request you want to use.

Parameters
urlUrl to call. Must be a complete URL, starting with "http://" or "https://"

Note that you must use only only one of these methods in one request.

std::unique_ptr< Requestrequest_
 
std::unique_ptr< class Optionsoptions_
 
std::string url_
 
RequestType request_type_ = RequestType::INVALID
 
bool have_data_in_ = false
 
bool have_data_out_ = false
 
bool is_built_ = false
 
completion_fn_t completion_
 
long request_timeout_ = 10000L
 
long connect_timeout_ = 3000L
 
Workerworker_
 
RequestBuilderGet (const std::string &url)
 
RequestBuilderHead (const std::string &url)
 
RequestBuilderPost (const std::string &url)
 
RequestBuilderPostMime (const std::string &url)
 
RequestBuilderPut (const std::string &url)
 
RequestBuilderPatch (const std::string &url)
 
RequestBuilderDelete (const std::string &url)
 
RequestBuilderOptions (const std::string &url)
 
RequestBuilderHeader (const char *value)
 
RequestBuilderHeader (const std::string &name, const std::string &value)
 
RequestBuilderWithJson ()
 
RequestBuilderWithJson (std::string body)
 
RequestBuilderAcceptJson ()
 
template<typename T>
RequestBuilderOption (const CURLoption &opt, const T &value)
 
RequestBuilderTrace (bool enable=true)
 
RequestBuilderRequestTimeout (const long timeout)
 
RequestBuilderConnectTimeout (const long timeout)
 
RequestBuilderSendFile (const std::string &path)
 
RequestBuilderSendFileAsMimeData (const std::string &path, const std::string &name={}, const std::string &remoteName={}, const std::string &mimeType={})
 
RequestBuilderSendFileAsForm (const std::string &path)
 
template<typename T>
RequestBuilderSendData (OutDataHandler< T > &dh)
 
template<typename T>
RequestBuilderSendData (T data)
 
template<typename T>
RequestBuilderStoreData (InDataHandler< T > &dh)
 
template<typename T>
RequestBuilderStoreData (T &data)
 
RequestBuilderIgnoreIncomingData ()
 
RequestBuilderWithCompletion (completion_fn_t fn)
 
RequestBuilderBasicAuthentication (const std::string &name, const std::string &passwd)
 
RequestBuilderSetReadHandler (size_t(*handler)(char *, size_t, size_t, void *), void *userdata)
 
RequestBuilderSetWriteHandler (size_t(*handler)(char *, size_t, size_t, void *), void *userdata)
 
void Build ()
 
void ExecuteSynchronous ()
 
void Execute ()
 

Detailed Description

Convenient interface to build requests.

Even if this is a light-weight wrapper around libcurl, we have a simple and modern way to define our requests that contains convenience-methods for the most common use-cases.

Definition at line 1103 of file restincurl.h.

Member Typedef Documentation

◆ ptr_t

using restincurl::RequestBuilder::ptr_t = std::unique_ptr<RequestBuilder>

Definition at line 1148 of file restincurl.h.

Constructor & Destructor Documentation

◆ RequestBuilder()

restincurl::RequestBuilder::RequestBuilder ( Worker & worker)
inline

Definition at line 1149 of file restincurl.h.

◆ ~RequestBuilder()

restincurl::RequestBuilder::~RequestBuilder ( )
inline

Definition at line 1161 of file restincurl.h.

Member Function Documentation

◆ AcceptJson()

RequestBuilder & restincurl::RequestBuilder::AcceptJson ( )
inline

Sets the accept header to "Application/json"

Definition at line 1272 of file restincurl.h.

References Header().

◆ BasicAuthentication()

RequestBuilder & restincurl::RequestBuilder::BasicAuthentication ( const std::string & name,
const std::string & passwd )
inline

HTTP Basic Authentication

Authenticate the request with HTTP Basic Authentication.

Parameters
nameName to authenticate with
passwdPassword to authenticate with

Note that if name or password is empty, authentication is ignored. This makes it simple to add optional authentication to your project, by simply assigning values to the strings you pass here, or not.

Definition at line 1523 of file restincurl.h.

◆ Build()

void restincurl::RequestBuilder::Build ( )
inline

Definition at line 1561 of file restincurl.h.

◆ CanSendFile()

bool restincurl::RequestBuilder::CanSendFile ( ) const
inlinenoexcept

Definition at line 1174 of file restincurl.h.

◆ ConnectTimeout()

RequestBuilder & restincurl::RequestBuilder::ConnectTimeout ( const long timeout)
inline

Set the connect timeout for a request

Parameters
timeoutTimeout in milliseconds. Set to -1 to use the default.

Definition at line 1325 of file restincurl.h.

◆ debug_callback()

static int restincurl::RequestBuilder::debug_callback ( CURL * handle,
curl_infotype type,
char * data,
size_t size,
void * userp )
inlinestaticprivate

Definition at line 1110 of file restincurl.h.

◆ Delete()

RequestBuilder & restincurl::RequestBuilder::Delete ( const std::string & url)
inline

Use a HTTP DELETE request

Definition at line 1223 of file restincurl.h.

◆ Execute()

void restincurl::RequestBuilder::Execute ( )
inline

Execute the request asynchronously

This will queue the request for processing. If the number of active requests are less than RESTINCURL_MAX_CONNECTIONS, the request will start executing almost immediately.

The method returns immediately.

Exceptions
restincurl::Exceptionderived exceptions on error

This method is only available when RESTINCURL_ENABLE_ASYNC is nonzero.

Definition at line 1623 of file restincurl.h.

◆ ExecuteSynchronous()

void restincurl::RequestBuilder::ExecuteSynchronous ( )
inline

Execute the request synchronously

This will execute the request and call the callback (if you declared one) in the current thread before the method returns.

Exceptions
restincurl::Exceptionderived exceptions on error

This method is available even when RESTINCURL_ENABLE_ASYNC is enabled ( != 0).

Definition at line 1605 of file restincurl.h.

◆ Get()

RequestBuilder & restincurl::RequestBuilder::Get ( const std::string & url)
inline

Use a HTTP GET request

Definition at line 1193 of file restincurl.h.

◆ Head()

RequestBuilder & restincurl::RequestBuilder::Head ( const std::string & url)
inline

Use a HTTP HEAD request

Definition at line 1198 of file restincurl.h.

◆ Header() [1/2]

RequestBuilder & restincurl::RequestBuilder::Header ( const char * value)
inline

Specify a HTTP header for the request.

Parameters
valueThe value of the header-line, properly formatted according to the relevant HTTP specifications.

Definition at line 1237 of file restincurl.h.

Referenced by AcceptJson(), Header(), and WithJson().

◆ Header() [2/2]

RequestBuilder & restincurl::RequestBuilder::Header ( const std::string & name,
const std::string & value )
inline

Specify a HTTP header for the request.

Parameters
nameName of the header
valueThe value of the header

This is a convenience method that will build the appropriate header for you.

Definition at line 1251 of file restincurl.h.

References Header().

◆ IgnoreIncomingData()

RequestBuilder & restincurl::RequestBuilder::IgnoreIncomingData ( )
inline

Do not process incoming data

The response body will be read from the network, but not buffered and not available for later inspection.

Definition at line 1489 of file restincurl.h.

◆ Option()

template<typename T>
RequestBuilder & restincurl::RequestBuilder::Option ( const CURLoption & opt,
const T & value )
inline

Sets a Curl options.

Parameters
optCURLoption enum specifying the option
valueValue to set.

It is critical that the type of the value is of the same type that libcurl is expecting for the option. RESTinCurl makes no attempt to validate or cast the values.

Please refer to the libcurl documentation for curl_easy_setopt()

Definition at line 1288 of file restincurl.h.

Referenced by Trace().

◆ Options()

RequestBuilder & restincurl::RequestBuilder::Options ( const std::string & url)
inline

Use a HTTP OPTIONS request

Definition at line 1228 of file restincurl.h.

◆ Patch()

RequestBuilder & restincurl::RequestBuilder::Patch ( const std::string & url)
inline

Use a HTTP PATCH request

Definition at line 1218 of file restincurl.h.

◆ Post()

RequestBuilder & restincurl::RequestBuilder::Post ( const std::string & url)
inline

Use a HTTP POST request

Definition at line 1203 of file restincurl.h.

◆ PostMime()

RequestBuilder & restincurl::RequestBuilder::PostMime ( const std::string & url)
inline

Use a HTTP POST request

Definition at line 1208 of file restincurl.h.

◆ Prepare()

RequestBuilder & restincurl::RequestBuilder::Prepare ( RequestType rt,
const std::string & url )
inlineprotected

Definition at line 1165 of file restincurl.h.

◆ Put()

RequestBuilder & restincurl::RequestBuilder::Put ( const std::string & url)
inline

Use a HTTP PUT request

Definition at line 1213 of file restincurl.h.

◆ RequestTimeout()

RequestBuilder & restincurl::RequestBuilder::RequestTimeout ( const long timeout)
inline

Set request timeout

Parameters
timeoutTimeout in milliseconds. Set to -1 to use the default.

Definition at line 1316 of file restincurl.h.

◆ SendData() [1/2]

template<typename T>
RequestBuilder & restincurl::RequestBuilder::SendData ( OutDataHandler< T > & dh)
inline

Specify Data Handler for outbound data

You can use this method when you need to use a Data Handler, rather than a simple string, to provide the data for a POST, PUT etc. request.

Parameters
dhData Handler instance.

Note that the Data Handler is passed by reference. It is your responsibility that the instance is present at least until the request has finished (your code owns the Data Handler instance).

Definition at line 1423 of file restincurl.h.

Referenced by SendData(), and WithJson().

◆ SendData() [2/2]

template<typename T>
RequestBuilder & restincurl::RequestBuilder::SendData ( T data)
inline

Convenience method to specify a object that contains the data to send during a request.

Parameters
dataData to send. Typically this will be a std::string, std::vector<char> or a similar object.

RESTinCurl takes ownership of this data (by moving it).

Definition at line 1439 of file restincurl.h.

References SendData().

◆ SendFile()

RequestBuilder & restincurl::RequestBuilder::SendFile ( const std::string & path)
inline

Send a file

Parameters
pathFull path to the file to send.
Exceptions
SystemExceptionif the file cannot be opened.
Exceptionif the method is called for a non-send operation

Definition at line 1337 of file restincurl.h.

◆ SendFileAsForm()

RequestBuilder & restincurl::RequestBuilder::SendFileAsForm ( const std::string & path)
inline

Send a file

Parameters
pathFull path to the file to send.
Exceptions
SystemExceptionif the file cannot be opened.
Exceptionif the method is called for a non-send operation

Definition at line 1390 of file restincurl.h.

◆ SendFileAsMimeData()

RequestBuilder & restincurl::RequestBuilder::SendFileAsMimeData ( const std::string & path,
const std::string & name = {},
const std::string & remoteName = {},
const std::string & mimeType = {} )
inline

Send a file as a multipart/form mime segment

Parameters
pathFull path to the file tro send
nameOtional name to use for the file in the mime segment
remoteNameOptional name to label the file as for the remote end
mimeTypeOptional mime-type for the file
Exceptions
Exceptionif the method is called for a non-mime-post operation

Definition at line 1370 of file restincurl.h.

◆ SetReadHandler()

RequestBuilder & restincurl::RequestBuilder::SetReadHandler ( size_t(* handler )(char *, size_t, size_t, void *),
void * userdata )
inline

Set a Curl compatible read handler.

Parameters
handlerCurl C API read handler

You probably don't need to call this directly.

Definition at line 1541 of file restincurl.h.

◆ SetWriteHandler()

RequestBuilder & restincurl::RequestBuilder::SetWriteHandler ( size_t(* handler )(char *, size_t, size_t, void *),
void * userdata )
inline

Set a Curl compatible write handler.

Parameters
handlerCurl C API write handler

You probably don't need to call this directly.

Definition at line 1554 of file restincurl.h.

◆ StoreData() [1/2]

template<typename T>
RequestBuilder & restincurl::RequestBuilder::StoreData ( InDataHandler< T > & dh)
inline

Specify Data Handler for inbound data

You can use this method when you need to use a Data Handler, rather than a simple string, to receive data during the request.

Parameters
dhData Handler instance.

Note that the Data Handler is passed by reference. It is your responsibility that the instance is present at least until the request has finished (your code owns the Data Handler instance).

Definition at line 1458 of file restincurl.h.

Referenced by StoreData().

◆ StoreData() [2/2]

template<typename T>
RequestBuilder & restincurl::RequestBuilder::StoreData ( T & data)
inline

Convenience method to specify a object that receives incoming data during a request.

Parameters
dataBuffer to hold incoming data. Typically this will be a std::string, std::vector<char> or a similar object.

Note that data is passed by reference. It is your responsibility that the instance is present at least until the request has finished (your code owns the object).

Definition at line 1475 of file restincurl.h.

References StoreData().

◆ Trace()

RequestBuilder & restincurl::RequestBuilder::Trace ( bool enable = true)
inline

Enables or disables trace logging for requests.

The trace logging will show detailed information about what libcurl does and data sent and received during a request.

Basically it sets CURLOPT_DEBUGFUNCTION and CURLOPT_VERBOSE.

Definition at line 1301 of file restincurl.h.

References Option().

◆ WithCompletion()

RequestBuilder & restincurl::RequestBuilder::WithCompletion ( completion_fn_t fn)
inline

Specify a callback that will be called when the request is complete (or failed).

Parameters
fnCallback to be called

For asynchronous requests, the callback will be called from the worker-thread shared by all requests and timers for the client instance. It is imperative that you return immediately, and don't keep the thread busy more than strictly required. If you need do do some computing or IO in response to the information you receive, you should do that in another thread.

Definition at line 1505 of file restincurl.h.

◆ WithJson() [1/2]

RequestBuilder & restincurl::RequestBuilder::WithJson ( )
inline

Sets the content-type to "Application/json; charset=utf-8"

Definition at line 1258 of file restincurl.h.

References Header().

Referenced by WithJson().

◆ WithJson() [2/2]

RequestBuilder & restincurl::RequestBuilder::WithJson ( std::string body)
inline

Sets the content-type to "Application/json; charset=utf-8"

Parameters
bodyJson payload to send with the request.

Definition at line 1266 of file restincurl.h.

References SendData(), and WithJson().

◆ write_callback()

static size_t restincurl::RequestBuilder::write_callback ( char * ptr,
size_t size,
size_t nitems,
void * userdata )
inlinestaticprivate

Definition at line 1105 of file restincurl.h.

Member Data Documentation

◆ completion_

completion_fn_t restincurl::RequestBuilder::completion_
private

Definition at line 1637 of file restincurl.h.

◆ connect_timeout_

long restincurl::RequestBuilder::connect_timeout_ = 3000L
private

Definition at line 1639 of file restincurl.h.

◆ have_data_in_

bool restincurl::RequestBuilder::have_data_in_ = false
private

Definition at line 1634 of file restincurl.h.

◆ have_data_out_

bool restincurl::RequestBuilder::have_data_out_ = false
private

Definition at line 1635 of file restincurl.h.

◆ is_built_

bool restincurl::RequestBuilder::is_built_ = false
private

Definition at line 1636 of file restincurl.h.

◆ options_

std::unique_ptr<class Options> restincurl::RequestBuilder::options_
private

Definition at line 1631 of file restincurl.h.

◆ request_

std::unique_ptr<Request> restincurl::RequestBuilder::request_
private

Definition at line 1630 of file restincurl.h.

◆ request_timeout_

long restincurl::RequestBuilder::request_timeout_ = 10000L
private

Definition at line 1638 of file restincurl.h.

◆ request_type_

RequestType restincurl::RequestBuilder::request_type_ = RequestType::INVALID
private

Definition at line 1633 of file restincurl.h.

◆ url_

std::string restincurl::RequestBuilder::url_
private

Definition at line 1632 of file restincurl.h.

◆ worker_

Worker& restincurl::RequestBuilder::worker_
private

Definition at line 1641 of file restincurl.h.


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