Skip to content
This repository was archived by the owner on Mar 17, 2025. It is now read-only.

Commit ecbefd2

Browse files
committed
Copied over Firebase.{cc,h} changes
1 parent 8e81a0e commit ecbefd2

File tree

2 files changed

+94
-53
lines changed

2 files changed

+94
-53
lines changed

src/Firebase.cpp

Lines changed: 52 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,9 @@
1515
//
1616
#include "Firebase.h"
1717

18-
// Detect whether stable version of HTTP library is installed instead of
19-
// master branch and patch in missing status and methods.
20-
#ifndef HTTP_CODE_TEMPORARY_REDIRECT
21-
#define HTTP_CODE_TEMPORARY_REDIRECT 307
22-
#define USE_ESP_ARDUINO_CORE_2_0_0
23-
#endif
18+
using std::unique_ptr;
2419

2520
namespace {
26-
const char* kFirebaseFingerprint = "7A 54 06 9B DC 7A 25 B3 86 8D 66 53 48 2C 0B 96 42 C7 B3 0A";
27-
const uint16_t kFirebasePort = 443;
28-
2921
String makeFirebaseURL(const String& path, const String& auth) {
3022
String url;
3123
if (path[0] != '/') {
@@ -41,40 +33,66 @@ String makeFirebaseURL(const String& path, const String& auth) {
4133
} // namespace
4234

4335
Firebase::Firebase(const String& host, const String& auth) : host_(host), auth_(auth) {
44-
http_.setReuse(true);
36+
http_.reset(FirebaseHttpClient::create());
37+
http_->setReuseConnection(true);
4538
}
4639

4740
FirebaseGet Firebase::get(const String& path) {
48-
return FirebaseGet(host_, auth_, path, &http_);
41+
return FirebaseGet(host_, auth_, path, http_.get());
42+
}
43+
44+
unique_ptr<FirebaseGet> Firebase::getPtr(const String& path) {
45+
return unique_ptr<FirebaseGet>(new FirebaseGet(host_, auth_, path, http_.get()));
4946
}
5047

5148
FirebaseSet Firebase::set(const String& path, const String& value) {
52-
return FirebaseSet(host_, auth_, path, value, &http_);
49+
return FirebaseSet(host_, auth_, path, value, http_.get());
50+
}
51+
52+
unique_ptr<FirebaseSet> Firebase::setPtr(const String& path,
53+
const String& value) {
54+
return unique_ptr<FirebaseSet>(
55+
new FirebaseSet(host_, auth_, path, value, http_.get()));
5356
}
5457

5558
FirebasePush Firebase::push(const String& path, const String& value) {
56-
return FirebasePush(host_, auth_, path, value, &http_);
59+
return FirebasePush(host_, auth_, path, value, http_.get());
60+
}
61+
unique_ptr<FirebasePush> Firebase::pushPtr(const String& path, const String& value) {
62+
return unique_ptr<FirebasePush>(
63+
new FirebasePush(host_, auth_, path, value, http_.get()));
5764
}
5865

5966
FirebaseRemove Firebase::remove(const String& path) {
60-
return FirebaseRemove(host_, auth_, path, &http_);
67+
return FirebaseRemove(host_, auth_, path, http_.get());
68+
}
69+
70+
unique_ptr<FirebaseRemove> Firebase::removePtr(const String& path) {
71+
return unique_ptr<FirebaseRemove>(
72+
new FirebaseRemove(host_, auth_, path, http_.get()));
6173
}
6274

6375
FirebaseStream Firebase::stream(const String& path) {
6476
// TODO: create new client dedicated to stream.
65-
return FirebaseStream(host_, auth_, path, &http_);
77+
return FirebaseStream(host_, auth_, path, http_.get());
78+
}
79+
80+
unique_ptr<FirebaseStream> Firebase::streamPtr(const String& path) {
81+
// TODO: create new client dedicated to stream.
82+
return unique_ptr<FirebaseStream>(
83+
new FirebaseStream(host_, auth_, path, http_.get()));
6684
}
6785

6886
// FirebaseCall
6987
FirebaseCall::FirebaseCall(const String& host, const String& auth,
7088
const char* method, const String& path,
71-
const String& data, HTTPClient* http) : http_(http) {
72-
String url = makeFirebaseURL(path, auth);
73-
http_->setReuse(true);
74-
http_->begin(host, kFirebasePort, url, true, kFirebaseFingerprint);
89+
const String& data, FirebaseHttpClient* http) : http_(http) {
90+
String path_with_auth = makeFirebaseURL(path, auth);
91+
http_->setReuseConnection(true);
92+
http_->begin(host, path_with_auth);
7593

7694
bool followRedirect = false;
77-
if (method == "STREAM") {
95+
if (String(method) == "STREAM") {
7896
method = "GET";
7997
http_->addHeader("Accept", "text/event-stream");
8098
followRedirect = true;
@@ -85,26 +103,24 @@ FirebaseCall::FirebaseCall(const String& host, const String& auth,
85103
http_->collectHeaders(headers, 1);
86104
}
87105

88-
int status = http_->sendRequest(method, (uint8_t*)data.c_str(), data.length());
106+
int status = http_->sendRequest(method, data);
89107

90108
// TODO: Add a max redirect check
91109
if (followRedirect) {
92-
while (status == HTTP_CODE_TEMPORARY_REDIRECT) {
110+
while (status == HttpStatus::TEMPORARY_REDIRECT) {
93111
String location = http_->header("Location");
94-
http_->setReuse(false);
112+
http_->setReuseConnection(false);
95113
http_->end();
96-
http_->setReuse(true);
97-
http_->begin(location, kFirebaseFingerprint);
98-
status = http_->sendRequest("GET", (uint8_t*)NULL, 0);
114+
http_->setReuseConnection(true);
115+
http_->begin(location);
116+
status = http_->sendRequest("GET", String());
99117
}
100118
}
101119

102120
if (status != 200) {
103-
#ifdef USE_ESP_ARDUINO_CORE_2_0_0
104-
error_ = FirebaseError(status, String(method) + " " + url + ": " + status);
105-
#else
106-
error_ = FirebaseError(status, String(method) + " " + url + ": " + HTTPClient::errorToString(status));
107-
#endif
121+
error_ = FirebaseError(status,
122+
String(method) + " " + path_with_auth +
123+
": " + http_->errorToString(status));
108124
}
109125

110126
// if not streaming.
@@ -123,14 +139,14 @@ const JsonObject& FirebaseCall::json() {
123139
// FirebaseGet
124140
FirebaseGet::FirebaseGet(const String& host, const String& auth,
125141
const String& path,
126-
HTTPClient* http)
142+
FirebaseHttpClient* http)
127143
: FirebaseCall(host, auth, "GET", path, "", http) {
128144
}
129145

130146
// FirebaseSet
131147
FirebaseSet::FirebaseSet(const String& host, const String& auth,
132148
const String& path, const String& value,
133-
HTTPClient* http)
149+
FirebaseHttpClient* http)
134150
: FirebaseCall(host, auth, "PUT", path, value, http) {
135151
if (!error()) {
136152
// TODO: parse json
@@ -140,7 +156,7 @@ FirebaseSet::FirebaseSet(const String& host, const String& auth,
140156
// FirebasePush
141157
FirebasePush::FirebasePush(const String& host, const String& auth,
142158
const String& path, const String& value,
143-
HTTPClient* http)
159+
FirebaseHttpClient* http)
144160
: FirebaseCall(host, auth, "POST", path, value, http) {
145161
if (!error()) {
146162
name_ = json()["name"].as<const char*>();
@@ -150,14 +166,14 @@ FirebasePush::FirebasePush(const String& host, const String& auth,
150166
// FirebasePush
151167
FirebaseRemove::FirebaseRemove(const String& host, const String& auth,
152168
const String& path,
153-
HTTPClient* http)
169+
FirebaseHttpClient* http)
154170
: FirebaseCall(host, auth, "DELETE", path, "", http) {
155171
}
156172

157173
// FirebaseStream
158174
FirebaseStream::FirebaseStream(const String& host, const String& auth,
159175
const String& path,
160-
HTTPClient* http)
176+
FirebaseHttpClient* http)
161177
: FirebaseCall(host, auth, "STREAM", path, "", http) {
162178
}
163179

src/Firebase.h

Lines changed: 42 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,11 @@
2121
#define firebase_h
2222

2323
#include <Arduino.h>
24-
#include <ESP8266WiFi.h>
25-
#include <WiFiClientSecure.h>
26-
#include <ESP8266HTTPClient.h>
24+
#include <memory>
25+
#include "FirebaseHttpClient.h"
26+
// TODO(edcoyne): move this into our mock_arduino fork where we actually do the
27+
// override.
28+
#define ARDUINO_STRING_OVERRIDE
2729
#include "third-party/arduino-json-5.1.1/include/ArduinoJson.h"
2830

2931
class FirebaseGet;
@@ -39,21 +41,30 @@ class Firebase {
3941

4042
// Fetch json encoded `value` at `path`.
4143
FirebaseGet get(const String& path);
44+
virtual std::unique_ptr<FirebaseGet> getPtr(const String& path);
4245

4346
// Set json encoded `value` at `path`.
4447
FirebaseSet set(const String& path, const String& json);
48+
virtual std::unique_ptr<FirebaseSet> setPtr(const String& path, const String& json);
4549

4650
// Add new json encoded `value` to list at `path`.
4751
FirebasePush push(const String& path, const String& json);
52+
virtual std::unique_ptr<FirebasePush> pushPtr(const String& path, const String& json);
4853

4954
// Delete value at `path`.
5055
FirebaseRemove remove(const String& path);
56+
virtual std::unique_ptr<FirebaseRemove> removePtr(const String& path);
5157

5258
// Start a stream of events that affect value at `path`.
5359
FirebaseStream stream(const String& path);
60+
virtual std::unique_ptr<FirebaseStream> streamPtr(const String& path);
61+
62+
protected:
63+
// Used for testing.
64+
Firebase() {}
5465

5566
private:
56-
HTTPClient http_;
67+
std::unique_ptr<FirebaseHttpClient> http_;
5768
String host_;
5869
String auth_;
5970
};
@@ -76,20 +87,20 @@ class FirebaseCall {
7687
FirebaseCall() {}
7788
FirebaseCall(const String& host, const String& auth,
7889
const char* method, const String& path,
79-
const String& data = "",
80-
HTTPClient* http = NULL);
81-
const FirebaseError& error() const {
90+
const String& data = "",
91+
FirebaseHttpClient* http = NULL);
92+
virtual const FirebaseError& error() const {
8293
return error_;
8394
}
8495

85-
const String& response() {
96+
virtual const String& response() {
8697
return response_;
8798
}
8899

89100
const JsonObject& json();
90101

91102
protected:
92-
HTTPClient* http_;
103+
FirebaseHttpClient* http_;
93104
FirebaseError error_;
94105
String response_;
95106
DynamicJsonBuffer buffer_;
@@ -99,7 +110,7 @@ class FirebaseGet : public FirebaseCall {
99110
public:
100111
FirebaseGet() {}
101112
FirebaseGet(const String& host, const String& auth,
102-
const String& path, HTTPClient* http = NULL);
113+
const String& path, FirebaseHttpClient* http = NULL);
103114

104115
private:
105116
String json_;
@@ -109,7 +120,8 @@ class FirebaseSet: public FirebaseCall {
109120
public:
110121
FirebaseSet() {}
111122
FirebaseSet(const String& host, const String& auth,
112-
const String& path, const String& value, HTTPClient* http = NULL);
123+
const String& path, const String& value, FirebaseHttpClient* http = NULL);
124+
113125

114126
private:
115127
String json_;
@@ -119,9 +131,9 @@ class FirebasePush : public FirebaseCall {
119131
public:
120132
FirebasePush() {}
121133
FirebasePush(const String& host, const String& auth,
122-
const String& path, const String& value, HTTPClient* http = NULL);
134+
const String& path, const String& value, FirebaseHttpClient* http = NULL);
123135

124-
const String& name() const {
136+
virtual const String& name() const {
125137
return name_;
126138
}
127139

@@ -133,18 +145,18 @@ class FirebaseRemove : public FirebaseCall {
133145
public:
134146
FirebaseRemove() {}
135147
FirebaseRemove(const String& host, const String& auth,
136-
const String& path, HTTPClient* http = NULL);
148+
const String& path, FirebaseHttpClient* http = NULL);
137149
};
138150

139151

140152
class FirebaseStream : public FirebaseCall {
141153
public:
142154
FirebaseStream() {}
143155
FirebaseStream(const String& host, const String& auth,
144-
const String& path, HTTPClient* http = NULL);
156+
const String& path, FirebaseHttpClient* http = NULL);
145157

146158
// Return if there is any event available to read.
147-
bool available();
159+
virtual bool available();
148160

149161
// Event type.
150162
enum Event {
@@ -153,8 +165,21 @@ class FirebaseStream : public FirebaseCall {
153165
PATCH
154166
};
155167

168+
static inline String EventToName(Event event) {
169+
switch(event) {
170+
case UNKNOWN:
171+
return "UNKNOWN";
172+
case PUT:
173+
return "PUT";
174+
case PATCH:
175+
return "PATCH";
176+
default:
177+
return "INVALID_EVENT_" + event;
178+
}
179+
}
180+
156181
// Read next json encoded `event` from stream.
157-
Event read(String& event);
182+
virtual Event read(String& event);
158183

159184
const FirebaseError& error() const {
160185
return _error;

0 commit comments

Comments
 (0)