Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added update method (PATCH), return error object #309

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
13 changes: 12 additions & 1 deletion src/Firebase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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,
Expand Down
16 changes: 16 additions & 0 deletions src/Firebase.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

class FirebaseGet;
class FirebaseSet;
class FirebaseUpdate;
class FirebasePush;
class FirebaseRemove;
class FirebaseStream;
Expand All @@ -48,6 +49,10 @@ class Firebase {
FirebaseSet set(const std::string& path, const std::string& json);
virtual std::unique_ptr<FirebaseSet> 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<FirebaseSet> 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<FirebasePush> pushPtr(const std::string& path, const std::string& json);
Expand Down Expand Up @@ -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() {}
Expand Down
11 changes: 11 additions & 0 deletions src/FirebaseArduino.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -159,4 +166,8 @@ const String& FirebaseArduino::error() {
return error_.message().c_str();
}

const FirebaseError& FirebaseArduino::errorObj() {
return error_;
}

FirebaseArduino Firebase;
9 changes: 9 additions & 0 deletions src/FirebaseArduino.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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_;
Expand Down