|
6 | 6 | * @date 2010-10-11 |
7 | 7 | */ |
8 | 8 |
|
9 | | -#ifndef INCLUDE_RESTCLIENT_H_ |
10 | | -#define INCLUDE_RESTCLIENT_H_ |
| 9 | +#ifndef INCLUDE_RESTCLIENT_CPP_RESTCLIENT_H_ |
| 10 | +#define INCLUDE_RESTCLIENT_CPP_RESTCLIENT_H_ |
11 | 11 |
|
| 12 | +#include <curl/curl.h> |
12 | 13 | #include <string> |
13 | 14 | #include <map> |
14 | 15 | #include <cstdlib> |
15 | | -#include <algorithm> |
16 | 16 |
|
17 | 17 | #include "restclient-cpp/version.h" |
18 | 18 |
|
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 { |
86 | 23 |
|
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; |
95 | 28 |
|
96 | | - // trim from start |
97 | | - static inline std::string <rim(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; |
101 | 43 |
|
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(); |
107 | 47 |
|
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); |
112 | 61 |
|
113 | | -}; |
| 62 | +}; // namespace RestClient |
114 | 63 |
|
115 | | -#endif // INCLUDE_RESTCLIENT_H_ |
| 64 | +#endif // INCLUDE_RESTCLIENT_CPP_RESTCLIENT_H_ |
0 commit comments