Skip to content

Commit 952d01b

Browse files
committed
Rename HTTPPackage and HTTPPackageParser + change HTTPRequestType type
1 parent 2dda5d1 commit 952d01b

File tree

6 files changed

+30
-23
lines changed

6 files changed

+30
-23
lines changed

.circleci/config.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@ jobs:
1313
- run: pio ci --board=esp32dev examples/Values/ --lib src/
1414
- run: pio ci --board=esp32dev examples/Timer_Basic/ --lib src/
1515
- run: pio ci --board=esp32dev examples/Timer_Callbacks/ --lib src/
16-
- run: pio ci --board=esp32dev examples/HTTPPackage/ --lib src/
17-
- run: pio ci --board=esp32dev examples/HTTPParser/ --lib src/
16+
- run: pio ci --board=esp32dev examples/HTTPRequest/ --lib src/
17+
- run: pio ci --board=esp32dev examples/HTTPResponse/ --lib src/
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
// Include library
3-
#include <net/HTTPPackage.h>
3+
#include <net/HTTPRequest.h>
44

55
using namespace dream;
66

@@ -13,7 +13,7 @@ void setup()
1313
Serial.begin(115200);
1414

1515
// Create GET request
16-
HTTPPackage get = HTTPPackage(HTTPRequestType::GET, "google.com", "/")
16+
HTTPRequest get = HTTPRequest(GET, "google.com", "/")
1717
.setUserAgent("ESP")
1818
.addHeader("X-My-Header", "value")
1919
.setEmptyBody();
@@ -23,7 +23,7 @@ void setup()
2323
Serial.println();
2424

2525
// Create POST request
26-
HTTPPackage post = HTTPPackage(HTTPRequestType::POST, "eco-city.org.ua", "/")
26+
HTTPRequest post = HTTPRequest(POST, "eco-city.org.ua", "/")
2727
.setUserAgent("ESP")
2828
.setContentType("application/json");
2929

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
// Include library
3-
#include <net/HTTPPackageParser.h>
3+
#include <net/HTTPResponse.h>
44

55
using namespace dream;
66

@@ -13,7 +13,7 @@ void setup()
1313
String package = "HTTP/1.1 200 OK\r\nDate: Tue, 14 Mar 2023 11:37:03 GMT\r\nServer: Apache/2.4.38 (Debian)\r\nCache-Control: no-cache, must-revalidate\r\nExpires: Sat, 26 Jul 1997 05:00:00 GMT\r\nVary: Accept-Encoding\r\nContent-Length: 19\r\nConnection: close\r\nContent-Type: application/json\r\n\r\n{\n \"key\": true\n}";
1414

1515
// Parse package
16-
HTTPPackageParser parser = HTTPPackageParser(package);
16+
HTTPResponse parser = HTTPResponse(package);
1717

1818
// Print general values
1919
Serial.println("HTTP status: " + String(parser.getStatus()));
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,48 +22,48 @@
2222

2323
namespace dream
2424
{
25-
class HTTPPackage
25+
class HTTPRequest
2626
{
2727
protected: //-----------------------------------------------------------
2828

2929
String _buffer = "";
3030

3131
public: //--------------------------------------------------------------
3232

33-
HTTPPackage(HTTPRequestType type, const String &host, const String &url)
33+
HTTPRequest(HTTPRequestType type, const String &host, const String &url)
3434
{
3535
_buffer += HTTPTypeToString(type) + " " + url + " HTTP/1.1\r\n";
3636
_buffer += "Host: " + host + "\r\n";
3737
}
3838

3939
// Add custom header
40-
HTTPPackage& addHeader(const String &name, const String &value)
40+
HTTPRequest& addHeader(const String &name, const String &value)
4141
{
4242
_buffer += (name + ": " + value + "\r\n");
4343
return *this;
4444
}
4545

4646
// Set User-Agent header
47-
HTTPPackage& setUserAgent(const String &user_agent)
47+
HTTPRequest& setUserAgent(const String &user_agent)
4848
{
4949
return addHeader(F("User-Agent"), user_agent);
5050
}
5151

5252
// Set Content-Type header
53-
HTTPPackage& setContentType(const String &content_type)
53+
HTTPRequest& setContentType(const String &content_type)
5454
{
5555
return addHeader(F("Content-Type"), content_type);
5656
}
5757

5858
// Set BasicAuth
59-
HTTPPackage& setAuthorization(const String &user, const String &pass)
59+
HTTPRequest& setAuthorization(const String &user, const String &pass)
6060
{
6161
_buffer += "Authorization: Basic " + base64::encode(user + ":" + pass);
6262
return *this;
6363
}
6464

6565
// Add package body
66-
HTTPPackage& setBody(const String &data)
66+
HTTPRequest& setBody(const String &data)
6767
{
6868
addHeader(F("Content-Length"), String(data.length()));
6969

@@ -74,7 +74,7 @@ namespace dream
7474
}
7575

7676
// End package
77-
HTTPPackage& setEmptyBody()
77+
HTTPRequest& setEmptyBody()
7878
{
7979
_buffer += "Connection: close\r\n\r\n";
8080
return *this;

src/net/HTTPRequestType.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
namespace dream
77
{
8-
enum class HTTPRequestType
8+
enum HTTPRequestType
99
{
1010
GET,
1111
POST,
Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,12 @@
1919

2020
namespace dream
2121
{
22-
class HTTPPackageParser
22+
class HTTPResponse
2323
{
2424
private: //-------------------------------------------------------------
2525

26-
struct HTTPRequestHeader
26+
// Object of HTTP package header
27+
struct HTTPHeader
2728
{
2829
String name;
2930
String value;
@@ -36,8 +37,9 @@ namespace dream
3637
int _http_code;
3738

3839
// Headers
39-
std::vector<HTTPRequestHeader> _headers;
40+
std::vector<HTTPHeader> _headers;
4041

42+
// Some common headers as values
4143
int _content_length;
4244
String _content_type;
4345
String _location;
@@ -132,7 +134,7 @@ namespace dream
132134

133135
public: //--------------------------------------------------------------
134136

135-
HTTPPackageParser(const String &package)
137+
HTTPResponse(const String &package)
136138
{
137139
_buffer = package;
138140
_buffer.trim();
@@ -141,37 +143,42 @@ namespace dream
141143
headersDataToValues();
142144
}
143145

146+
// Get some of most common headers
144147
int getStatus() { return _http_code; }
145148
int getContentLength() { return _content_length; }
146149
String getContentType() { return _content_type; }
147150
String getLocation() { return _location; }
148151
String getDate() { return _date; }
149152

150-
std::vector<HTTPRequestHeader> getHeaders()
153+
// Get list of headers
154+
std::vector<HTTPHeader> getHeaders()
151155
{
152156
return _headers;
153157
}
154158

159+
// Check if package has header
155160
bool hasHeader(const String &name)
156161
{
157-
for (const HTTPRequestHeader &h : _headers)
162+
for (const HTTPHeader &h : _headers)
158163
{
159164
if (h.name == name) return true;
160165
}
161166

162167
return false;
163168
}
164169

170+
// Get value of header
165171
String getHeader(const String &name)
166172
{
167-
for (const HTTPRequestHeader &h : _headers)
173+
for (const HTTPHeader &h : _headers)
168174
{
169175
if (h.name == name) return h.value;
170176
}
171177

172178
return "";
173179
}
174180

181+
// Get body data
175182
String getBody()
176183
{
177184
return _body;

0 commit comments

Comments
 (0)