forked from FirebaseExtended/firebase-arduino
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathFirebase.cpp
255 lines (202 loc) · 6.94 KB
/
Firebase.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
//
// Copyright 2015 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#include "Firebase.h"
namespace {
const char* kFirebaseFingerprint = "7A 54 06 9B DC 7A 25 B3 86 8D 66 53 48 2C 0B 96 42 C7 B3 0A";
const uint16_t kFirebasePort = 443;
String makeUrl(const String& path, const String& auth) {
String url;
if (path[0] != '/') {
url = "/";
}
url += path + ".json";
if (auth.length() > 0) {
url += "?auth=" + auth;
}
return url;
}
class FirebaseCall {
public:
FirebaseCall(const String& host, const String& auth,
const char* method, const String& path, const String& value,
HTTPClient* http);
FirebaseCall(const String& host, const String& auth,
const char* method, const String& path,
HTTPClient* http);
// True if there was an error completing call.
bool isError() const;
String errorMessage() const;
// True if http status code is 200(OK).
bool isOk() const;
// Message sent back from Firebase backend. This pulls value to local memory,
// be careful if value can be large.
String rawResponse();
int httpStatus() const {
return status_;
}
private:
FirebaseCall(HTTPClient* http);
HTTPClient* http_;
int status_;
String error_message_;
};
} // namespace
Firebase::Firebase(const String& host) : host_(host) {
http_.setReuse(true);
}
Firebase& Firebase::auth(const String& auth) {
auth_ = auth;
return *this;
}
FirebaseGetResult Firebase::get(const String& path) {
FirebaseCall call(host_, auth_, "GET", path, &http_);
return call.isError() ? FirebaseGetResult::FromError(call.errorMessage())
: FirebaseGetResult::FromResponse(call.rawResponse());
}
FirebasePushResult Firebase::push(const String& path, const String& value) {
FirebaseCall call(host_, auth_, "POST", path, value, &http_);
return call.isError() ? FirebasePushResult::FromError(call.errorMessage())
: FirebasePushResult::FromResponse(call.rawResponse());
}
FirebaseRemoveResult Firebase::remove(const String& path) {
FirebaseCall call(host_, auth_, "DELETE", path, &http_);
if (call.isError()) {
return FirebaseRemoveResult::FromError(call.errorMessage());
}
// Remove is only complete if returned status is OK(200).
if (!call.isOk()) {
return FirebaseRemoveResult::FromError(
"Remove " + path + " returned with status " + call.httpStatus());
}
return FirebaseRemoveResult::Ok();
}
FirebaseEventStream Firebase::stream(const String& path) {
return FirebaseEventStream(host_, auth_, path);
}
/* FirebaseCall */
FirebaseCall::FirebaseCall(const String& host, const String& auth,
const char* method, const String& path, const String& value,
HTTPClient* http) : http_(http) {
const String url = makeUrl(path, auth);
http_->begin(host.c_str(), kFirebasePort, url.c_str(), true, kFirebaseFingerprint);
status_ = http_->sendRequest(method, (uint8_t*)value.c_str(), value.length());
if (isError()) {
error_message_ = String(method) + " " + url + ": " + HTTPClient::errorToString(status_);
}
}
FirebaseCall::FirebaseCall(const String& host, const String& auth,
const char* method, const String& path,
HTTPClient* http) : FirebaseCall(host, auth, method, path, "", http) {
}
bool FirebaseCall::isOk() const {
return status_ == HTTP_CODE_OK;
}
bool FirebaseCall::isError() const {
return status_ < 0;
}
String FirebaseCall::errorMessage() const {
return error_message_;
}
String FirebaseCall::rawResponse() {
return http_->getString();
}
/* FirebaseEventStream */
FirebaseEventStream::FirebaseEventStream(const String& host, const String& auth,
const String& path) {
const String url = makeUrl(path, auth);
http_.setReuse(true);
http_.begin(host.c_str(), kFirebasePort, url.c_str(), true,
kFirebaseFingerprint);
const char* headers[] = {"Location"};
http_.collectHeaders(headers, 1);
http_.addHeader("Accept", "text/event-stream");
status_ = http_.sendRequest("GET", (uint8_t*)NULL, 0);
String location;
// TODO(proppy): Add a max redirect check
while (status_ == HTTP_CODE_TEMPORARY_REDIRECT) {
location = http_.header("Location");
http_.setReuse(false);
http_.end();
http_.setReuse(true);
http_.begin(location, kFirebaseFingerprint);
status_ = http_.sendRequest("GET", (uint8_t*)NULL, 0);
}
if (status_ != 200) {
error_message_ = "stream " + location + ": "
+ HTTPClient::errorToString(status_);
}
}
bool FirebaseEventStream::connected() {
return http_.connected();
}
bool FirebaseEventStream::available() {
return http_.getStreamPtr()->available();
}
FirebaseEventStream::Event FirebaseEventStream::read(String& event) {
auto client = http_.getStreamPtr();
Event type;
String typeStr = client->readStringUntil('\n').substring(7);
if (typeStr == "put") {
type = FirebaseEventStream::Event::PUT;
} else if (typeStr == "patch") {
type = FirebaseEventStream::Event::PATCH;
} else {
type = FirebaseEventStream::Event::UNKNOWN;
}
event = client->readStringUntil('\n').substring(6);
client->readStringUntil('\n'); // consume separator
return type;
}
bool FirebaseEventStream::isError() const {
return status_ < 0;
}
String FirebaseEventStream::errorMessage() const {
return error_message_;
}
FirebaseEventStream::operator bool() {
return !isError() && connected();
}
/* FirebaseResult */
FirebaseResult::FirebaseResult(const String& error_message)
: is_error_(true), error_message_(error_message) {}
FirebaseResult::FirebaseResult() {}
FirebaseResult::operator bool() const {
return !isError();
}
bool FirebaseResult::isError() const {
return is_error_;
}
const String& FirebaseResult::errorMessage() const {
return error_message_;
}
/* FirebaseRemoveResult */
FirebaseRemoveResult::FirebaseRemoveResult(const String& error_message)
: FirebaseResult(error_message) {}
FirebaseRemoveResult::FirebaseRemoveResult() {}
/* FirebasePushResult */
FirebasePushResult::FirebasePushResult(const String& error_message)
: FirebaseResult(error_message) {}
FirebasePushResult::FirebasePushResult() {}
const String& FirebasePushResult::name() const {
return name_;
}
/* FirebaseGetResult */
FirebaseGetResult::FirebaseGetResult(const String& error_message)
: FirebaseResult(error_message) {}
FirebaseGetResult::FirebaseGetResult() {}
const String& FirebaseGetResult::rawResponse() {
return response_;
}