iFun Engine API  1.0.0-b2821
Great Technology for Great Games
 All Classes Files Functions Variables Typedefs Macros
http_client.h
Go to the documentation of this file.
1 // Copyright (C) 2013-2015 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 
20 namespace fun {
21 
22 // Hidden implementation detail.
23 namespace impl {
24 class HttpClient;
25 } // namespace impl
26 
27 
29 class HttpClient : public boost::enable_shared_from_this<HttpClient>,
30  private boost::noncopyable {
31  public:
32  DECLARE_CLASS_PTR(HttpClient);
33 
35  typedef function<void(const CURLcode, const http::Response &)> Handler;
36 
38  HttpClient();
39 
41  ~HttpClient();
42 
47  void SetHeader(const string &key, const string &value);
48 
51  void UnsetHeader(const string &key);
52 
54  void ResetHeader();
55 
59  CURLcode Get(const string &url);
60 
62  // Content type is not set by this function.
66  CURLcode Post(const string &url, const Ptr<const string> &data);
67 
73  CURLcode Post(const string &url, const Json &data);
74 
76  // Content type is not set by this function.
79  // the callee takes the ownership of the pointer.
81  CURLcode Post(const string &url, const curl_httppost *data);
82 
84  // Content type is not set by this function.
88  CURLcode Put(const string &url, const Ptr<const string> &data);
89 
95  CURLcode Put(const string &url, const Json &data);
96 
100  void GetAsync(const string &url, const Handler &handler,
101  size_t timeout_in_ms = 0);
102 
107  void PostAsync(const string &url, const Ptr<const string> &data,
108  const Handler &handler, size_t timeout_in_ms = 0);
109 
115  void PostAsync(const string &url, const Json &data,
116  const Handler &handler, size_t timeout_in_ms = 0);
117 
122  // the callee takes the ownership of the pointer.
124  void PostAsync(const string &url, const curl_httppost *data,
125  const Handler &handler, size_t timeout_in_ms = 0);
126 
131  void PutAsync(const string &url, const Ptr<const string> &data,
132  const Handler &handler, size_t timeout_in_ms = 0);
133 
139  void PutAsync(const string &url, const Json &data,
140  const Handler &handler, size_t timeout_in_ms = 0);
141 
144  size_t GetPendingRequestCount();
145 
148  const http::Response &response() const;
149 
154  CURL *handle();
155 
158  void set_verbose(bool on);
159 
160  private:
161  Ptr<impl::HttpClient> impl_;
162 };
163 
164 } // namespace fun
165 
166 
167 #endif // INCLUDE_FUNAPI_NETWORK_HTTP_CLIENT_H_
void set_verbose(bool on)
Turns on/off verbose logging.
size_t GetPendingRequestCount()
Gets the number of pending requests.
void UnsetHeader(const string &key)
Removes a header item already set.
const http::Response & response() const
Returns a stored response.
CURLcode Get(const string &url)
Initiates a GET request synchronously.
void ResetHeader()
Reset the header to the default state.
function< void(const CURLcode, const http::Response &)> Handler
Handler type for asynchronous operations.
Definition: http_client.h:35
void PostAsync(const string &url, const Ptr< const string > &data, const Handler &handler, size_t timeout_in_ms=0)
Initiates a POST request asynchronously.
CURLcode Put(const string &url, const Ptr< const string > &data)
Initiates a PUT request synchronously.
void SetHeader(const string &key, const string &value)
Set HTTP header.
CURLcode Post(const string &url, const Ptr< const string > &data)
Initiates a POST request synchronously.
~HttpClient()
Destructor.
Data structure to hold HTTP response.
Definition: http_util.h:136
void GetAsync(const string &url, const Handler &handler, size_t timeout_in_ms=0)
Initiates a GET request asynchronously.
Definition: json.h:27
Curl-based HTTP(s) client.
Definition: http_client.h:29
HttpClient()
Constructor.
CURL * handle()
Returns an underlying curl handle.
void PutAsync(const string &url, const Ptr< const string > &data, const Handler &handler, size_t timeout_in_ms=0)
Initiates a PUT request asynchronously.