diff --git a/Firebase.cpp b/Firebase.cpp index 6752811a..ef27bd35 100644 --- a/Firebase.cpp +++ b/Firebase.cpp @@ -46,6 +46,10 @@ FirebaseGet Firebase::get(const String& path) { return FirebaseGet(host_, auth_, path, &http_); } +FirebaseSet Firebase::set(const String& path, const String& value) { + return FirebaseSet(host_, auth_, path, value, &http_); +} + FirebasePush Firebase::push(const String& path, const String& value) { return FirebasePush(host_, auth_, path, value, &http_); } @@ -115,6 +119,17 @@ FirebaseGet::FirebaseGet(const String& host, const String& auth, } } +// FirebaseSet +FirebaseSet::FirebaseSet(const String& host, const String& auth, + const String& path, const String& value, + HTTPClient* http) + : FirebaseCall(host, auth, "PUT", path, value, http) { + + if (!error()) { + // TODO: parse json + json_ = response(); + } +} // FirebasePush FirebasePush::FirebasePush(const String& host, const String& auth, const String& path, const String& value, diff --git a/Firebase.h b/Firebase.h index aac4e86c..f82a0376 100644 --- a/Firebase.h +++ b/Firebase.h @@ -26,28 +26,30 @@ #include class FirebaseGet; +class FirebaseSet; class FirebasePush; class FirebaseRemove; class FirebaseStream; -// Primary client to the Firebase backend. +// Firebase REST API client. class Firebase { public: Firebase(const String& host); Firebase& auth(const String& auth); - // Fetch result at "path". + // Fetch json encoded `value` at `path`. FirebaseGet get(const String& path); - // Add new value to list at "path", will return key for the new item. - FirebasePush push(const String& path, const String& value); + // Set json encoded `value` at `path`. + FirebaseSet set(const String& path, const String& json); - // Deletes value at "path" from firebase. + // Add new json encoded `value` to list at `path`. + FirebasePush push(const String& path, const String& json); + + // Delete value at `path`. FirebaseRemove remove(const String& path); - // Starts a stream of events that affect object at "path". - // TODO: fix FirebaseStream lifecycle - // https://github.com/esp8266/Arduino/issues/500 + // Start a stream of events that affect value at `path`. FirebaseStream stream(const String& path); private: @@ -102,6 +104,20 @@ class FirebaseGet : public FirebaseCall { String json_; }; +class FirebaseSet: public FirebaseCall { + public: + FirebaseSet() {} + FirebaseSet(const String& host, const String& auth, + const String& path, const String& value, HTTPClient* http = NULL); + + const String& json() const { + return json_; + } + + private: + String json_; +}; + class FirebasePush : public FirebaseCall { public: FirebasePush() {} @@ -130,17 +146,17 @@ class FirebaseStream : public FirebaseCall { FirebaseStream(const String& host, const String& auth, const String& path, HTTPClient* http = NULL); - // True if there is an event available. + // Return if there is any event available to read. bool available(); - // event type. + // Event type. enum Event { UNKNOWN, PUT, PATCH }; - // Read next event in stream. + // Read next json encoded `event` from stream. Event read(String& event); const FirebaseError& error() const {