Skip to content

Commit 5082ca5

Browse files
committed
refactored, changed API
1 parent 109e1f5 commit 5082ca5

File tree

2 files changed

+77
-115
lines changed

2 files changed

+77
-115
lines changed

RestClient.cpp

Lines changed: 64 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "Client.h"
22
#include "RestClient.h"
33

4+
//#define HTTP_DEBUG true
45
#ifdef HTTP_DEBUG
56
#define HTTP_DEBUG_PRINT(string) (Serial.print(string))
67
#endif
@@ -16,6 +17,8 @@ RestClient::RestClient(Client& netClient, const char* _host) {
1617
num_headers = 0;
1718
contentType = "x-www-form-urlencoded"; // default
1819
this->client = &netClient;
20+
this->responseBody = "";
21+
this->timeout = 1000; // default. TODO: add a setter function
1922
}
2023

2124
RestClient::RestClient(Client& netClient, const char* _host, int _port) {
@@ -26,159 +29,119 @@ RestClient::RestClient(Client& netClient, const char* _host, int _port) {
2629
this->client = &netClient;
2730
}
2831

29-
30-
31-
3232
// GET path
3333
int RestClient::get(const char* path){
34-
return request("GET", path, NULL, NULL);
35-
}
36-
37-
//GET path with response
38-
int RestClient::get(const char* path, String* response){
39-
return request("GET", path, NULL, response);
34+
return request("GET", path, "");
4035
}
4136

4237
// POST path and body
43-
int RestClient::post(const char* path, const char* body){
44-
return request("POST", path, body, NULL);
45-
}
46-
47-
// POST path and body with response
48-
int RestClient::post(const char* path, const char* body, String* response){
49-
return request("POST", path, body, response);
38+
int RestClient::post(const char* path, String body){
39+
return request("POST", path, body);
5040
}
5141

5242
// PUT path and body
53-
int RestClient::put(const char* path, const char* body){
54-
return request("PUT", path, body, NULL);
55-
}
56-
57-
// PUT path and body with response
58-
int RestClient::put(const char* path, const char* body, String* response){
59-
return request("PUT", path, body, response);
43+
int RestClient::put(const char* path, String body){
44+
return request("PUT", path, body);
6045
}
6146

6247
// DELETE path
6348
int RestClient::del(const char* path){
64-
return request("DELETE", path, NULL, NULL);
65-
}
66-
67-
// DELETE path and response
68-
int RestClient::del(const char* path, String* response){
69-
return request("DELETE", path, NULL, response);
49+
return request("DELETE", path, "");
7050
}
7151

7252
// DELETE path and body
73-
int RestClient::del(const char* path, const char* body ){
74-
return request("DELETE", path, body, NULL);
75-
}
76-
77-
// DELETE path and body with response
78-
int RestClient::del(const char* path, const char* body, String* response){
79-
return request("DELETE", path, body, response);
53+
int RestClient::del(const char* path, String body ){
54+
return request("DELETE", path, body);
8055
}
8156

82-
void RestClient::write(const char* string){
83-
HTTP_DEBUG_PRINT(string);
84-
client->print(string);
85-
}
86-
87-
void RestClient::setHeader(const char* header){
57+
void RestClient::setHeader(String header){
8858
headers[num_headers] = header;
8959
num_headers++;
9060
}
9161

92-
void RestClient::setContentType(const char* contentTypeValue){
62+
void RestClient::setContentType(String contentTypeValue){
9363
contentType = contentTypeValue;
9464
}
9565

9666
// The mother- generic request method.
9767
//
98-
int RestClient::request(const char* method, const char* path,
99-
const char* body, String* response){
68+
int RestClient::request(const char* method, String path, String body){
10069

10170
HTTP_DEBUG_PRINT("HTTP: connect\n");
10271

10372
if(client->connect(host, port)){
10473
HTTP_DEBUG_PRINT("HTTP: connected\n");
10574
HTTP_DEBUG_PRINT("REQUEST: \n");
10675
// Make a HTTP request line:
107-
write(method);
108-
write(" ");
109-
write(path);
110-
write(" HTTP/1.1\r\n");
76+
client->print(method);
77+
client->print(" ");
78+
client->print(path);
79+
client->println(" HTTP/1.1");
11180
for(int i=0; i<num_headers; i++){
112-
write(headers[i]);
113-
write("\r\n");
81+
client->println(headers[i]);
11482
}
115-
write("Host: ");
116-
write(host);
117-
write("\r\n");
118-
write("Connection: close\r\n");
119-
120-
if(body != NULL){
121-
char contentLength[30];
122-
sprintf(contentLength, "Content-Length: %d\r\n", strlen(body));
123-
write(contentLength);
124-
125-
write("Content-Type: ");
126-
write(contentType);
127-
write("\r\n");
83+
client->print("Host: ");
84+
client->println(host);
85+
client->println("Connection: close");
86+
87+
// TODO: POST is not sending body right now. Not sure why not
88+
if(body != ""){
89+
//char contentLength[30];
90+
//sprintf(contentLength, "Content-Length: %d", body.length());
91+
client->print("Content-Length: ");
92+
client->println(body.length());
93+
94+
client->print("Content-Type: ");
95+
client->println(contentType);
96+
HTTP_DEBUG_PRINT(body.length());
97+
HTTP_DEBUG_PRINT(contentType);
98+
client->println(); // empty line after headers
99+
client->println(body);
100+
HTTP_DEBUG_PRINT(body);
128101
}
129-
130-
write("\r\n");
131-
132-
if(body != NULL){
133-
write(body);
134-
write("\r\n");
135-
write("\r\n");
136-
}
137-
138-
//make sure you write all those bytes.
102+
client->println();
103+
// make sure you've sent all bytes. Ugly hack.
104+
// TODO: check output buffer instead
139105
delay(100);
140106

141-
HTTP_DEBUG_PRINT("HTTP: call readResponse\n");
142-
int statusCode = readResponse(response);
143-
HTTP_DEBUG_PRINT("HTTP: return readResponse\n");
107+
HTTP_DEBUG_PRINT("HTTP: call getResponse\n");
108+
int statusCode = getResponse();
109+
HTTP_DEBUG_PRINT("HTTP: return getResponse\n");
144110

145111
//cleanup
146-
HTTP_DEBUG_PRINT("HTTP: stop client\n");
147-
num_headers = 0;
148-
client->stop();
149-
delay(50);
150-
HTTP_DEBUG_PRINT("HTTP: client stopped\n");
151-
112+
// only stop if server disconnected. Otherwise you can
113+
// get in an infinite loop in getResponse:
114+
if (!client->connected()) {
115+
HTTP_DEBUG_PRINT("HTTP: stop client\n");
116+
num_headers = 0;
117+
client->stop();
118+
HTTP_DEBUG_PRINT("HTTP: client stopped\n");
119+
}
152120
return statusCode;
153121
}else{
154122
HTTP_DEBUG_PRINT("HTTP Connection failed\n");
155123
return 0;
156124
}
157125
}
158126

159-
int RestClient::readResponse(String* response) {
160-
127+
int RestClient::getResponse() {
128+
this->requestStart = millis();
161129
// an http request ends with a blank line
162130
boolean currentLineIsBlank = true;
163131
boolean httpBody = false;
164132
boolean inStatus = false;
133+
// clear response string:
134+
this->responseBody = "";
165135

166136
char statusCode[4];
167137
int i = 0;
168138
int code = 0;
169139

170-
if(response == NULL){
171-
HTTP_DEBUG_PRINT("HTTP: NULL RESPONSE POINTER: \n");
172-
}else{
173-
HTTP_DEBUG_PRINT("HTTP: NON-NULL RESPONSE POINTER: \n");
174-
}
175-
176140
HTTP_DEBUG_PRINT("HTTP: RESPONSE: \n");
177141
while (client->connected()) {
178-
HTTP_DEBUG_PRINT(".");
179-
142+
// HTTP_DEBUG_PRINT(".");
180143
if (client->available()) {
181-
HTTP_DEBUG_PRINT(",");
144+
// HTTP_DEBUG_PRINT(",");
182145

183146
char c = client->read();
184147
HTTP_DEBUG_PRINT(c);
@@ -197,8 +160,8 @@ int RestClient::readResponse(String* response) {
197160
}
198161

199162
if(httpBody){
200-
//only write response if its not null
201-
if(response != NULL) response->concat(c);
163+
// add this char tot the responseBody
164+
this->responseBody += c;
202165
}
203166
else
204167
{
@@ -215,9 +178,13 @@ int RestClient::readResponse(String* response) {
215178
currentLineIsBlank = false;
216179
}
217180
}
181+
} else {
182+
// there is a condition where client connects
183+
// and available() always <= 0. So timeout is here to catch that:
184+
if (millis() - this->requestStart > this->timeout) return 0;
218185
}
219186
}
220187

221-
HTTP_DEBUG_PRINT("HTTP: return readResponse3\n");
188+
HTTP_DEBUG_PRINT("HTTP: return getResponse3\n");
222189
return code;
223190
}

RestClient.h

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,44 +9,39 @@ class RestClient {
99
RestClient(Client& netClient, const char* _host, int _port);
1010

1111
//Generic HTTP Request
12-
int request(const char* method, const char* path,
13-
const char* body, String* response);
12+
int request(const char* method, String path, String body);
1413
// Set a Request Header
15-
void setHeader(const char*);
14+
void setHeader(String);
1615
// Set Content-Type Header
17-
void setContentType(const char*);
16+
void setContentType(String);
1817

1918
// GET path
2019
int get(const char*);
21-
// GET path and response
22-
int get(const char*, String*);
2320

2421
// POST path and body
2522
int post(const char* path, const char* body);
26-
// POST path and body and response
27-
int post(const char* path, const char* body, String*);
2823

2924
// PUT path and body
3025
int put(const char* path, const char* body);
31-
// PUT path and body and response
32-
int put(const char* path, const char* body, String*);
3326

3427
// DELETE path
3528
int del(const char*);
3629
// DELETE path and body
3730
int del(const char*, const char*);
38-
// DELETE path and response
39-
int del(const char*, String*);
40-
// DELETE path and body and response
41-
int del(const char*, const char*, String*);
31+
// get HTTP response:
32+
String readResponse(){ return responseBody;};
33+
// TO DO: add cookie functions
4234

4335
private:
4436
Client* client;
45-
int readResponse(String*);
46-
void write(const char*);
37+
int getResponse();
38+
// void write(const char*);
4739
const char* host;
4840
int port;
4941
int num_headers;
50-
const char* headers[10];
51-
const char* contentType;
42+
String headers[10];
43+
String contentType;
44+
String responseBody; // body of the HTTP response
45+
long requestStart; // time the request started
46+
int timeout; // timeout to avoid blocking
5247
};

0 commit comments

Comments
 (0)