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 Developer 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)
 
 ~RequestBuilder ()
 
bool CanSendFile () const noexcept
 

Protected Member Functions

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

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.

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.

Member Typedef Documentation

◆ ptr_t

Constructor & Destructor Documentation

◆ RequestBuilder()

◆ ~RequestBuilder()

restincurl::RequestBuilder::~RequestBuilder ( )
inline

Member Function Documentation

◆ AcceptJson()

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

Sets the accept header to "Application/json"

References Header(), and RequestBuilder().

◆ 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.

References RequestBuilder().

◆ Build()

void restincurl::RequestBuilder::Build ( )
inline

References RESTINCURL_LOG, and StoreData().

Referenced by Execute(), and ExecuteSynchronous().

◆ CanSendFile()

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

References restincurl::POST, and restincurl::PUT.

Referenced by SendFile(), and SendFileAsForm().

◆ 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.

References RequestBuilder().

◆ Delete()

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

Use a HTTP DELETE request

References restincurl::DELETE, Prepare(), and RequestBuilder().

◆ 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.

References Build().

◆ 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).

References Build().

◆ Get()

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

Use a HTTP GET request

References restincurl::GET, Prepare(), and RequestBuilder().

◆ Head()

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

Use a HTTP HEAD request

References restincurl::HEAD, Prepare(), and RequestBuilder().

◆ 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.

References RequestBuilder().

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.

References Header(), and RequestBuilder().

◆ 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.

References RequestBuilder().

◆ 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()

References RequestBuilder().

Referenced by Trace().

◆ Options()

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

Use a HTTP OPTIONS request

References restincurl::OPTIONS, Prepare(), and RequestBuilder().

Referenced by RequestBuilder().

◆ Patch()

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

Use a HTTP PATCH request

References restincurl::PATCH, Prepare(), and RequestBuilder().

◆ Post()

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

Use a HTTP POST request

References restincurl::POST, Prepare(), and RequestBuilder().

◆ PostMime()

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

Use a HTTP POST request

References restincurl::POST_MIME, Prepare(), and RequestBuilder().

◆ Prepare()

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

◆ Put()

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

Use a HTTP PUT request

References Prepare(), restincurl::PUT, and RequestBuilder().

◆ RequestTimeout()

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

Set request timeout

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

References RequestBuilder().

◆ 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).

References restincurl::OutDataHandler< T >::read_callback(), and RequestBuilder().

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).

References RequestBuilder(), and 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

References CanSendFile(), errno, and RequestBuilder().

◆ 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

References CanSendFile(), errno, and RequestBuilder().

◆ 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

References RequestBuilder().

◆ 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.

References RequestBuilder().

◆ 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.

References RequestBuilder().

◆ 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).

References RequestBuilder(), and restincurl::InDataHandler< T >::write_callback().

Referenced by Build(), and 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).

References RequestBuilder(), and 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.

References Option(), and RequestBuilder().

◆ 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.

References RequestBuilder().

◆ WithJson() [1/2]

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

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

References Header(), and RequestBuilder().

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.

References RequestBuilder(), SendData(), and WithJson().


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