TDME2  1.9.200
HTTPClient.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <sstream>
4 #include <unordered_map>
5 #include <vector>
6 
7 #include <tdme/tdme.h>
11 
12 using std::string;
13 using std::stringstream;
14 using std::unordered_map;
15 using std::vector;
16 
19 
20 /**
21  * Basic HTTP client
22  * @author Andreas Drewke
23  */
25 
26 private:
27  string url;
28  string method;
29  unordered_map<string, string> headers;
30  unordered_map<string, string> getParameters;
31  unordered_map<string, string> postParameters;
32  string body;
33  string contentType;
34  string username;
35  string password;
36 
37  stringstream rawResponse;
38  int16_t statusCode { -1 };
39  unordered_map<string, string> responseHeaders;
40 
41  /**
42  * Returns a URL encoded representation of value
43  * @param value value
44  * @return URL encoded value
45  */
46  static string urlEncode(const string& value);
47 
48  /**
49  * Create HTTP request headers
50  * @param hostname hostname
51  * @param relativeUrl url relative to server root
52  * @param body body
53  */
54  string createHTTPRequestHeaders(const string& hostname, const string& relativeUrl, const string& body);
55 
56  /**
57  * Parse HTTP response headers
58  * @param rawResponse raw response
59  */
60  void parseHTTPResponseHeaders(stringstream& rawResponse);
61 
62 public:
63  static const constexpr int16_t HTTP_STATUSCODE_OK { 200 };
64 
65  STATIC_DLL_IMPEXT static const string HTTP_METHOD_GET;
66  STATIC_DLL_IMPEXT static const string HTTP_METHOD_HEAD;
67  STATIC_DLL_IMPEXT static const string HTTP_METHOD_POST;
68  STATIC_DLL_IMPEXT static const string HTTP_METHOD_PUT;
70 
71  /**
72  * Get URL
73  * @return URL
74  */
75  inline const string& getURL() {
76  return url;
77  }
78 
79  /**
80  * Set URL
81  * @param url URL
82  */
83  inline void setURL(const string& url) {
84  this->url = url;
85  }
86 
87  /**
88  * Get method
89  * @return method
90  */
91  inline const string& getMethod() {
92  return method;
93  }
94 
95  /**
96  * Set method
97  * @param method method
98  */
99  inline void setMethod(const string& method) {
100  this->method = method;
101  }
102 
103  /**
104  * Get username
105  * @return username
106  */
107  inline const string& getUsername() {
108  return username;
109  }
110 
111  /**
112  * Set username
113  * @param username user name
114  */
115  inline void setUsername(const string& username) {
116  this->username = username;
117  }
118 
119  /**
120  * Get password
121  * @return password
122  */
123  inline const string& getPassword() {
124  return password;
125  }
126 
127  /**
128  * Set password
129  * @param password password
130  */
131  inline void setPassword(const string& password) {
132  this->password = password;
133  }
134 
135  /**
136  * Get request headers
137  * @return request headers
138  */
139  inline const unordered_map<string, string>& getHeaders() {
140  return headers;
141  }
142 
143  /**
144  * Set request headers
145  * @param headers request headers
146  */
147  inline void setHeaders(const unordered_map<string, string>& headers) {
148  this->headers = headers;
149  }
150 
151  /**
152  * Get GET parameter
153  * @return GET parameter
154  */
155  inline const unordered_map<string, string>& getGETParameters() {
156  return getParameters;
157  }
158 
159  /**
160  * Set GET parameter
161  * @param GET parameter
162  */
163  inline void setGETParameters(const unordered_map<string, string>& parameters) {
164  this->getParameters = parameters;
165  }
166 
167  /**
168  * Get POST parameter
169  * @return POST parameter
170  */
171  inline const unordered_map<string, string>& getPOSTParameters() {
172  return postParameters;
173  }
174 
175  /**
176  * Set POST parameter
177  * @param POST parameter
178  */
179  inline void setPOSTParameters(const unordered_map<string, string>& parameters) {
180  this->postParameters = parameters;
181  }
182 
183  /**
184  * Get body
185  * @return body
186  */
187  inline const string& getBody() {
188  return body;
189  }
190 
191  /**
192  * Set body
193  * @param contentType content type
194  * @param body body
195  */
196  inline void setBody(const string& contentType, const string& body) {
197  this->contentType = contentType;
198  this->body = body;
199  }
200 
201  /**
202  * Get content type
203  * @return content type
204  */
205  inline const string& getContentType() {
206  return contentType;
207  }
208 
209  /**
210  * Set content type
211  * @param contentType content type
212  */
213  inline void setContentType(const string& contentType) {
214  this->contentType = contentType;
215  }
216 
217 
218  /**
219  * Reset this HTTP client
220  */
221  void reset();
222 
223  /**
224  * Execute HTTP request
225  * @throws tdme::network::httpclient::HTTPClientException
226  * @throws tdme::os::network::NetworkException
227  */
228  void execute();
229 
230  /**
231  * @return complete response stream
232  */
233  inline stringstream& getResponse() {
234  return rawResponse;
235  }
236 
237  /**
238  * @return HTTP status code
239  */
240  inline int16_t getStatusCode() {
241  return statusCode;
242  }
243 
244  /**
245  * @return response headers
246  */
247  inline const unordered_map<string, string>& getResponseHeaders() {
248  return responseHeaders;
249  }
250 
251 };
void setBody(const string &contentType, const string &body)
Set body.
Definition: HTTPClient.h:196
unordered_map< string, string > headers
Definition: HTTPClient.h:29
static STATIC_DLL_IMPEXT const string HTTP_METHOD_GET
Definition: HTTPClient.h:65
void setContentType(const string &contentType)
Set content type.
Definition: HTTPClient.h:213
static STATIC_DLL_IMPEXT const string HTTP_METHOD_DELETE
Definition: HTTPClient.h:69
const string & getUsername()
Get username.
Definition: HTTPClient.h:107
void setPassword(const string &password)
Set password.
Definition: HTTPClient.h:131
void setPOSTParameters(const unordered_map< string, string > &parameters)
Set POST parameter.
Definition: HTTPClient.h:179
const unordered_map< string, string > & getResponseHeaders()
Definition: HTTPClient.h:247
void execute()
Execute HTTP request.
Definition: HTTPClient.cpp:179
static STATIC_DLL_IMPEXT const string HTTP_METHOD_POST
Definition: HTTPClient.h:67
const unordered_map< string, string > & getHeaders()
Get request headers.
Definition: HTTPClient.h:139
static STATIC_DLL_IMPEXT const string HTTP_METHOD_PUT
Definition: HTTPClient.h:68
unordered_map< string, string > getParameters
Definition: HTTPClient.h:30
const unordered_map< string, string > & getGETParameters()
Get GET parameter.
Definition: HTTPClient.h:155
void setGETParameters(const unordered_map< string, string > &parameters)
Set GET parameter.
Definition: HTTPClient.h:163
static STATIC_DLL_IMPEXT const string HTTP_METHOD_HEAD
Definition: HTTPClient.h:66
void setUsername(const string &username)
Set username.
Definition: HTTPClient.h:115
void setMethod(const string &method)
Set method.
Definition: HTTPClient.h:99
unordered_map< string, string > responseHeaders
Definition: HTTPClient.h:39
unordered_map< string, string > postParameters
Definition: HTTPClient.h:31
static string urlEncode(const string &value)
Returns a URL encoded representation of value.
Definition: HTTPClient.cpp:58
const string & getMethod()
Get method.
Definition: HTTPClient.h:91
void setHeaders(const unordered_map< string, string > &headers)
Set request headers.
Definition: HTTPClient.h:147
void reset()
Reset this HTTP client.
Definition: HTTPClient.cpp:165
const string & getURL()
Get URL.
Definition: HTTPClient.h:75
string createHTTPRequestHeaders(const string &hostname, const string &relativeUrl, const string &body)
Create HTTP request headers.
Definition: HTTPClient.cpp:83
const string & getPassword()
Get password.
Definition: HTTPClient.h:123
const unordered_map< string, string > & getPOSTParameters()
Get POST parameter.
Definition: HTTPClient.h:171
const string & getContentType()
Get content type.
Definition: HTTPClient.h:205
static constexpr const int16_t HTTP_STATUSCODE_OK
Definition: HTTPClient.h:63
void setURL(const string &url)
Set URL.
Definition: HTTPClient.h:83
void parseHTTPResponseHeaders(stringstream &rawResponse)
Parse HTTP response headers.
Definition: HTTPClient.cpp:125
const string & getBody()
Get body.
Definition: HTTPClient.h:187
Base exception class for network exceptions.
#define STATIC_DLL_IMPEXT
Definition: tdme.h:15