16
16
#include " Firebase.h"
17
17
18
18
using std::unique_ptr;
19
+ using std::shared_ptr;
19
20
20
21
namespace {
21
22
std::string makeFirebaseURL (const std::string& path, const std::string& auth) {
@@ -42,55 +43,56 @@ const std::string& Firebase::auth() const {
42
43
}
43
44
44
45
FirebaseGet Firebase::get (const std::string& path) {
45
- return FirebaseGet (host_, auth_, path, http_. get () );
46
+ return FirebaseGet (host_, auth_, path, http_);
46
47
}
47
48
48
- unique_ptr<FirebaseGet> Firebase::getPtr (const std::string& path) {
49
- return unique_ptr<FirebaseGet>(new FirebaseGet (host_, auth_, path, http_. get () ));
49
+ unique_ptr<FirebaseGet> Firebase::getPtr (const std::string& path) {
50
+ return unique_ptr<FirebaseGet>(new FirebaseGet (host_, auth_, path, http_));
50
51
}
51
52
52
53
FirebaseSet Firebase::set (const std::string& path, const std::string& value) {
53
- return FirebaseSet (host_, auth_, path, value, http_. get () );
54
+ return FirebaseSet (host_, auth_, path, value, http_);
54
55
}
55
56
56
- unique_ptr<FirebaseSet> Firebase::setPtr (const std::string& path,
57
- const std::string& value) {
58
- return unique_ptr<FirebaseSet>(
59
- new FirebaseSet (host_, auth_, path, value, http_. get ()));
57
+ unique_ptr<FirebaseSet> Firebase::setPtr (const std::string& path,
58
+ const std::string& value) {
59
+ return unique_ptr<FirebaseSet>(
60
+ new FirebaseSet (host_, auth_, path, value, http_));
60
61
}
61
62
62
63
FirebasePush Firebase::push (const std::string& path, const std::string& value) {
63
- return FirebasePush (host_, auth_, path, value, http_. get () );
64
+ return FirebasePush (host_, auth_, path, value, http_);
64
65
}
65
- unique_ptr<FirebasePush> Firebase::pushPtr (const std::string& path, const std::string& value) {
66
- return unique_ptr<FirebasePush>(
67
- new FirebasePush (host_, auth_, path, value, http_.get ()));
66
+
67
+ unique_ptr<FirebasePush> Firebase::pushPtr (const std::string& path, const std::string& value) {
68
+ return unique_ptr<FirebasePush>(
69
+ new FirebasePush (host_, auth_, path, value, http_));
68
70
}
69
71
70
72
FirebaseRemove Firebase::remove (const std::string& path) {
71
- return FirebaseRemove (host_, auth_, path, http_. get () );
73
+ return FirebaseRemove (host_, auth_, path, http_);
72
74
}
73
75
74
- unique_ptr<FirebaseRemove> Firebase::removePtr (const std::string& path) {
75
- return unique_ptr<FirebaseRemove>(
76
- new FirebaseRemove (host_, auth_, path, http_. get ()));
76
+ unique_ptr<FirebaseRemove> Firebase::removePtr (const std::string& path) {
77
+ return unique_ptr<FirebaseRemove>(
78
+ new FirebaseRemove (host_, auth_, path, http_));
77
79
}
78
80
79
81
FirebaseStream Firebase::stream (const std::string& path) {
80
82
// TODO: create new client dedicated to stream.
81
- return FirebaseStream (host_, auth_, path, http_. get () );
83
+ return FirebaseStream (host_, auth_, path, http_);
82
84
}
83
85
84
- unique_ptr<FirebaseStream> Firebase::streamPtr (const std::string& path) {
85
- // TODO: create new client dedicated to stream.
86
- return unique_ptr<FirebaseStream>(
87
- new FirebaseStream (host_, auth_, path, http_. get ()));
86
+ unique_ptr<FirebaseStream> Firebase::streamPtr (const std::string& path) {
87
+ // TODO: create new client dedicated to stream.
88
+ return unique_ptr<FirebaseStream>(
89
+ new FirebaseStream (host_, auth_, path, http_));
88
90
}
89
91
90
92
// FirebaseCall
91
93
FirebaseCall::FirebaseCall (const std::string& host, const std::string& auth,
92
94
const char * method, const std::string& path,
93
- const std::string& data, FirebaseHttpClient* http) : http_(http) {
95
+ const std::string& data, const std::shared_ptr< FirebaseHttpClient> http) : http_(http) {
94
96
std::string path_with_auth = makeFirebaseURL (path, auth);
95
97
if ((method == " STREAM" ) && (path == http->getStreamingPath ())){
96
98
// already streaming requested path.
@@ -153,22 +155,24 @@ FirebaseCall::~FirebaseCall() {
153
155
154
156
const JsonObject& FirebaseCall::json () {
155
157
// TODO(edcoyne): This is not efficient, we should do something smarter with
156
- // the buffers.
157
- buffer_ = DynamicJsonBuffer ();
158
- return buffer_.parseObject (response ().c_str ());
158
+ // the buffers. kotl: Is this still valid?
159
+ if (buffer_.get () == NULL ) {
160
+ buffer_.reset (new StaticJsonBuffer<FIREBASE_JSONBUFFER_SIZE>());
161
+ }
162
+ return buffer_.get ()->parseObject (response ().c_str ());
159
163
}
160
164
161
165
// FirebaseGet
162
166
FirebaseGet::FirebaseGet (const std::string& host, const std::string& auth,
163
167
const std::string& path,
164
- FirebaseHttpClient* http)
168
+ const std::shared_ptr< FirebaseHttpClient> http)
165
169
: FirebaseCall(host, auth, " GET" , path, " " , http) {
166
170
}
167
171
168
172
// FirebaseSet
169
173
FirebaseSet::FirebaseSet (const std::string& host, const std::string& auth,
170
174
const std::string& path, const std::string& value,
171
- FirebaseHttpClient* http)
175
+ const std::shared_ptr< FirebaseHttpClient> http)
172
176
: FirebaseCall(host, auth, " PUT" , path, value, http) {
173
177
if (!error ()) {
174
178
// TODO: parse json
@@ -179,7 +183,7 @@ FirebaseSet::FirebaseSet(const std::string& host, const std::string& auth,
179
183
// FirebasePush
180
184
FirebasePush::FirebasePush (const std::string& host, const std::string& auth,
181
185
const std::string& path, const std::string& value,
182
- FirebaseHttpClient* http)
186
+ const std::shared_ptr< FirebaseHttpClient> http)
183
187
: FirebaseCall(host, auth, " POST" , path, value, http) {
184
188
if (!error ()) {
185
189
name_ = json ()[" name" ].as <const char *>();
@@ -189,26 +193,27 @@ FirebasePush::FirebasePush(const std::string& host, const std::string& auth,
189
193
// FirebaseRemove
190
194
FirebaseRemove::FirebaseRemove (const std::string& host, const std::string& auth,
191
195
const std::string& path,
192
- FirebaseHttpClient* http)
196
+ const std::shared_ptr< FirebaseHttpClient> http)
193
197
: FirebaseCall(host, auth, " DELETE" , path, " " , http) {
194
198
}
195
199
196
200
// FirebaseStream
197
201
FirebaseStream::FirebaseStream (const std::string& host, const std::string& auth,
198
202
const std::string& path,
199
- FirebaseHttpClient* http)
203
+ const std::shared_ptr< FirebaseHttpClient> http)
200
204
: FirebaseCall(host, auth, " STREAM" , path, " " , http) {
201
205
}
202
206
203
207
bool FirebaseStream::available () {
204
- if (http_->getStreamPtr () == nullptr ) {
205
- return false ;
206
- }
207
- return http_->getStreamPtr ()->available ();
208
+ auto client = http_->getStreamPtr ();
209
+ return (client == nullptr ) ? false : client->available ();
208
210
}
209
211
210
212
FirebaseStream::Event FirebaseStream::read (std::string& event) {
211
213
auto client = http_->getStreamPtr ();
214
+ if (client == nullptr ) {
215
+ return Event ();
216
+ }
212
217
Event type;
213
218
std::string typeStr = client->readStringUntil (' \n ' ).substring (7 ).c_str ();
214
219
if (typeStr == " put" ) {
0 commit comments