Skip to content
This repository was archived by the owner on Mar 17, 2025. It is now read-only.
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit e6bde19

Browse files
committedJan 6, 2016
Merge pull request #34 from proppy/remove-child
remove child method
2 parents 254f23a + abc0bf9 commit e6bde19

File tree

4 files changed

+38
-39
lines changed

4 files changed

+38
-39
lines changed
 

‎Firebase.cpp

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -27,25 +27,17 @@ Firebase& Firebase::auth(const String& auth) {
2727
return *this;
2828
}
2929

30-
Firebase& Firebase::child(const String& key) {
31-
_path = key;
32-
return *this;
33-
}
34-
35-
String Firebase::val() {
36-
return sendRequest("GET");
30+
String Firebase::val(const String& path) {
31+
return sendRequest("GET", path);
3732
}
3833

39-
String Firebase::push(const String& value) {
40-
return sendRequest("POST", (uint8_t*)value.c_str(), value.length());
34+
String Firebase::push(const String& path, const String& value) {
35+
return sendRequest("POST", path, value);
4136
}
4237

43-
Firebase& Firebase::stream() {
44-
_error.reset();
45-
String url = "/" + _path + ".json";
46-
if (_auth.length() > 0) {
47-
url += "?auth=" + _auth;
48-
}
38+
Firebase& Firebase::stream(const String& path) {
39+
_error.reset();
40+
String url = makeURL(path);
4941
const char* headers[] = {"Location"};
5042
_http.setReuse(true);
5143
_http.begin(_host.c_str(), firebasePort, url.c_str(), true, firebaseFingerprint);
@@ -70,14 +62,23 @@ Firebase& Firebase::stream() {
7062
return *this;
7163
}
7264

73-
String Firebase::sendRequest(const char* method, uint8_t* value, size_t size) {
74-
_error.reset();
75-
String url = "/" + _path + ".json";
65+
String Firebase::makeURL(const String& path) {
66+
String url;
67+
if (path[0] != '/') {
68+
url = "/";
69+
}
70+
url += path + ".json";
7671
if (_auth.length() > 0) {
7772
url += "?auth=" + _auth;
7873
}
74+
return url;
75+
}
76+
77+
String Firebase::sendRequest(const char* method, const String& path, const String& value) {
78+
_error.reset();
79+
String url = makeURL(path);
7980
_http.begin(_host.c_str(), firebasePort, url.c_str(), true, firebaseFingerprint);
80-
int statusCode = _http.sendRequest(method, value, size);
81+
int statusCode = _http.sendRequest(method, (uint8_t*)value.c_str(), value.length());
8182
if (statusCode < 0) {
8283
_error.set(statusCode,
8384
String(method) + " " + url + ": "

‎Firebase.h

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,13 @@ class Firebase {
4747
public:
4848
Firebase(const String& host);
4949
Firebase& auth(const String& auth);
50-
Firebase& child(const String& key);
5150
const FirebaseError& error() const {
5251
return _error;
5352
}
54-
String val();
55-
String push(const String& value);
53+
String val(const String& path);
54+
String push(const String& path, const String& value);
5655
bool connected();
57-
Firebase& stream();
56+
Firebase& stream(const String& path);
5857
bool available();
5958
enum Event {
6059
UNKNOWN,
@@ -63,11 +62,11 @@ class Firebase {
6362
};
6463
Event read(String& event);
6564
private:
66-
String sendRequest(const char* method, uint8_t* value = NULL, size_t size = 0);
65+
String makeURL(const String& path);
66+
String sendRequest(const char* method, const String& path, const String& value = "");
6767
HTTPClient _http;
6868
String _host;
6969
String _auth;
70-
String _path;
7170
FirebaseError _error;
7271
};
7372

‎examples/FirebasePush_ESP8266/FirebasePush_ESP8266.ino

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,8 @@
2020
#include <Firebase.h>
2121

2222
// create firebase client.
23-
Firebase logs = Firebase("example.firebaseio.com")
24-
.auth("secret_or_token")
25-
.child("child_node");
23+
Firebase fbase = Firebase("example.firebaseio.com")
24+
.auth("secret_or_token");
2625

2726
void setup() {
2827
Serial.begin(9600);
@@ -39,17 +38,17 @@ void setup() {
3938
Serial.println(WiFi.localIP());
4039

4140
// add a new entry.
42-
String l = logs.push("{\".sv\": \"timestamp\"}");
41+
String l = fbase.push("/logs", "{\".sv\": \"timestamp\"}");
4342
// handle error.
44-
if (logs.error()) {
43+
if (fbase.error()) {
4544
Serial.println("Firebase request failed");
46-
Serial.println(logs.error().message());
45+
Serial.println(fbase.error().message());
4746
return;
4847
}
4948
// print response.
5049
Serial.println(l);
5150
// print all entries.
52-
Serial.println(logs.val());
51+
Serial.println(fbase.val("/logs"));
5352
}
5453

5554
void loop() {

‎examples/FirebaseStream_ESP8266/FirebaseStream_ESP8266.ino

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919

2020
#include <Firebase.h>
2121

22-
Firebase node = Firebase("example.firebaseio.com")
23-
.child("child_node");
22+
Firebase fbase = Firebase("example.firebaseio.com");
23+
2424
void setup() {
2525
Serial.begin(9600);
2626

@@ -35,18 +35,18 @@ void setup() {
3535
Serial.print("connected: ");
3636
Serial.println(WiFi.localIP());
3737

38-
node.stream();
38+
fbase.stream("/chat");
3939
}
4040

4141

4242
void loop() {
43-
if (node.error()) {
43+
if (fbase.error()) {
4444
Serial.println("streaming error");
45-
Serial.println(node.error().message());
45+
Serial.println(fbase.error().message());
4646
}
47-
if (node.available()) {
47+
if (fbase.available()) {
4848
String event;
49-
auto type = node.read(event);
49+
auto type = fbase.read(event);
5050
Serial.print("event: ");
5151
Serial.println(type);
5252
if (type != Firebase::Event::UNKNOWN) {

0 commit comments

Comments
 (0)
This repository has been archived.