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
2520namespace {
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-
2921String makeFirebaseURL (const String& path, const String& auth) {
3022 String url;
3123 if (path[0 ] != ' /' ) {
@@ -41,45 +33,75 @@ String makeFirebaseURL(const String& path, const String& auth) {
4133} // namespace
4234
4335Firebase::Firebase (const String& host) : host_(host) {
44- http_.setReuse (true );
36+ http_.reset (FirebaseHttpClient::create ());
37+ http_->setReuseConnection (true );
4538}
4639
4740Firebase& Firebase::auth (const String& auth) {
4841 auth_ = auth;
4942 return *this ;
5043}
5144
45+ const String& Firebase::auth () {
46+ return auth_;
47+ }
48+
5249FirebaseGet Firebase::get (const String& path) {
53- return FirebaseGet (host_, auth_, path, &http_);
50+ return FirebaseGet (host_, auth_, path, http_.get ());
51+ }
52+
53+ unique_ptr<FirebaseGet> Firebase::getPtr (const String& path) {
54+ return unique_ptr<FirebaseGet>(new FirebaseGet (host_, auth_, path, http_.get ()));
5455}
5556
5657FirebaseSet Firebase::set (const String& path, const String& value) {
57- return FirebaseSet (host_, auth_, path, value, &http_);
58+ return FirebaseSet (host_, auth_, path, value, http_.get ());
59+ }
60+
61+ unique_ptr<FirebaseSet> Firebase::setPtr (const String& path,
62+ const String& value) {
63+ return unique_ptr<FirebaseSet>(
64+ new FirebaseSet (host_, auth_, path, value, http_.get ()));
5865}
5966
6067FirebasePush Firebase::push (const String& path, const String& value) {
61- return FirebasePush (host_, auth_, path, value, &http_);
68+ return FirebasePush (host_, auth_, path, value, http_.get ());
69+ }
70+ unique_ptr<FirebasePush> Firebase::pushPtr (const String& path, const String& value) {
71+ return unique_ptr<FirebasePush>(
72+ new FirebasePush (host_, auth_, path, value, http_.get ()));
6273}
6374
6475FirebaseRemove Firebase::remove (const String& path) {
65- return FirebaseRemove (host_, auth_, path, &http_);
76+ return FirebaseRemove (host_, auth_, path, http_.get ());
77+ }
78+
79+ unique_ptr<FirebaseRemove> Firebase::removePtr (const String& path) {
80+ return unique_ptr<FirebaseRemove>(
81+ new FirebaseRemove (host_, auth_, path, http_.get ()));
6682}
6783
6884FirebaseStream Firebase::stream (const String& path) {
6985 // TODO: create new client dedicated to stream.
70- return FirebaseStream (host_, auth_, path, &http_);
86+ return FirebaseStream (host_, auth_, path, http_.get ());
87+ }
88+
89+ unique_ptr<FirebaseStream> Firebase::streamPtr (const String& path) {
90+ // TODO: create new client dedicated to stream.
91+ return unique_ptr<FirebaseStream>(
92+ new FirebaseStream (host_, auth_, path, http_.get ()));
7193}
7294
7395// FirebaseCall
7496FirebaseCall::FirebaseCall (const String& host, const String& auth,
7597 const char * method, const String& path,
76- const String& data, HTTPClient * http) : http_(http) {
77- String url = makeFirebaseURL (path, auth);
78- http_->setReuse (true );
79- http_->begin (host, kFirebasePort , url, true , kFirebaseFingerprint );
98+ const String& data, FirebaseHttpClient * http) : http_(http) {
99+ String path_with_auth = makeFirebaseURL (path, auth);
100+ http_->setReuseConnection (true );
101+ http_->begin (host, path_with_auth );
80102
81103 bool followRedirect = false ;
82- if (method == " STREAM" ) {
104+ if (String ( method) == " STREAM" ) {
83105 method = " GET" ;
84106 http_->addHeader (" Accept" , " text/event-stream" );
85107 followRedirect = true ;
@@ -90,26 +112,24 @@ FirebaseCall::FirebaseCall(const String& host, const String& auth,
90112 http_->collectHeaders (headers, 1 );
91113 }
92114
93- int status = http_->sendRequest (method, ( uint8_t *) data. c_str (), data. length () );
115+ int status = http_->sendRequest (method, data);
94116
95117 // TODO: Add a max redirect check
96118 if (followRedirect) {
97- while (status == HTTP_CODE_TEMPORARY_REDIRECT ) {
119+ while (status == HttpStatus::TEMPORARY_REDIRECT ) {
98120 String location = http_->header (" Location" );
99- http_->setReuse (false );
121+ http_->setReuseConnection (false );
100122 http_->end ();
101- http_->setReuse (true );
102- http_->begin (location, kFirebaseFingerprint );
103- status = http_->sendRequest (" GET" , ( uint8_t *) NULL , 0 );
123+ http_->setReuseConnection (true );
124+ http_->begin (location);
125+ status = http_->sendRequest (" GET" , String () );
104126 }
105127 }
106128
107129 if (status != 200 ) {
108- #ifdef USE_ESP_ARDUINO_CORE_2_0_0
109- error_ = FirebaseError (status, String (method) + " " + url + " : " + status);
110- #else
111- error_ = FirebaseError (status, String (method) + " " + url + " : " + HTTPClient::errorToString (status));
112- #endif
130+ error_ = FirebaseError (status,
131+ String (method) + " " + path_with_auth +
132+ " : " + http_->errorToString (status));
113133 }
114134
115135 // if not streaming.
@@ -128,14 +148,14 @@ const JsonObject& FirebaseCall::json() {
128148// FirebaseGet
129149FirebaseGet::FirebaseGet (const String& host, const String& auth,
130150 const String& path,
131- HTTPClient * http)
151+ FirebaseHttpClient * http)
132152 : FirebaseCall(host, auth, " GET" , path, " " , http) {
133153}
134154
135155// FirebaseSet
136156FirebaseSet::FirebaseSet (const String& host, const String& auth,
137157 const String& path, const String& value,
138- HTTPClient * http)
158+ FirebaseHttpClient * http)
139159 : FirebaseCall(host, auth, " PUT" , path, value, http) {
140160 if (!error ()) {
141161 // TODO: parse json
@@ -145,24 +165,24 @@ FirebaseSet::FirebaseSet(const String& host, const String& auth,
145165// FirebasePush
146166FirebasePush::FirebasePush (const String& host, const String& auth,
147167 const String& path, const String& value,
148- HTTPClient * http)
168+ FirebaseHttpClient * http)
149169 : FirebaseCall(host, auth, " POST" , path, value, http) {
150170 if (!error ()) {
151- name_ = json ()[" name" ].as <const char *>();
171+ // name_ = json()["name"].as<const char*>();
152172 }
153173}
154174
155175// FirebasePush
156176FirebaseRemove::FirebaseRemove (const String& host, const String& auth,
157177 const String& path,
158- HTTPClient * http)
178+ FirebaseHttpClient * http)
159179 : FirebaseCall(host, auth, " DELETE" , path, " " , http) {
160180}
161181
162182// FirebaseStream
163183FirebaseStream::FirebaseStream (const String& host, const String& auth,
164184 const String& path,
165- HTTPClient * http)
185+ FirebaseHttpClient * http)
166186 : FirebaseCall(host, auth, " STREAM" , path, " " , http) {
167187}
168188
0 commit comments