Skip to content
This repository was archived by the owner on Mar 17, 2025. It is now read-only.

Commit 6a78932

Browse files
committed
Added remove() method.
1 parent 615dfe4 commit 6a78932

File tree

2 files changed

+30
-10
lines changed

2 files changed

+30
-10
lines changed

Firebase.cpp

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,16 @@ Firebase& Firebase::auth(const String& auth) {
2828
}
2929

3030
String Firebase::get(const String& path) {
31-
return sendRequest("GET", path);
31+
return sendRequestGetBody("GET", path);
3232
}
3333

3434
String Firebase::push(const String& path, const String& value) {
35-
return sendRequest("POST", path, value);
35+
return sendRequestGetBody("POST", path, value);
36+
}
37+
38+
bool Firebase::remove(const String& path) {
39+
int status = sendRequest("DELETE", path);
40+
return status == HTTP_CODE_OK;
3641
}
3742

3843
Firebase& Firebase::stream(const String& path) {
@@ -74,21 +79,32 @@ String Firebase::makeURL(const String& path) {
7479
return url;
7580
}
7681

77-
String Firebase::sendRequest(const char* method, const String& path, const String& value) {
78-
_error.reset();
82+
int Firebase::sendRequest(const char* method, const String& path, const String& value) {
7983
String url = makeURL(path);
8084
_http.begin(_host.c_str(), firebasePort, url.c_str(), true, firebaseFingerprint);
81-
int statusCode = _http.sendRequest(method, (uint8_t*)value.c_str(), value.length());
82-
if (statusCode < 0) {
83-
_error.set(statusCode,
84-
String(method) + " " + url + ": "
85-
+ HTTPClient::errorToString(statusCode));
85+
int statusCode = _http.sendRequest(method, (uint8_t*)value.c_str(), value.length());
86+
setError(method, url, statusCode);
87+
return statusCode;
88+
}
89+
90+
String Firebase::sendRequestGetBody(const char* method, const String& path, const String& value) {
91+
sendRequest(method, path, value);
92+
if (_error.code() != 0) {
8693
return "";
8794
}
8895
// no _http.end() because of connection reuse.
8996
return _http.getString();
9097
}
9198

99+
void Firebase::setError(const char* method, const String& url, int statusCode) {
100+
_error.reset();
101+
if (statusCode < 0) {
102+
_error.set(statusCode,
103+
String(method) + " " + url + ": "
104+
+ HTTPClient::errorToString(statusCode));
105+
}
106+
}
107+
92108
bool Firebase::connected() {
93109
return _http.connected();
94110
}

Firebase.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ class Firebase {
5252
}
5353
String get(const String& path);
5454
String push(const String& path, const String& value);
55+
bool remove(const String& path);
5556
bool connected();
5657
Firebase& stream(const String& path);
5758
bool available();
@@ -63,7 +64,10 @@ class Firebase {
6364
Event read(String& event);
6465
private:
6566
String makeURL(const String& path);
66-
String sendRequest(const char* method, const String& path, const String& value = "");
67+
int sendRequest(const char* method, const String& path, const String& value = "");
68+
String sendRequestGetBody(const char* method, const String& path, const String& value = "");
69+
void setError(const char* method, const String& url, int status_code);
70+
6771
HTTPClient _http;
6872
String _host;
6973
String _auth;

0 commit comments

Comments
 (0)