forked from mrtazz/restclient-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrestclient.h
More file actions
97 lines (85 loc) · 3.04 KB
/
restclient.h
File metadata and controls
97 lines (85 loc) · 3.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/**
* @file restclient.h
* @brief libcurl wrapper for REST calls
* @author Daniel Schauenberg <[email protected]>
* @version
* @date 2010-10-11
*/
#ifndef INCLUDE_RESTCLIENT_H_
#define INCLUDE_RESTCLIENT_H_
#include <string>
#include <map>
#include <cstdlib>
#include <algorithm>
class RestClient
{
public:
/**
* public data definitions
*/
typedef std::map<std::string, std::string> headermap;
/** response struct for queries */
typedef struct
{
int code;
std::string body;
headermap headers;
} response;
/** struct used for uploading data */
typedef struct
{
const char* data;
size_t length;
} upload_object;
/** public methods */
// Auth
static void clearAuth();
static void setAuth(const std::string& user,const std::string& password);
// HTTP GET
static response get(const std::string& url, const size_t timeout = 0);
static response get(const std::string& url, const headermap& headers,
const size_t timeout = 0);
// HTTP POST
static response post(const std::string& url, const std::string& ctype,
const std::string& data, const size_t timeout = 0);
static response post(const std::string& url, const std::string& ctype,
const std::string& data, const headermap& headers,
const size_t timeout = 0);
// HTTP PUT
static response put(const std::string& url, const std::string& ctype,
const std::string& data, const size_t timeout = 0);
static response put(const std::string& url, const std::string& ctype,
const std::string& data, const headermap& headers,
const size_t timeout = 0);
// HTTP DELETE
static response del(const std::string& url, const size_t timeout = 0);
static response del(const std::string& url, const headermap& headers,
const size_t timeout = 0);
private:
// writedata callback function
static size_t write_callback(void *ptr, size_t size, size_t nmemb,
void *userdata);
// header callback function
static size_t header_callback(void *ptr, size_t size, size_t nmemb,
void *userdata);
// read callback function
static size_t read_callback(void *ptr, size_t size, size_t nmemb,
void *userdata);
static const char* user_agent;
static std::string user_pass;
// trim from start
static inline std::string <rim(std::string &s) {
s.erase(s.begin(), std::find_if(s.begin(), s.end(), std::not1(std::ptr_fun<int, int>(std::isspace))));
return s;
}
// trim from end
static inline std::string &rtrim(std::string &s) {
s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());
return s;
}
// trim from both ends
static inline std::string &trim(std::string &s) {
return ltrim(rtrim(s));
}
};
#endif // INCLUDE_RESTCLIENT_H_