From 6a789326a8606e0f89ddff50ae18e15ddbb632b3 Mon Sep 17 00:00:00 2001 From: Ed Coyne Date: Tue, 12 Jan 2016 09:17:06 -0800 Subject: [PATCH 1/4] Added remove() method. --- Firebase.cpp | 34 +++++++++++++++++++++++++--------- Firebase.h | 6 +++++- 2 files changed, 30 insertions(+), 10 deletions(-) diff --git a/Firebase.cpp b/Firebase.cpp index 6f41b2f2..63d3188e 100644 --- a/Firebase.cpp +++ b/Firebase.cpp @@ -28,11 +28,16 @@ Firebase& Firebase::auth(const String& auth) { } String Firebase::get(const String& path) { - return sendRequest("GET", path); + return sendRequestGetBody("GET", path); } String Firebase::push(const String& path, const String& value) { - return sendRequest("POST", path, value); + return sendRequestGetBody("POST", path, value); +} + +bool Firebase::remove(const String& path) { + int status = sendRequest("DELETE", path); + return status == HTTP_CODE_OK; } Firebase& Firebase::stream(const String& path) { @@ -74,21 +79,32 @@ String Firebase::makeURL(const String& path) { return url; } -String Firebase::sendRequest(const char* method, const String& path, const String& value) { - _error.reset(); +int Firebase::sendRequest(const char* method, const String& path, const String& value) { String url = makeURL(path); _http.begin(_host.c_str(), firebasePort, url.c_str(), true, firebaseFingerprint); - int statusCode = _http.sendRequest(method, (uint8_t*)value.c_str(), value.length()); - if (statusCode < 0) { - _error.set(statusCode, - String(method) + " " + url + ": " - + HTTPClient::errorToString(statusCode)); + int statusCode = _http.sendRequest(method, (uint8_t*)value.c_str(), value.length()); + setError(method, url, statusCode); + return statusCode; +} + +String Firebase::sendRequestGetBody(const char* method, const String& path, const String& value) { + sendRequest(method, path, value); + if (_error.code() != 0) { return ""; } // no _http.end() because of connection reuse. return _http.getString(); } +void Firebase::setError(const char* method, const String& url, int statusCode) { + _error.reset(); + if (statusCode < 0) { + _error.set(statusCode, + String(method) + " " + url + ": " + + HTTPClient::errorToString(statusCode)); + } +} + bool Firebase::connected() { return _http.connected(); } diff --git a/Firebase.h b/Firebase.h index b4104b56..3b2a01e4 100644 --- a/Firebase.h +++ b/Firebase.h @@ -52,6 +52,7 @@ class Firebase { } String get(const String& path); String push(const String& path, const String& value); + bool remove(const String& path); bool connected(); Firebase& stream(const String& path); bool available(); @@ -63,7 +64,10 @@ class Firebase { Event read(String& event); private: String makeURL(const String& path); - String sendRequest(const char* method, const String& path, const String& value = ""); + int sendRequest(const char* method, const String& path, const String& value = ""); + String sendRequestGetBody(const char* method, const String& path, const String& value = ""); + void setError(const char* method, const String& url, int status_code); + HTTPClient _http; String _host; String _auth; From 99853429eda2c522c5b529f139ed524ba1e9f396 Mon Sep 17 00:00:00 2001 From: Ed Coyne Date: Wed, 13 Jan 2016 10:53:14 -0800 Subject: [PATCH 2/4] rename setError to checkResponse --- Firebase.cpp | 4 ++-- Firebase.h | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Firebase.cpp b/Firebase.cpp index 63d3188e..2e04d006 100644 --- a/Firebase.cpp +++ b/Firebase.cpp @@ -83,7 +83,7 @@ int Firebase::sendRequest(const char* method, const String& path, const String& String url = makeURL(path); _http.begin(_host.c_str(), firebasePort, url.c_str(), true, firebaseFingerprint); int statusCode = _http.sendRequest(method, (uint8_t*)value.c_str(), value.length()); - setError(method, url, statusCode); + checkResponse(method, url, statusCode); return statusCode; } @@ -96,7 +96,7 @@ String Firebase::sendRequestGetBody(const char* method, const String& path, cons return _http.getString(); } -void Firebase::setError(const char* method, const String& url, int statusCode) { +void Firebase::checkResponse(const char* method, const String& url, int statusCode) { _error.reset(); if (statusCode < 0) { _error.set(statusCode, diff --git a/Firebase.h b/Firebase.h index 3b2a01e4..37b31905 100644 --- a/Firebase.h +++ b/Firebase.h @@ -66,7 +66,7 @@ class Firebase { String makeURL(const String& path); int sendRequest(const char* method, const String& path, const String& value = ""); String sendRequestGetBody(const char* method, const String& path, const String& value = ""); - void setError(const char* method, const String& url, int status_code); + void checkResponse(const char* method, const String& url, int status_code); HTTPClient _http; String _host; From 102c84b5c2e314d9ccb40a6fd6794d7bdea6c82a Mon Sep 17 00:00:00 2001 From: Ed Coyne Date: Wed, 13 Jan 2016 10:56:11 -0800 Subject: [PATCH 3/4] Split sendRequestGetBody to GetBody --- Firebase.cpp | 9 +++++---- Firebase.h | 2 +- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/Firebase.cpp b/Firebase.cpp index 2e04d006..37da0e57 100644 --- a/Firebase.cpp +++ b/Firebase.cpp @@ -28,11 +28,13 @@ Firebase& Firebase::auth(const String& auth) { } String Firebase::get(const String& path) { - return sendRequestGetBody("GET", path); + sendRequest("GET", path); + return getBody(); } String Firebase::push(const String& path, const String& value) { - return sendRequestGetBody("POST", path, value); + sendRequest("POST", path, value); + return getBody(); } bool Firebase::remove(const String& path) { @@ -87,8 +89,7 @@ int Firebase::sendRequest(const char* method, const String& path, const String& return statusCode; } -String Firebase::sendRequestGetBody(const char* method, const String& path, const String& value) { - sendRequest(method, path, value); +String Firebase::getBody() { if (_error.code() != 0) { return ""; } diff --git a/Firebase.h b/Firebase.h index 37b31905..3c48da11 100644 --- a/Firebase.h +++ b/Firebase.h @@ -65,7 +65,7 @@ class Firebase { private: String makeURL(const String& path); int sendRequest(const char* method, const String& path, const String& value = ""); - String sendRequestGetBody(const char* method, const String& path, const String& value = ""); + String getBody(); void checkResponse(const char* method, const String& url, int status_code); HTTPClient _http; From 17a00560c76b9891a79963e5ff5d67c698e20eb2 Mon Sep 17 00:00:00 2001 From: Johan Euphrosine Date: Wed, 20 Jan 2016 12:44:37 -0800 Subject: [PATCH 4/4] firebase: void return for send request and inline checkResponse --- Firebase.cpp | 30 ++++++++++++------------------ Firebase.h | 7 +++---- 2 files changed, 15 insertions(+), 22 deletions(-) diff --git a/Firebase.cpp b/Firebase.cpp index 37da0e57..40f8dfe1 100644 --- a/Firebase.cpp +++ b/Firebase.cpp @@ -29,17 +29,16 @@ Firebase& Firebase::auth(const String& auth) { String Firebase::get(const String& path) { sendRequest("GET", path); - return getBody(); + return readBody(); } String Firebase::push(const String& path, const String& value) { sendRequest("POST", path, value); - return getBody(); + return readBody(); } -bool Firebase::remove(const String& path) { - int status = sendRequest("DELETE", path); - return status == HTTP_CODE_OK; +void Firebase::remove(const String& path) { + sendRequest("DELETE", path); } Firebase& Firebase::stream(const String& path) { @@ -81,15 +80,19 @@ String Firebase::makeURL(const String& path) { return url; } -int Firebase::sendRequest(const char* method, const String& path, const String& value) { +void Firebase::sendRequest(const char* method, const String& path, const String& value) { String url = makeURL(path); _http.begin(_host.c_str(), firebasePort, url.c_str(), true, firebaseFingerprint); int statusCode = _http.sendRequest(method, (uint8_t*)value.c_str(), value.length()); - checkResponse(method, url, statusCode); - return statusCode; + _error.reset(); + if (statusCode < 0) { + _error.set(statusCode, + String(method) + " " + url + ": " + + HTTPClient::errorToString(statusCode)); + } } -String Firebase::getBody() { +String Firebase::readBody() { if (_error.code() != 0) { return ""; } @@ -97,15 +100,6 @@ String Firebase::getBody() { return _http.getString(); } -void Firebase::checkResponse(const char* method, const String& url, int statusCode) { - _error.reset(); - if (statusCode < 0) { - _error.set(statusCode, - String(method) + " " + url + ": " - + HTTPClient::errorToString(statusCode)); - } -} - bool Firebase::connected() { return _http.connected(); } diff --git a/Firebase.h b/Firebase.h index 3c48da11..ac7ff6ca 100644 --- a/Firebase.h +++ b/Firebase.h @@ -52,7 +52,7 @@ class Firebase { } String get(const String& path); String push(const String& path, const String& value); - bool remove(const String& path); + void remove(const String& path); bool connected(); Firebase& stream(const String& path); bool available(); @@ -64,9 +64,8 @@ class Firebase { Event read(String& event); private: String makeURL(const String& path); - int sendRequest(const char* method, const String& path, const String& value = ""); - String getBody(); - void checkResponse(const char* method, const String& url, int status_code); + void sendRequest(const char* method, const String& path, const String& value = ""); + String readBody(); HTTPClient _http; String _host;