From 986b41ded02960be5d5d5fbc48f971c8122b1d57 Mon Sep 17 00:00:00 2001 From: Adnan Y Date: Sat, 30 Dec 2017 06:46:01 -0800 Subject: [PATCH] added update method (PATCH) --- src/Firebase.cpp | 13 ++++++++++++- src/Firebase.h | 16 ++++++++++++++++ src/FirebaseArduino.cpp | 11 +++++++++++ src/FirebaseArduino.h | 9 +++++++++ 4 files changed, 48 insertions(+), 1 deletion(-) diff --git a/src/Firebase.cpp b/src/Firebase.cpp index d00787d1..2b69c0fa 100644 --- a/src/Firebase.cpp +++ b/src/Firebase.cpp @@ -92,7 +92,7 @@ FirebaseCall::FirebaseCall(const std::string& host, const std::string& auth, const char* method, const std::string& path, const std::string& data, FirebaseHttpClient* http) : http_(http) { std::string path_with_auth = makeFirebaseURL(path, auth); - if ((method == "STREAM") && (path == http->getStreamingPath())){ + if (strcmp(method, "STREAM") == 0 && (path == http->getStreamingPath())){ // already streaming requested path. return; } @@ -176,6 +176,17 @@ FirebaseSet::FirebaseSet(const std::string& host, const std::string& auth, } } +// FirebaseUpdate +FirebaseUpdate::FirebaseUpdate(const std::string& host, const std::string& auth, + const std::string& path, const std::string& value, + FirebaseHttpClient* http) + : FirebaseCall(host, auth, "PATCH", path, value, http) { + if (!error()) { + // TODO: parse json + json_ = response(); + } +} + // FirebasePush FirebasePush::FirebasePush(const std::string& host, const std::string& auth, const std::string& path, const std::string& value, diff --git a/src/Firebase.h b/src/Firebase.h index 8a352045..f5f32588 100644 --- a/src/Firebase.h +++ b/src/Firebase.h @@ -29,6 +29,7 @@ class FirebaseGet; class FirebaseSet; +class FirebaseUpdate; class FirebasePush; class FirebaseRemove; class FirebaseStream; @@ -48,6 +49,10 @@ class Firebase { FirebaseSet set(const std::string& path, const std::string& json); virtual std::unique_ptr setPtr(const std::string& path, const std::string& json); + // Update json encoded `value` at `path`. + FirebaseSet update(const std::string& path, const std::string& json); + virtual std::unique_ptr updatePtr(const std::string& path, const std::string& json); + // Add new json encoded `value` to list at `path`. FirebasePush push(const std::string& path, const std::string& json); virtual std::unique_ptr pushPtr(const std::string& path, const std::string& json); @@ -117,6 +122,17 @@ class FirebaseSet: public FirebaseCall { std::string json_; }; +class FirebaseUpdate: public FirebaseCall { + public: + FirebaseUpdate() {} + FirebaseUpdate(const std::string& host, const std::string& auth, + const std::string& path, const std::string& value, FirebaseHttpClient* http = NULL); + + + private: + std::string json_; +}; + class FirebasePush : public FirebaseCall { public: FirebasePush() {} diff --git a/src/FirebaseArduino.cpp b/src/FirebaseArduino.cpp index 44dc056f..28320a5d 100644 --- a/src/FirebaseArduino.cpp +++ b/src/FirebaseArduino.cpp @@ -75,6 +75,13 @@ void FirebaseArduino::set(const String& path, const JsonVariant& value) { error_ = set.error(); } +void FirebaseArduino::update(const String& path, const JsonVariant& value) { + String buf; + value.printTo(buf); + auto set = FirebaseUpdate(host_, auth_, path.c_str(), buf.c_str(), http_.get()); + error_ = set.error(); +} + FirebaseObject FirebaseArduino::get(const String& path) { auto get = FirebaseGet(host_, auth_, path.c_str(), http_.get()); error_ = get.error(); @@ -159,4 +166,8 @@ const String& FirebaseArduino::error() { return error_.message().c_str(); } +const FirebaseError& FirebaseArduino::errorObj() { + return error_; +} + FirebaseArduino Firebase; diff --git a/src/FirebaseArduino.h b/src/FirebaseArduino.h index a0bcca27..c1f6c6ba 100644 --- a/src/FirebaseArduino.h +++ b/src/FirebaseArduino.h @@ -134,6 +134,14 @@ class FirebaseArduino { */ void set(const String& path, const JsonVariant& value); + /** + * Writes the JSON data to the node located at path. + * Equivalent to the REST API's PATCH. + * You should check success() after calling. + * \param path The path inside of your db to the node you wish to update. + * \param value JSON data that you wish to write. + */ + void update(const String& path, const JsonVariant& value); /** * Gets the integer value located at path. @@ -222,6 +230,7 @@ class FirebaseArduino { * \return Error message from last command if failed() is true. */ const String& error(); + const FirebaseError &errorObj(); private: std::string host_; std::string auth_;