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

add typed push and set. #80

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 5 additions & 22 deletions examples/FirebasePush_ESP8266/FirebasePush_ESP8266.ino
Original file line number Diff line number Diff line change
Expand Up @@ -35,29 +35,12 @@ void setup() {
Serial.println();
Serial.print("connected: ");
Serial.println(WiFi.localIP());

// add a new entry.
FirebasePush push = fbase.push("/logs", "{\".sv\": \"timestamp\"}");
if (push.error()) {
Serial.println("Firebase push failed");
Serial.println(push.error().message());
return;
}

// print key.
Serial.println("Name: " + push.name());

// get all entries.
FirebaseGet get = fbase.get("/logs");
if (get.error()) {
Serial.println("Firebase get failed");
Serial.println(push.error().message());
return;
}
// Print written timestamp.
String data = get.json()[push.name()];
Serial.println("Timestamp:" + data);
}

int n = 0;
void loop() {
// add a new entry.
FirebasePush push = fbase.push("/logs", n++);
Serial.println("pushed: " + push.name());
delay(1000);
}
42 changes: 42 additions & 0 deletions src/Firebase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ String makeFirebaseURL(const String& path, const String& auth) {
return url;
}

template <typename T>
String jsonEncode(const T& value) {
JsonVariant json(value);
String buf;
json.printTo(buf);
return buf;
}

} // namespace

Firebase::Firebase(const String& host) : host_(host) {
Expand All @@ -53,11 +61,45 @@ FirebaseGet Firebase::get(const String& path) {
return FirebaseGet(host_, auth_, path, &http_);
}

FirebaseSet Firebase::set(const String& path, int value) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems these would be easy to template.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but then we won't be able to really generate typed documentation for them, I prefered templating the internal helper: jsonEncode.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

makes sense.

return FirebaseSet(host_, auth_, path, jsonEncode(value), &http_);
}
FirebaseSet Firebase::set(const String& path, float value) {
return FirebaseSet(host_, auth_, path, jsonEncode(value), &http_);
}
FirebaseSet Firebase::set(const String& path, double value) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bool is missing

return FirebaseSet(host_, auth_, path, jsonEncode(value), &http_);
}
FirebaseSet Firebase::set(const String& path, const String& value) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: that this will break client that were pushing json encoded string.

If needed, we could error out if the value is decoding to a valid json object / literal to help with the transition.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ya I like that idea.

return FirebaseSet(host_, auth_, path, jsonEncode(value), &http_);
}
FirebaseSet Firebase::set(const String& path, const JsonObject& value) {
String buf;
value.printTo(buf);
return FirebaseSet(host_, auth_, path, buf, &http_);
}
FirebaseSet Firebase::setRaw(const String& path, const String& value) {
return FirebaseSet(host_, auth_, path, value, &http_);
}

FirebasePush Firebase::push(const String& path, int value) {
return FirebasePush(host_, auth_, path, jsonEncode(value), &http_);
}
FirebasePush Firebase::push(const String& path, float value) {
return FirebasePush(host_, auth_, path, jsonEncode(value), &http_);
}
FirebasePush Firebase::push(const String& path, double value) {
return FirebasePush(host_, auth_, path, jsonEncode(value), &http_);
}
FirebasePush Firebase::push(const String& path, const String& value) {
return FirebasePush(host_, auth_, path, jsonEncode(value), &http_);
}
FirebasePush Firebase::push(const String& path, const JsonObject& value) {
String buf;
value.printTo(buf);
return FirebasePush(host_, auth_, path, buf, &http_);
}
FirebasePush Firebase::pushRaw(const String& path, const String& value) {
return FirebasePush(host_, auth_, path, value, &http_);
}

Expand Down
20 changes: 15 additions & 5 deletions src/Firebase.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,21 @@ class Firebase {
// Fetch json encoded `value` at `path`.
FirebaseGet get(const String& path);

FirebaseSet set(const String& path, int value);
FirebaseSet set(const String& path, float value);
FirebaseSet set(const String& path, double value);
FirebaseSet set(const String& path, const String& value);
FirebaseSet set(const String& path, const JsonObject& value);
// Set json encoded `value` at `path`.
FirebaseSet set(const String& path, const String& json);
FirebaseSet setRaw(const String& path, const String& value);

FirebasePush push(const String& path, int value);
FirebasePush push(const String& path, float value);
FirebasePush push(const String& path, double value);
FirebasePush push(const String& path, const String& value);
FirebasePush push(const String& path, const JsonObject& value);
// Add new json encoded `value` to list at `path`.
FirebasePush push(const String& path, const String& json);
FirebasePush pushRaw(const String& path, const String& value);

// Delete value at `path`.
FirebaseRemove remove(const String& path);
Expand All @@ -63,11 +73,11 @@ class FirebaseError {
public:
FirebaseError() {}
FirebaseError(int code, const String& message) : code_(code), message_(message) {
}
}
operator bool() const { return code_ != 0; }
int code() const { return code_; }
const String& message() const { return message_; }
private:
private:
int code_ = 0;
String message_ = "";
};
Expand All @@ -77,7 +87,7 @@ class FirebaseCall {
FirebaseCall() {}
FirebaseCall(const String& host, const String& auth,
const char* method, const String& path,
const String& data = "",
const String& data = "",
HTTPClient* http = NULL);
const FirebaseError& error() const {
return error_;
Expand Down