iFun Engine API  1.0.0-b6053
Great Technology for Great Games
http_client.h
Go to the documentation of this file.
1 // Copyright (C) 2013-2020 iFunFactory Inc. All Rights Reserved.
2 //
3 // This work is confidential and proprietary to iFunFactory Inc. and
4 // must not be used, disclosed, copied, or distributed without the prior
5 // consent of iFunFactory Inc.
6 
9 #ifndef INCLUDE_FUNAPI_NETWORK_HTTP_CLIENT_H_
10 #define INCLUDE_FUNAPI_NETWORK_HTTP_CLIENT_H_
11 
12 #include <boost/enable_shared_from_this.hpp>
13 #include <boost/function.hpp>
14 #include <funapi/common/json.h>
15 #include <funapi/network/curl.h>
17 #include <funapi/types.h>
18 
19 #include <deque>
20 
21 
22 namespace fun {
23 
25 class FUNAPI_DLL_VISIBILITY HttpClient
26  : public boost::enable_shared_from_this<HttpClient>,
27  private boost::noncopyable {
28  public:
29  class Impl;
30 
32 
34  typedef function<void(const CURLcode, const http::Response &)> Handler;
35  typedef function<void(const CURLcode, const string &error_desc,
36  const http::Response &)> Handler2;
37 
39  HttpClient();
40 
42  ~HttpClient();
43 
48  void SetHeader(const string &key, const string &value);
49 
52  void UnsetHeader(const string &key);
53 
55  void ResetHeader();
56 
60  CURLcode Get(const string &url, size_t timeout_in_ms = 0);
61 
63  // Content type is not set by this function.
67  CURLcode Post(const string &url, const Ptr<const string> &data,
68  size_t timeout_in_ms = 0);
69 
75  CURLcode Post(const string &url, const Json &data, size_t timeout_in_ms = 0);
76 
78  // Content type is not set by this function.
81  // the callee takes the ownership of the pointer.
83  CURLcode Post(const string &url, const curl_httppost *data,
84  size_t timeout_in_ms = 0);
85 
87  // Content type is not set by this function.
91  CURLcode Put(const string &url, const Ptr<const string> &data,
92  size_t timeout_in_ms = 0);
93 
99  CURLcode Put(const string &url, const Json &data, size_t timeout_in_ms = 0);
100 
102  // Content type is not set by this function.
106  CURLcode Delete(const string &url, const Ptr<const string> &data,
107  size_t timeout_in_ms = 0);
108 
114  CURLcode Delete(const string &url, const Json &data,
115  size_t timeout_in_ms = 0);
116 
120  void GetAsync(const string &url, const Handler &handler,
121  size_t timeout_in_ms = 0);
122 
127  void PostAsync(const string &url, const Ptr<const string> &data,
128  const Handler &handler, size_t timeout_in_ms = 0);
129 
135  void PostAsync(const string &url, const Json &data,
136  const Handler &handler, size_t timeout_in_ms = 0);
137 
142  // the callee takes the ownership of the pointer.
144  void PostAsync(const string &url, const curl_httppost *data,
145  const Handler &handler, size_t timeout_in_ms = 0);
146 
151  void PutAsync(const string &url, const Ptr<const string> &data,
152  const Handler &handler, size_t timeout_in_ms = 0);
153 
159  void PutAsync(const string &url, const Json &data,
160  const Handler &handler, size_t timeout_in_ms = 0);
161 
166  void DeleteAsync(const string &url, const Ptr<const string> &data,
167  const Handler &handler, size_t timeout_in_ms = 0);
168 
174  void DeleteAsync(const string &url, const Json &data,
175  const Handler &handler, size_t timeout_in_ms = 0);
176 
180  void GetAsync2(const string &url, const Handler2 &handler,
181  size_t timeout_in_ms = 0);
182 
187  void PostAsync2(const string &url, const Ptr<const string> &data,
188  const Handler2 &handler, size_t timeout_in_ms = 0);
189 
195  void PostAsync2(const string &url, const Json &data,
196  const Handler2 &handler, size_t timeout_in_ms = 0);
197 
202  // the callee takes the ownership of the pointer.
204  void PostAsync2(const string &url, const curl_httppost *data,
205  const Handler2 &handler, size_t timeout_in_ms = 0);
206 
211  void PutAsync2(const string &url, const Ptr<const string> &data,
212  const Handler2 &handler, size_t timeout_in_ms = 0);
213 
219  void PutAsync2(const string &url, const Json &data,
220  const Handler2 &handler, size_t timeout_in_ms = 0);
221 
226  void DeleteAsync2(const string &url, const Ptr<const string> &data,
227  const Handler2 &handler, size_t timeout_in_ms = 0);
228 
234  void DeleteAsync2(const string &url, const Json &data,
235  const Handler2 &handler, size_t timeout_in_ms = 0);
236 
239  const http::Response &response() const;
240 
243  const string &error_message() const;
244 
249  CURL *handle();
250 
253  void set_verbose(bool on);
254 
255  private:
256  Ptr<Impl> impl_;
257 };
258 
259 } // namespace fun
260 
261 
262 #endif // INCLUDE_FUNAPI_NETWORK_HTTP_CLIENT_H_
function< void(const CURLcode, const http::Response &)> Handler
Handler type for asynchronous operations.
Definition: http_client.h:34
Definition: json.h:18
Data structure to hold HTTP response.
Definition: http_util.h:137
Definition: json.h:27
Curl-based HTTP(s) client.
Definition: http_client.h:25
#define DECLARE_CLASS_PTR(CLS)
Utility macro to forward-declare smart pointer types for a given class.
Definition: types.h:89