Skip to content

Commit b8995ec

Browse files
committed
add first stab of new API to header files
this adds the new proposed API to the header files and also splits them out into distinct files.
1 parent 69cf32f commit b8995ec

3 files changed

Lines changed: 219 additions & 94 deletions

File tree

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/**
2+
* @file connection.h
3+
* @brief header definitions for restclient-cpp connection class
4+
* @author Daniel Schauenberg <[email protected]>
5+
* @version
6+
* @date 2010-10-11
7+
*/
8+
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+
17+
#include "restclient-cpp/version.h"
18+
19+
/**
20+
* @brief namespace for all RestClient definitions
21+
*/
22+
namespace RestClient {
23+
24+
/**
25+
* @brief Connection object for advanced usage
26+
*/
27+
class Connection {
28+
public:
29+
/**
30+
* @brief enum to hold identifiers for HTTP verbs
31+
*/
32+
enum HttpVerb {
33+
GET,
34+
POST,
35+
PUT,
36+
DELETE
37+
};
38+
39+
/**
40+
* @struct Info
41+
* @brief holds some diagnostics information
42+
* about the connection object it came from
43+
*/
44+
typedef struct {
45+
std::string base_url;
46+
HeaderFields headers;
47+
int timeout;
48+
struct {
49+
std::string username;
50+
std::string password;
51+
} basicAuth;
52+
std::string customUserAgent;
53+
} Info;
54+
55+
56+
explicit Connection(const std::string& baseUrl);
57+
~Connection();
58+
59+
// Instance configuration methods
60+
// configure basic auth
61+
void SetBasicAuth(const std::string& username,
62+
const std::string& password);
63+
64+
// set connection timeout to 5s
65+
void SetTimeout(int seconds);
66+
67+
// set custom user agent
68+
// (this will result in the UA "foo/cool restclient-cpp/VERSION")
69+
void SetUserAgent(const std::string& userAgent);
70+
71+
// set headers
72+
void SetHeaders(headermap headers);
73+
74+
// append additional headers
75+
void AppendHeader(const std::string& key,
76+
const std::string& value);
77+
78+
79+
// Basic HTTP verb methods
80+
response get(const std::string& uri);
81+
response post(const std::string& uri,
82+
const std::string& contentType,
83+
const std::string& data);
84+
response put(const std::string& uri,
85+
const std::string& contentType,
86+
const std::string& data);
87+
response del(const std::string& uri);
88+
89+
private:
90+
CURL* curlHandle = NULL;
91+
std::string baseUrl;
92+
headermap headers;
93+
int timeout;
94+
struct {
95+
std::string username;
96+
std::string password;
97+
} basicAuth;
98+
std::string customUserAgent;
99+
};
100+
}; // namespace RestClient
101+
102+
#endif // INCLUDE_RESTCLIENT_CPP_CONNECTION_H_

include/restclient-cpp/helpers.h

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/**
2+
* @file helpers.h
3+
* @brief header file for restclient-cpp helpers
4+
* @author Daniel Schauenberg <[email protected]>
5+
* @version
6+
* @date 2010-10-11
7+
*/
8+
9+
#ifndef INCLUDE_RESTCLIENT_CPP_HELPERS_H_
10+
#define INCLUDE_RESTCLIENT_CPP_HELPERS_H_
11+
12+
#include <string>
13+
#include <algorithm>
14+
#include <functional>
15+
16+
#include "restclient-cpp/version.h"
17+
18+
/**
19+
* @brief namespace for all RestClient definitions
20+
*/
21+
namespace RestClient {
22+
23+
/**
24+
* @brief: namespace for all helper functions
25+
*/
26+
namespace Helpers {
27+
28+
/** @struct upload_object
29+
* @brief This structure represents the payload to upload on POST
30+
* requests
31+
* @var upload_object::data
32+
* Member 'data' contains the data to upload
33+
* @var upload_object::length
34+
* Member 'length' contains the length of the data to upload
35+
*/
36+
typedef struct {
37+
const char* data;
38+
size_t length;
39+
} UploadObject;
40+
41+
// writedata callback function
42+
static size_t write_callback(void *ptr, size_t size, size_t nmemb,
43+
void *userdata);
44+
45+
// header callback function
46+
static size_t header_callback(void *ptr, size_t size, size_t nmemb,
47+
void *userdata);
48+
// read callback function
49+
static size_t read_callback(void *ptr, size_t size, size_t nmemb,
50+
void *userdata);
51+
52+
// trim from start
53+
static inline std::string &ltrim(const std::string &s) {
54+
s.erase(s.begin(), std::find_if(s.begin(), s.end(),
55+
std::not1(std::ptr_fun<int, int>(std::isspace))));
56+
return s;
57+
}
58+
59+
// trim from end
60+
static inline std::string &rtrim(const std::string &s) {
61+
s.erase(std::find_if(s.rbegin(), s.rend(),
62+
std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());
63+
return s;
64+
}
65+
66+
// trim from both ends
67+
static inline std::string &trim(const std::string &s) {
68+
return ltrim(rtrim(s));
69+
}
70+
}; // namespace Helpers
71+
72+
}; // namespace RestClient
73+
74+
#endif // INCLUDE_RESTCLIENT_CPP_HELPERS_H_

include/restclient-cpp/restclient.h

Lines changed: 43 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -6,110 +6,59 @@
66
* @date 2010-10-11
77
*/
88

9-
#ifndef INCLUDE_RESTCLIENT_H_
10-
#define INCLUDE_RESTCLIENT_H_
9+
#ifndef INCLUDE_RESTCLIENT_CPP_RESTCLIENT_H_
10+
#define INCLUDE_RESTCLIENT_CPP_RESTCLIENT_H_
1111

12+
#include <curl/curl.h>
1213
#include <string>
1314
#include <map>
1415
#include <cstdlib>
15-
#include <algorithm>
1616

1717
#include "restclient-cpp/version.h"
1818

19-
class RestClient
20-
{
21-
public:
22-
/**
23-
* public data definitions
24-
*/
25-
typedef std::map<std::string, std::string> headermap;
26-
27-
/** @struct response
28-
* @brief This structure represents the HTTP response data
29-
* @var response::code
30-
* Member 'code' contains the HTTP response code
31-
* @var response::body
32-
* Member 'body' contains the HTTP response body
33-
* @var response::headers
34-
* Member 'headers' contains the HTTP response headers
35-
*/
36-
typedef struct
37-
{
38-
int code;
39-
std::string body;
40-
headermap headers;
41-
} response;
42-
43-
/** @struct upload_object
44-
* @brief This structure represents the payload to upload on POST
45-
* requests
46-
* @var upload_object::data
47-
* Member 'data' contains the data to upload
48-
* @var upload_object::length
49-
* Member 'length' contains the length of the data to upload
50-
*/
51-
typedef struct
52-
{
53-
const char* data;
54-
size_t length;
55-
} upload_object;
56-
57-
/** public methods */
58-
// Auth
59-
static void clearAuth();
60-
static void setAuth(const std::string& user,const std::string& password);
61-
// HTTP GET
62-
static response get(const std::string& url, const size_t timeout = 0);
63-
static response get(const std::string& url, const headermap& headers,
64-
const size_t timeout = 0);
65-
// HTTP POST
66-
static response post(const std::string& url, const std::string& ctype,
67-
const std::string& data, const size_t timeout = 0);
68-
static response post(const std::string& url, const std::string& ctype,
69-
const std::string& data, const headermap& headers,
70-
const size_t timeout = 0);
71-
// HTTP PUT
72-
static response put(const std::string& url, const std::string& ctype,
73-
const std::string& data, const size_t timeout = 0);
74-
static response put(const std::string& url, const std::string& ctype,
75-
const std::string& data, const headermap& headers,
76-
const size_t timeout = 0);
77-
// HTTP DELETE
78-
static response del(const std::string& url, const size_t timeout = 0);
79-
static response del(const std::string& url, const headermap& headers,
80-
const size_t timeout = 0);
81-
82-
private:
83-
// writedata callback function
84-
static size_t write_callback(void *ptr, size_t size, size_t nmemb,
85-
void *userdata);
19+
/**
20+
* @brief namespace for all RestClient definitions
21+
*/
22+
namespace RestClient {
8623

87-
// header callback function
88-
static size_t header_callback(void *ptr, size_t size, size_t nmemb,
89-
void *userdata);
90-
// read callback function
91-
static size_t read_callback(void *ptr, size_t size, size_t nmemb,
92-
void *userdata);
93-
static const char* user_agent;
94-
static std::string user_pass;
24+
/**
25+
* public data definitions
26+
*/
27+
typedef std::map<std::string, std::string> HeaderFields;
9528

96-
// trim from start
97-
static inline std::string &ltrim(std::string &s) {
98-
s.erase(s.begin(), std::find_if(s.begin(), s.end(), std::not1(std::ptr_fun<int, int>(std::isspace))));
99-
return s;
100-
}
29+
/** @struct response
30+
* @brief This structure represents the HTTP response data
31+
* @var response::code
32+
* Member 'code' contains the HTTP response code
33+
* @var response::body
34+
* Member 'body' contains the HTTP response body
35+
* @var response::headers
36+
* Member 'headers' contains the HTTP response headers
37+
*/
38+
typedef struct {
39+
int code;
40+
std::string body;
41+
HeaderFields headers;
42+
} Response;
10143

102-
// trim from end
103-
static inline std::string &rtrim(std::string &s) {
104-
s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());
105-
return s;
106-
}
44+
// init function
45+
void init();
46+
void disable();
10747

108-
// trim from both ends
109-
static inline std::string &trim(std::string &s) {
110-
return ltrim(rtrim(s));
111-
}
48+
/**
49+
* public methods for the simple API. These don't allow a lot of
50+
* configuration but are meant for simple HTTP calls.
51+
*
52+
*/
53+
Response get(const std::string& url);
54+
Response post(const std::string& url,
55+
const std::string& content_type,
56+
const std::string& data);
57+
Response put(const std::string& url,
58+
const std::string& content_type,
59+
const std::string& data);
60+
Response del(const std::string& url);
11261

113-
};
62+
}; // namespace RestClient
11463

115-
#endif // INCLUDE_RESTCLIENT_H_
64+
#endif // INCLUDE_RESTCLIENT_CPP_RESTCLIENT_H_

0 commit comments

Comments
 (0)