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,40 +33,70 @@ String makeFirebaseURL(const String& path, const String& auth) {
4133} // namespace
4234
4335Firebase::Firebase (const String& host, const String& auth) : host_(host), auth_(auth) {
44- http_.setReuse (true );
36+ http_.reset (FirebaseHttpClient::create ());
37+ http_->setReuseConnection (true );
38+ }
39+
40+ const String& Firebase::auth () const {
41+ return auth_;
4542}
4643
4744FirebaseGet Firebase::get (const String& path) {
48- return FirebaseGet (host_, auth_, path, &http_);
45+ return FirebaseGet (host_, auth_, path, http_.get ());
46+ }
47+
48+ unique_ptr<FirebaseGet> Firebase::getPtr (const String& path) {
49+ return unique_ptr<FirebaseGet>(new FirebaseGet (host_, auth_, path, http_.get ()));
4950}
5051
5152FirebaseSet Firebase::set (const String& path, const String& value) {
52- return FirebaseSet (host_, auth_, path, value, &http_);
53+ return FirebaseSet (host_, auth_, path, value, http_.get ());
54+ }
55+
56+ unique_ptr<FirebaseSet> Firebase::setPtr (const String& path,
57+ const String& value) {
58+ return unique_ptr<FirebaseSet>(
59+ new FirebaseSet (host_, auth_, path, value, http_.get ()));
5360}
5461
5562FirebasePush Firebase::push (const String& path, const String& value) {
56- return FirebasePush (host_, auth_, path, value, &http_);
63+ return FirebasePush (host_, auth_, path, value, http_.get ());
64+ }
65+ unique_ptr<FirebasePush> Firebase::pushPtr (const String& path, const String& value) {
66+ return unique_ptr<FirebasePush>(
67+ new FirebasePush (host_, auth_, path, value, http_.get ()));
5768}
5869
5970FirebaseRemove Firebase::remove (const String& path) {
60- return FirebaseRemove (host_, auth_, path, &http_);
71+ return FirebaseRemove (host_, auth_, path, http_.get ());
72+ }
73+
74+ unique_ptr<FirebaseRemove> Firebase::removePtr (const String& path) {
75+ return unique_ptr<FirebaseRemove>(
76+ new FirebaseRemove (host_, auth_, path, http_.get ()));
6177}
6278
6379FirebaseStream Firebase::stream (const String& path) {
6480 // TODO: create new client dedicated to stream.
65- return FirebaseStream (host_, auth_, path, &http_);
81+ return FirebaseStream (host_, auth_, path, http_.get ());
82+ }
83+
84+ unique_ptr<FirebaseStream> Firebase::streamPtr (const String& path) {
85+ // TODO: create new client dedicated to stream.
86+ return unique_ptr<FirebaseStream>(
87+ new FirebaseStream (host_, auth_, path, http_.get ()));
6688}
6789
6890// FirebaseCall
6991FirebaseCall::FirebaseCall (const String& host, const String& auth,
7092 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 );
93+ const String& data, FirebaseHttpClient * http) : http_(http) {
94+ String path_with_auth = makeFirebaseURL (path, auth);
95+ http_->setReuseConnection (true );
96+ http_->begin (host, path_with_auth );
7597
7698 bool followRedirect = false ;
77- if (method == " STREAM" ) {
99+ if (String ( method) == " STREAM" ) {
78100 method = " GET" ;
79101 http_->addHeader (" Accept" , " text/event-stream" );
80102 followRedirect = true ;
@@ -85,26 +107,24 @@ FirebaseCall::FirebaseCall(const String& host, const String& auth,
85107 http_->collectHeaders (headers, 1 );
86108 }
87109
88- int status = http_->sendRequest (method, ( uint8_t *) data. c_str (), data. length () );
110+ int status = http_->sendRequest (method, data);
89111
90112 // TODO: Add a max redirect check
91113 if (followRedirect) {
92- while (status == HTTP_CODE_TEMPORARY_REDIRECT ) {
114+ while (status == HttpStatus::TEMPORARY_REDIRECT ) {
93115 String location = http_->header (" Location" );
94- http_->setReuse (false );
116+ http_->setReuseConnection (false );
95117 http_->end ();
96- http_->setReuse (true );
97- http_->begin (location, kFirebaseFingerprint );
98- status = http_->sendRequest (" GET" , ( uint8_t *) NULL , 0 );
118+ http_->setReuseConnection (true );
119+ http_->begin (location);
120+ status = http_->sendRequest (" GET" , String () );
99121 }
100122 }
101123
102124 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
125+ error_ = FirebaseError (status,
126+ String (method) + " " + path_with_auth +
127+ " : " + http_->errorToString (status));
108128 }
109129
110130 // if not streaming.
@@ -123,14 +143,14 @@ const JsonObject& FirebaseCall::json() {
123143// FirebaseGet
124144FirebaseGet::FirebaseGet (const String& host, const String& auth,
125145 const String& path,
126- HTTPClient * http)
146+ FirebaseHttpClient * http)
127147 : FirebaseCall(host, auth, " GET" , path, " " , http) {
128148}
129149
130150// FirebaseSet
131151FirebaseSet::FirebaseSet (const String& host, const String& auth,
132152 const String& path, const String& value,
133- HTTPClient * http)
153+ FirebaseHttpClient * http)
134154 : FirebaseCall(host, auth, " PUT" , path, value, http) {
135155 if (!error ()) {
136156 // TODO: parse json
@@ -140,7 +160,7 @@ FirebaseSet::FirebaseSet(const String& host, const String& auth,
140160// FirebasePush
141161FirebasePush::FirebasePush (const String& host, const String& auth,
142162 const String& path, const String& value,
143- HTTPClient * http)
163+ FirebaseHttpClient * http)
144164 : FirebaseCall(host, auth, " POST" , path, value, http) {
145165 if (!error ()) {
146166 name_ = json ()[" name" ].as <const char *>();
@@ -150,14 +170,14 @@ FirebasePush::FirebasePush(const String& host, const String& auth,
150170// FirebasePush
151171FirebaseRemove::FirebaseRemove (const String& host, const String& auth,
152172 const String& path,
153- HTTPClient * http)
173+ FirebaseHttpClient * http)
154174 : FirebaseCall(host, auth, " DELETE" , path, " " , http) {
155175}
156176
157177// FirebaseStream
158178FirebaseStream::FirebaseStream (const String& host, const String& auth,
159179 const String& path,
160- HTTPClient * http)
180+ FirebaseHttpClient * http)
161181 : FirebaseCall(host, auth, " STREAM" , path, " " , http) {
162182}
163183
0 commit comments