restclient-cpp
C++ client for making HTTP/REST requests
connection.h
Go to the documentation of this file.
1 
9 #ifndef INCLUDE_RESTCLIENT_CPP_CONNECTION_H_
10 #define INCLUDE_RESTCLIENT_CPP_CONNECTION_H_
11 
12 #include <curl/curl.h>
13 #include <string>
14 #include <map>
15 #include <cstdlib>
16 
18 #include "restclient-cpp/version.h"
19 
23 namespace RestClient {
24 
28 class Connection {
29  public:
60  typedef struct {
61  double totalTime;
63  double connectTime;
67  double redirectTime;
69  } RequestInfo;
93  typedef struct {
94  std::string baseUrl;
96  int timeout;
98  struct {
99  std::string username;
100  std::string password;
101  } basicAuth;
102 
103  std::string certPath;
104  std::string certType;
105  std::string keyPath;
106  std::string customUserAgent;
108  } Info;
109 
110 
111  explicit Connection(const std::string& baseUrl);
112  ~Connection();
113 
114  // Instance configuration methods
115  // configure basic auth
116  void SetBasicAuth(const std::string& username,
117  const std::string& password);
118 
119  // set connection timeout to seconds
120  void SetTimeout(int seconds);
121 
122  // set whether to follow redirects
123  void FollowRedirects(bool follow);
124 
125  // set custom user agent
126  // (this will result in the UA "foo/cool restclient-cpp/VERSION")
127  void SetUserAgent(const std::string& userAgent);
128 
129  // set the Certificate Authority (CA) Info which is the path to file holding
130  // certificates to be used to verify peers. See CURLOPT_CAINFO
131  void SetCAInfoFilePath(const std::string& caInfoFilePath);
132 
133  // set CURLOPT_SSLCERT
134  void SetCertPath(const std::string& cert);
135 
136  // set CURLOPT_SSLCERTTYPE
137  void SetCertType(const std::string& type);
138 
139  // set CURLOPT_SSLKEY. Default format is PEM
140  void SetKeyPath(const std::string& keyPath);
141 
142  std::string GetUserAgent();
143 
145 
146  // set headers
147  void SetHeaders(RestClient::HeaderFields headers);
148 
149  // get headers
151 
152  // append additional headers
153  void AppendHeader(const std::string& key,
154  const std::string& value);
155 
156 
157  // Basic HTTP verb methods
158  RestClient::Response get(const std::string& uri);
159  RestClient::Response post(const std::string& uri,
160  const std::string& data);
161  RestClient::Response put(const std::string& uri,
162  const std::string& data);
163  RestClient::Response del(const std::string& uri);
164  RestClient::Response head(const std::string& uri);
165 
166  private:
167  CURL* curlHandle;
168  std::string baseUrl;
169  RestClient::HeaderFields headerFields;
170  int timeout;
171  bool followRedirects;
172  struct {
173  std::string username;
174  std::string password;
175  } basicAuth;
176  std::string customUserAgent;
177  std::string caInfoFilePath;
178  RequestInfo lastRequest;
179  std::string certPath;
180  std::string certType;
181  std::string keyPath;
182  RestClient::Response performCurlRequest(const std::string& uri);
183 };
184 }; // namespace RestClient
185 
186 #endif // INCLUDE_RESTCLIENT_CPP_CONNECTION_H_
This structure represents the HTTP response data.
Definition: restclient.h:38
RestClient::HeaderFields GetHeaders()
get all custom headers set on the connection
Definition: connection.cc:105
double totalTime
Definition: connection.h:61
double preTransferTime
Definition: connection.h:65
double redirectTime
Definition: connection.h:67
RestClient::Connection::Info GetInfo()
get diagnostic information about the connection object
Definition: connection.cc:53
libcurl wrapper for REST calls
void FollowRedirects(bool follow)
configure whether to follow redirects on this connection
Definition: connection.cc:115
Connection(const std::string &baseUrl)
constructor for the Connection object
Definition: connection.cc:28
holds some diagnostics information about a request
Definition: connection.h:60
int redirectCount
Definition: connection.h:68
holds some diagnostics information about the connection object it came from
Definition: connection.h:93
RestClient::HeaderFields headers
Definition: connection.h:95
void SetUserAgent(const std::string &userAgent)
set custom user agent for connection. This gets prepended to the default restclient-cpp/RESTCLIENT_VE...
Definition: connection.cc:127
std::string GetUserAgent()
get the user agent to add to the request
Definition: connection.cc:149
void SetHeaders(RestClient::HeaderFields headers)
set the custom headers map. This will replace the currently configured headers with the provided ones...
Definition: connection.cc:91
void SetCAInfoFilePath(const std::string &caInfoFilePath)
set custom Certificate Authority (CA) path
Definition: connection.cc:139
std::map< std::string, std::string > HeaderFields
Definition: restclient.h:27
RestClient::Response post(const std::string &uri, const std::string &data)
HTTP POST method.
Definition: connection.cc:351
void SetTimeout(int seconds)
set timeout for connection
Definition: connection.cc:164
void AppendHeader(const std::string &key, const std::string &value)
append a header to the internal map
Definition: connection.cc:78
RestClient::Response del(const std::string &uri)
HTTP DELETE method.
Definition: connection.cc:399
RestClient::Response put(const std::string &uri, const std::string &data)
HTTP PUT method.
Definition: connection.cc:370
RestClient::Response head(const std::string &uri)
HTTP HEAD method.
Definition: connection.cc:417
double startTransferTime
Definition: connection.h:66
Connection object for advanced usage.
Definition: connection.h:28
bool followRedirects
Definition: connection.h:97
std::string customUserAgent
Definition: connection.h:106
std::string baseUrl
Definition: connection.h:94
double appConnectTime
Definition: connection.h:64
double connectTime
Definition: connection.h:63
double nameLookupTime
Definition: connection.h:62
RequestInfo lastRequest
Definition: connection.h:107
void SetBasicAuth(const std::string &username, const std::string &password)
set username and password for basic auth
Definition: connection.cc:176
int timeout
Definition: connection.h:96
namespace for all RestClient definitions
Definition: connection.h:23