HttpClient.h
Go to the documentation of this file.
1 /****
2  * Sming Framework Project - Open Source framework for high efficiency native ESP8266 development.
3  * Created 2015 by Skurydin Alexey
4  * http://github.com/SmingHub/Sming
5  * All files of the Sming Core are provided under the LGPL v3 license.
6  *
7  * HttpClient.h
8  *
9  * Modified: 2017 - Slavey Karadzhov <slav@attachix.com>
10  *
11  ****/
12 
19 #pragma once
20 
21 #include "TcpClient.h"
22 #include "Http/HttpCommon.h"
23 #include "Http/HttpRequest.h"
25 
27 {
28 public:
36  virtual ~HttpClient()
37  {
38  }
39 
40  /* High-Level Methods */
41 
42  bool sendRequest(const Url& url, RequestCompletedDelegate requestComplete)
43  {
44  return send(createRequest(url)->setMethod(HTTP_GET)->onRequestComplete(requestComplete));
45  }
46 
47  bool sendRequest(const HttpMethod method, const Url& url, const HttpHeaders& headers,
48  RequestCompletedDelegate requestComplete)
49  {
50  return send(createRequest(url)->setMethod(method)->setHeaders(headers)->onRequestComplete(requestComplete));
51  }
52 
53  bool sendRequest(const HttpMethod method, const Url& url, const HttpHeaders& headers, const String& body,
54  RequestCompletedDelegate requestComplete)
55  {
56  return send(createRequest(url)->setMethod(method)->setHeaders(headers)->setBody(body)->onRequestComplete(
57  requestComplete));
58  }
59 
60  bool downloadString(const Url& url, RequestCompletedDelegate requestComplete)
61  {
62  return send(createRequest(url)->setMethod(HTTP_GET)->onRequestComplete(requestComplete));
63  }
64 
65  bool downloadFile(const Url& url, RequestCompletedDelegate requestComplete = nullptr)
66  {
67  return downloadFile(url, nullptr, requestComplete);
68  }
69 
76  bool downloadFile(const Url& url, const String& saveFileName, RequestCompletedDelegate requestComplete = nullptr);
77 
78  /* Low Level Methods */
79 
80  /*
81  * @brief This method queues a request and sends it, once it is connected to the remote server.
82  * @param HttpRequest* request The request object will be freed inside of the method.
83  * Do not try to reuse it outside of the send method as it will lead to unpredicted results
84  *
85  * @retval bool true if the request was queued, false otherwise.
86  *
87  */
88  bool send(HttpRequest* request);
89 
92  {
93  return createRequest(url);
94  }
95 
101  {
102  return new HttpRequest(url);
103  }
104 
108  static void cleanup()
109  {
111  }
112 
113 protected:
114  String getCacheKey(const Url& url)
115  {
116  return url.Host + ':' + url.getPort();
117  }
118 
119 protected:
121  static HttpConnectionPool httpConnectionPool;
122 };
123 
static void cleanup()
Definition: HttpClient.h:108
bool sendRequest(const HttpMethod method, const Url &url, const HttpHeaders &headers, RequestCompletedDelegate requestComplete)
Definition: HttpClient.h:47
Implementation of a HashMap for owned objects, i.e. anything created with new().
Definition: ObjectMap.h:47
bool downloadString(const Url &url, RequestCompletedDelegate requestComplete)
Definition: HttpClient.h:60
Class to manage URL instance.
Definition: Url.h:66
bool sendRequest(const HttpMethod method, const Url &url, const HttpHeaders &headers, const String &body, RequestCompletedDelegate requestComplete)
Definition: HttpClient.h:53
bool send(HttpRequest *request)
Definition: HttpClient.h:26
The String class.
Definition: WString.h:136
enum http_method HttpMethod
Definition: HttpCommon.h:34
Encapsulates a set of HTTP header information.
Definition: HttpHeaders.h:96
ObjectMap< String, HttpClientConnection > HttpConnectionPool
Definition: HttpClient.h:120
int getPort() const
Obtain the actual port number to be used.
Definition: Url.h:133
HttpRequest * request(const String &url)
Definition: HttpClient.h:91
#define SMING_DEPRECATED
Definition: sming_attr.h:30
virtual ~HttpClient()
HttpClient destructor.
Definition: HttpClient.h:36
bool downloadFile(const Url &url, RequestCompletedDelegate requestComplete=nullptr)
Definition: HttpClient.h:65
void clear()
Clear the map of all entries.
Definition: ObjectMap.h:313
HttpRequest * createRequest(const Url &url)
Helper function to create a new request on a URL.
Definition: HttpClient.h:100
Definition: HttpRequest.h:35
bool sendRequest(const Url &url, RequestCompletedDelegate requestComplete)
Definition: HttpClient.h:42
static HttpConnectionPool httpConnectionPool
Definition: HttpClient.h:121
String getCacheKey(const Url &url)
Definition: HttpClient.h:114
String Host
hostname or IP address
Definition: Url.h:173