From a40aa7d11f5569198f670acb173bc1d951034c03 Mon Sep 17 00:00:00 2001 From: tigoe Date: Mon, 22 Feb 2016 08:35:32 -0800 Subject: [PATCH 1/3] Changed name to HTTPClient --- RestClient.cpp | 24 ++++++++++---------- RestClient.h | 6 ++--- examples/DweetGet/DweetGet.ino | 8 +++---- examples/DweetPost/DweetPost.ino | 12 +++++----- examples/HueBlink/HueBlink.ino | 20 ++++++++-------- examples/SimpleDelete/SimpleDelete.ino | 11 +++++---- examples/SimpleGet/SimpleGet.ino | 11 +++++---- examples/SimplePost/SimplePost.ino | 11 +++++---- examples/SimplePut/SimplePut.ino | 11 +++++---- examples/full_test_suite/full_test_suite.ino | 8 ++++--- 10 files changed, 63 insertions(+), 59 deletions(-) diff --git a/RestClient.cpp b/RestClient.cpp index 6eac205..378b812 100644 --- a/RestClient.cpp +++ b/RestClient.cpp @@ -1,5 +1,5 @@ #include "Client.h" -#include "RestClient.h" +#include "HTTPClient.h" //#define HTTP_DEBUG true #ifdef HTTP_DEBUG @@ -11,7 +11,7 @@ #endif -RestClient::RestClient(Client& netClient, const char* _host) { +HTTPClient::HTTPClient(Client& netClient, const char* _host) { host = _host; port = 80; num_headers = 0; @@ -21,7 +21,7 @@ RestClient::RestClient(Client& netClient, const char* _host) { this->timeout = 1000; // default. TODO: add a setter function } -RestClient::RestClient(Client& netClient, const char* _host, int _port) { +HTTPClient::HTTPClient(Client& netClient, const char* _host, int _port) { host = _host; port = _port; num_headers = 0; @@ -30,42 +30,42 @@ RestClient::RestClient(Client& netClient, const char* _host, int _port) { } // GET path -int RestClient::get(String path){ +int HTTPClient::get(String path){ return request("GET", path, ""); } // POST path and body -int RestClient::post(String path, String body){ +int HTTPClient::post(String path, String body){ return request("POST", path, body); } // PUT path and body -int RestClient::put(String path, String body){ +int HTTPClient::put(String path, String body){ return request("PUT", path, body); } // DELETE path -int RestClient::del(String path){ +int HTTPClient::del(String path){ return request("DELETE", path, ""); } // DELETE path and body -int RestClient::del(String path, String body ){ +int HTTPClient::del(String path, String body ){ return request("DELETE", path, body); } -void RestClient::setHeader(String header){ +void HTTPClient::setHeader(String header){ headers[num_headers] = header; num_headers++; } -void RestClient::setContentType(String contentTypeValue){ +void HTTPClient::setContentType(String contentTypeValue){ contentType = contentTypeValue; } // The mother- generic request method. // -int RestClient::request(const char* method, String path, String body){ +int HTTPClient::request(const char* method, String path, String body){ HTTP_DEBUG_PRINT("HTTP: connect\n"); @@ -124,7 +124,7 @@ int RestClient::request(const char* method, String path, String body){ } } -int RestClient::getResponse() { +int HTTPClient::getResponse() { this->requestStart = millis(); // an http request ends with a blank line boolean currentLineIsBlank = true; diff --git a/RestClient.h b/RestClient.h index 4e0e04f..7a736e5 100644 --- a/RestClient.h +++ b/RestClient.h @@ -2,11 +2,11 @@ #include "Client.h" -class RestClient { +class HTTPClient { public: - RestClient(Client& netClient, const char* _host); - RestClient(Client& netClient, const char* _host, int _port); + HTTPClient(Client& netClient, const char* _host); + HTTPClient(Client& netClient, const char* _host, int _port); //Generic HTTP Request int request(const char* method, String path, String body); diff --git a/examples/DweetGet/DweetGet.ino b/examples/DweetGet/DweetGet.ino index d98a17b..1a43625 100644 --- a/examples/DweetGet/DweetGet.ino +++ b/examples/DweetGet/DweetGet.ino @@ -1,5 +1,5 @@ /* - Dweet.io GET client for RestClient library + Dweet.io GET client for HTTPClient library Connects to dweet.io once every ten seconds, sends a GET request and a request body. Uses SSL @@ -16,12 +16,12 @@ char pass[] = "password"; // your network password created 15 Feb 2016 - updated 16 Feb 2016 + updated 22 Feb 2016 by Tom Igoe this example is in the public domain */ -#include +#include #include #include "config.h" @@ -30,7 +30,7 @@ int port = 80; String dweetName = "scandalous-cheese-hoarder"; // use your own thing name here WiFiClient wifi; -RestClient client = RestClient(wifi, serverAddress, port); +HTTPClient client = HTTPClient(wifi, serverAddress, port); int status = WL_IDLE_STATUS; String response; int statusCode = 0; diff --git a/examples/DweetPost/DweetPost.ino b/examples/DweetPost/DweetPost.ino index cf7a987..457a837 100644 --- a/examples/DweetPost/DweetPost.ino +++ b/examples/DweetPost/DweetPost.ino @@ -1,5 +1,5 @@ /* - Dweet.io POST client for RestClient library + Dweet.io POST client for HTTPClient library Connects to dweet.io once every ten seconds, sends a POST request and a request body. Uses SSL @@ -16,15 +16,15 @@ this example is in the public domain */ -#include +#include #include #include "config.h" char serverAddress[] = "dweet.io"; // server address -int port = 80; +int port = 443; -WiFiClient wifi; -RestClient client = RestClient(wifi, serverAddress, port); +WiFiSSLClient wifi; +HTTPClient client = HTTPClient(wifi, serverAddress, port); int status = WL_IDLE_STATUS; String response; int statusCode = 0; @@ -57,7 +57,7 @@ void loop() { // assemble the body of the POST message: int sensorValue = analogRead(A0); - String postData = "{\"sensorValue\":\""; + String postData = "{\"sensorValue\":\""; postData += sensorValue; postData += "\"}"; diff --git a/examples/HueBlink/HueBlink.ino b/examples/HueBlink/HueBlink.ino index 8ca4839..d09dbf0 100644 --- a/examples/HueBlink/HueBlink.ino +++ b/examples/HueBlink/HueBlink.ino @@ -1,6 +1,6 @@ -/* HueBlink example for RestClient library +/* HueBlink example for HTTPClient library - Uses ResClient library to control Philips Hue + Uses HTTPClient library to control Philips Hue For more on Hue developer API see http://developer.meethue.com To control a light, the Hue expects a HTTP PUT request to: @@ -14,27 +14,27 @@ PUT request and the body of the request. note: WiFi SSID and password are stored in config.h file. - If it is not present, add a new tab, call it "config.h" + If it is not present, add a new tab, call it "config.h" and add the following variables: char ssid[] = "ssid"; // your network SSID (name) char pass[] = "password"; // your network password - modified 15 Feb 2016 + modified 22 Feb 2016 by Tom Igoe (tigoe) to match new API */ #include #include -#include +#include #include "config.h" int status = WL_IDLE_STATUS; // the Wifi radio's status char hueHubIP[] = "192.168.0.3"; // IP address of the HUE bridge String hueUserName = "huebridgeusername"; // hue bridge username -// make a wifi instance and a RestClient instance: +// make a wifi instance and a HTTPClient instance: WiFiClient wifi; -RestClient restClient = RestClient(wifi, hueHubIP); +HTTPClient HTTPClient = HTTPClient(wifi, hueHubIP); void setup() { @@ -81,13 +81,11 @@ void sendRequest(int light, String cmd, String value) { Serial.print("JSON command to server: "); // make the PUT request to the hub: - int statusCode = restClient.put(request, hueCmd); + int statusCode = HTTPClient.put(request, hueCmd); Serial.println(hueCmd); Serial.print("Status code from server: "); Serial.println(statusCode); Serial.print("Server response: "); - Serial.println(restClient.readResponse()); + Serial.println(HTTPClient.readResponse()); Serial.println(); } - - diff --git a/examples/SimpleDelete/SimpleDelete.ino b/examples/SimpleDelete/SimpleDelete.ino index ad9bb5c..4a35a82 100644 --- a/examples/SimpleDelete/SimpleDelete.ino +++ b/examples/SimpleDelete/SimpleDelete.ino @@ -1,20 +1,21 @@ /* - Simple DELETE client for RestClient library + Simple DELETE client for HTTPClient library Connects to server once every five seconds, sends a DELETE request and a request body note: WiFi SSID and password are stored in config.h file. - If it is not present, add a new tab, call it "config.h" + If it is not present, add a new tab, call it "config.h" and add the following variables: char ssid[] = "ssid"; // your network SSID (name) char pass[] = "password"; // your network password created 14 Feb 2016 + updated 22 Feb 2016 by Tom Igoe - + this example is in the public domain */ - #include + #include #include #include "config.h" @@ -22,7 +23,7 @@ char serverAddress[] = "192.168.0.3"; // server address int port = 8080; WiFiClient wifi; -RestClient client = RestClient(wifi, serverAddress, port); +HTTPClient client = HTTPClient(wifi, serverAddress, port); int status = WL_IDLE_STATUS; String response; int statusCode = 0; diff --git a/examples/SimpleGet/SimpleGet.ino b/examples/SimpleGet/SimpleGet.ino index eaf85c7..57f9eca 100644 --- a/examples/SimpleGet/SimpleGet.ino +++ b/examples/SimpleGet/SimpleGet.ino @@ -1,19 +1,20 @@ /* - Simple GET client for RestClient library + Simple GET client for HTTPClient library Connects to server once every five seconds, sends a GET request note: WiFi SSID and password are stored in config.h file. - If it is not present, add a new tab, call it "config.h" + If it is not present, add a new tab, call it "config.h" and add the following variables: char ssid[] = "ssid"; // your network SSID (name) char pass[] = "password"; // your network password created 14 Feb 2016 + updated 22 Feb 2016 by Tom Igoe - + this example is in the public domain */ -#include +#include #include #include "config.h" @@ -21,7 +22,7 @@ char serverAddress[] = "192.168.0.3"; // server address int port = 8080; WiFiClient wifi; -RestClient client = RestClient(wifi, serverAddress, port); +HTTPClient client = HTTPClient(wifi, serverAddress, port); int status = WL_IDLE_STATUS; String response; int statusCode = 0; diff --git a/examples/SimplePost/SimplePost.ino b/examples/SimplePost/SimplePost.ino index 2da4625..102c6fa 100644 --- a/examples/SimplePost/SimplePost.ino +++ b/examples/SimplePost/SimplePost.ino @@ -1,20 +1,21 @@ /* - Simple POST client for RestClient library + Simple POST client for HTTPClient library Connects to server once every five seconds, sends a POST request and a request body note: WiFi SSID and password are stored in config.h file. - If it is not present, add a new tab, call it "config.h" + If it is not present, add a new tab, call it "config.h" and add the following variables: char ssid[] = "ssid"; // your network SSID (name) char pass[] = "password"; // your network password created 14 Feb 2016 + updated 22 Feb 2016 by Tom Igoe - + this example is in the public domain */ -#include +#include #include #include "config.h" @@ -22,7 +23,7 @@ char serverAddress[] = "192.168.0.3"; // server address int port = 8080; WiFiClient wifi; -RestClient client = RestClient(wifi, serverAddress, port); +HTTPClient client = HTTPClient(wifi, serverAddress, port); int status = WL_IDLE_STATUS; String response; int statusCode = 0; diff --git a/examples/SimplePut/SimplePut.ino b/examples/SimplePut/SimplePut.ino index 5b9714c..9831df6 100644 --- a/examples/SimplePut/SimplePut.ino +++ b/examples/SimplePut/SimplePut.ino @@ -1,20 +1,21 @@ /* - Simple PUT client for RestClient library + Simple PUT client for HTTPClient library Connects to server once every five seconds, sends a PUT request and a request body note: WiFi SSID and password are stored in config.h file. - If it is not present, add a new tab, call it "config.h" + If it is not present, add a new tab, call it "config.h" and add the following variables: char ssid[] = "ssid"; // your network SSID (name) char pass[] = "password"; // your network password created 14 Feb 2016 + updated 22 Feb 2016 by Tom Igoe - + this example is in the public domain */ -#include +#include #include #include "config.h" @@ -22,7 +23,7 @@ char serverAddress[] = "192.168.0.3"; // server address int port = 8080; WiFiClient wifi; -RestClient client = RestClient(wifi, serverAddress, port); +HTTPClient client = HTTPClient(wifi, serverAddress, port); int status = WL_IDLE_STATUS; String response; int statusCode = 0; diff --git a/examples/full_test_suite/full_test_suite.ino b/examples/full_test_suite/full_test_suite.ino index f0bbf4f..cb44d8a 100644 --- a/examples/full_test_suite/full_test_suite.ino +++ b/examples/full_test_suite/full_test_suite.ino @@ -1,13 +1,15 @@ -/* RestClient full test suite +/* HTTPClient full test suite Every REST method is called. by Chris Continanza (csquared) modified by Massimo Banzi (mbanzi) to support more network devices modified by Tom Igoe to match new API + updated 22 Feb 2016 + by Tom Igoe */ -#include +#include #include #include "config.h" @@ -18,7 +20,7 @@ char serverAddress[] = "192.168.0.3"; // server address int port = 8080; WiFiClient wifi; -RestClient client = RestClient(wifi, serverAddress, port); +HTTPClient client = HTTPClient(wifi, serverAddress, port); int status = WL_IDLE_STATUS; void setup() { From b8c736e95cfbd28597400598f3361e838cfeddb8 Mon Sep 17 00:00:00 2001 From: tigoe Date: Mon, 22 Feb 2016 08:36:16 -0800 Subject: [PATCH 2/3] Changed file names --- RestClient.cpp => HTTPClient.cpp | 0 RestClient.h => HTTPClient.h | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename RestClient.cpp => HTTPClient.cpp (100%) rename RestClient.h => HTTPClient.h (100%) diff --git a/RestClient.cpp b/HTTPClient.cpp similarity index 100% rename from RestClient.cpp rename to HTTPClient.cpp diff --git a/RestClient.h b/HTTPClient.h similarity index 100% rename from RestClient.h rename to HTTPClient.h From 5d3d9d9e9ece7bb76a081dd3aba1357b12d95ea1 Mon Sep 17 00:00:00 2001 From: tigoe Date: Mon, 22 Feb 2016 09:59:13 -0800 Subject: [PATCH 3/3] Fixed error in Hue example --- examples/HueBlink/HueBlink.ino | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/examples/HueBlink/HueBlink.ino b/examples/HueBlink/HueBlink.ino index d09dbf0..3af7d13 100644 --- a/examples/HueBlink/HueBlink.ino +++ b/examples/HueBlink/HueBlink.ino @@ -34,7 +34,7 @@ String hueUserName = "huebridgeusername"; // hue bridge username // make a wifi instance and a HTTPClient instance: WiFiClient wifi; -HTTPClient HTTPClient = HTTPClient(wifi, hueHubIP); +HTTPClient client = HTTPClient(wifi, hueHubIP); void setup() { @@ -81,11 +81,11 @@ void sendRequest(int light, String cmd, String value) { Serial.print("JSON command to server: "); // make the PUT request to the hub: - int statusCode = HTTPClient.put(request, hueCmd); + int statusCode = client.put(request, hueCmd); Serial.println(hueCmd); Serial.print("Status code from server: "); Serial.println(statusCode); Serial.print("Server response: "); - Serial.println(HTTPClient.readResponse()); + Serial.println(client.readResponse()); Serial.println(); }