This repository was archived by the owner on Mar 17, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 492
Refactored Firebase library #42
Merged
Merged
Changes from 14 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
0a613cd
Refactor Firebase module, split into more focues subclasses, change r…
ed7coyne d805dc6
removed FirebaseResultWithMessage consolidated into one for simplicity
ed7coyne 217d58f
Added comments for clarify
ed7coyne b2438cf
removed old reference to FirebaseResultWithMessage
ed7coyne d5dd303
Refactor out FirebaseCall
ed7coyne 23fbbed
possibly fix streaming example
ed7coyne 2bf3a4f
Move helper classes below Firebase in Firebase.h
ed7coyne 2df5bd6
Added error message back to streaming imp
ed7coyne 077e060
Add call ids
ed7coyne 6f65250
Revert "Add call ids"
ed7coyne 33f81ef
Added independant opaque return types for all calls.
ed7coyne 2ac7ab3
Remove unused method declaration.
ed7coyne 357a1d0
firebase: refactor the refactoring
proppy c65fef7
Merge pull request #2 from proppy/refactor-12
ed7coyne b286b19
firebase: fix stream example
proppy 34357a9
Merge pull request #3 from proppy/refactor
ed7coyne f38602f
merge
proppy e8ac1e9
examples/stream: fix error handling
proppy caa70e9
Merge pull request #4 from proppy/refactor-merge-3
ed7coyne File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,52 +25,123 @@ | |
#include <WiFiClientSecure.h> | ||
#include <ESP8266HTTPClient.h> | ||
|
||
// FirebaseError represents a Firebase API error with a code and a | ||
// message. | ||
class FirebaseGet; | ||
class FirebasePush; | ||
class FirebaseRemove; | ||
class FirebaseStream; | ||
|
||
// Primary client to the Firebase backend. | ||
class Firebase { | ||
public: | ||
Firebase(const String& host); | ||
Firebase& auth(const String& auth); | ||
|
||
// Fetch result 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); | ||
|
||
// Deletes value at "path" from firebase. | ||
FirebaseRemove remove(const String& path); | ||
|
||
// Starts a stream of events that affect object at "path". | ||
FirebaseStream stream(const String& path); | ||
|
||
private: | ||
HTTPClient http_; | ||
String host_; | ||
String auth_; | ||
}; | ||
|
||
class FirebaseError { | ||
public: | ||
operator bool() const { return _code < 0; } | ||
int code() const { return _code; } | ||
const String& message() const { return _message; } | ||
void reset() { set(0, ""); } | ||
void set(int code, const String& message) { | ||
_code = code; | ||
_message = message; | ||
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: | ||
int code_ = 0; | ||
String message_ = ""; | ||
}; | ||
|
||
class FirebaseCall { | ||
public: | ||
FirebaseCall(const String& host, const String& auth, | ||
const char* method, const String& path, | ||
const String& data = "", | ||
HTTPClient* http = NULL); | ||
const FirebaseError& error() const { | ||
return error_; | ||
} | ||
const String& response() { | ||
return response_; | ||
} | ||
protected: | ||
HTTPClient http_; | ||
FirebaseError error_; | ||
String response_; | ||
}; | ||
|
||
class FirebaseGet : public FirebaseCall { | ||
public: | ||
FirebaseGet(const String& host, const String& auth, | ||
const String& path, HTTPClient* http = NULL); | ||
|
||
const String& json() const { | ||
return json_; | ||
} | ||
|
||
private: | ||
int _code = 0; | ||
String _message = ""; | ||
String json_; | ||
}; | ||
|
||
// Firebase is the connection to firebase. | ||
class Firebase { | ||
class FirebasePush : public FirebaseCall { | ||
public: | ||
Firebase(const String& host); | ||
Firebase& auth(const String& auth); | ||
const FirebaseError& error() const { | ||
return _error; | ||
FirebasePush(const String& host, const String& auth, | ||
const String& path, const String& value, HTTPClient* http = NULL); | ||
|
||
const String& name() const { | ||
return name_; | ||
} | ||
String get(const String& path); | ||
String push(const String& path, const String& value); | ||
bool remove(const String& path); | ||
bool connected(); | ||
Firebase& stream(const String& path); | ||
|
||
private: | ||
String name_; | ||
}; | ||
|
||
class FirebaseRemove : public FirebaseCall { | ||
public: | ||
FirebaseRemove(const String& host, const String& auth, | ||
const String& path, HTTPClient* http = NULL); | ||
}; | ||
|
||
|
||
class FirebaseStream : public FirebaseCall { | ||
public: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should probably allow a lazy construction of the |
||
FirebaseStream(const String& host, const String& auth, | ||
const String &path); | ||
|
||
// True if there is an event available. | ||
bool available(); | ||
|
||
// event type. | ||
enum Event { | ||
UNKNOWN, | ||
PUT, | ||
PATCH | ||
}; | ||
Event read(String& event); | ||
|
||
// Read next event in stream. | ||
Event read(String& event); | ||
|
||
const FirebaseError& error() const { | ||
return _error; | ||
} | ||
|
||
private: | ||
String makeURL(const String& path); | ||
int sendRequest(const char* method, const String& path, const String& value = ""); | ||
String sendRequestGetBody(const char* method, const String& path, const String& value = ""); | ||
void setError(const char* method, const String& url, int status_code); | ||
|
||
HTTPClient _http; | ||
String _host; | ||
String _auth; | ||
HTTPClient http_; | ||
FirebaseError _error; | ||
}; | ||
|
||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should probably reuse the client for now (even for stream) and do that in a separate PR (that might be the cause of the crash we see with the stream sample)