15
15
//
16
16
#include " Firebase.h"
17
17
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;
24
19
25
20
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
-
29
21
String makeFirebaseURL (const String& path, const String& auth) {
30
22
String url;
31
23
if (path[0 ] != ' /' ) {
@@ -41,40 +33,66 @@ String makeFirebaseURL(const String& path, const String& auth) {
41
33
} // namespace
42
34
43
35
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 );
45
38
}
46
39
47
40
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 ()));
49
46
}
50
47
51
48
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 ()));
53
56
}
54
57
55
58
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 ()));
57
64
}
58
65
59
66
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 ()));
61
73
}
62
74
63
75
FirebaseStream Firebase::stream (const String& path) {
64
76
// 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 ()));
66
84
}
67
85
68
86
// FirebaseCall
69
87
FirebaseCall::FirebaseCall (const String& host, const String& auth,
70
88
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 );
75
93
76
94
bool followRedirect = false ;
77
- if (method == " STREAM" ) {
95
+ if (String ( method) == " STREAM" ) {
78
96
method = " GET" ;
79
97
http_->addHeader (" Accept" , " text/event-stream" );
80
98
followRedirect = true ;
@@ -85,26 +103,24 @@ FirebaseCall::FirebaseCall(const String& host, const String& auth,
85
103
http_->collectHeaders (headers, 1 );
86
104
}
87
105
88
- int status = http_->sendRequest (method, ( uint8_t *) data. c_str (), data. length () );
106
+ int status = http_->sendRequest (method, data);
89
107
90
108
// TODO: Add a max redirect check
91
109
if (followRedirect) {
92
- while (status == HTTP_CODE_TEMPORARY_REDIRECT ) {
110
+ while (status == HttpStatus::TEMPORARY_REDIRECT ) {
93
111
String location = http_->header (" Location" );
94
- http_->setReuse (false );
112
+ http_->setReuseConnection (false );
95
113
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 () );
99
117
}
100
118
}
101
119
102
120
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));
108
124
}
109
125
110
126
// if not streaming.
@@ -123,14 +139,14 @@ const JsonObject& FirebaseCall::json() {
123
139
// FirebaseGet
124
140
FirebaseGet::FirebaseGet (const String& host, const String& auth,
125
141
const String& path,
126
- HTTPClient * http)
142
+ FirebaseHttpClient * http)
127
143
: FirebaseCall(host, auth, " GET" , path, " " , http) {
128
144
}
129
145
130
146
// FirebaseSet
131
147
FirebaseSet::FirebaseSet (const String& host, const String& auth,
132
148
const String& path, const String& value,
133
- HTTPClient * http)
149
+ FirebaseHttpClient * http)
134
150
: FirebaseCall(host, auth, " PUT" , path, value, http) {
135
151
if (!error ()) {
136
152
// TODO: parse json
@@ -140,7 +156,7 @@ FirebaseSet::FirebaseSet(const String& host, const String& auth,
140
156
// FirebasePush
141
157
FirebasePush::FirebasePush (const String& host, const String& auth,
142
158
const String& path, const String& value,
143
- HTTPClient * http)
159
+ FirebaseHttpClient * http)
144
160
: FirebaseCall(host, auth, " POST" , path, value, http) {
145
161
if (!error ()) {
146
162
name_ = json ()[" name" ].as <const char *>();
@@ -150,14 +166,14 @@ FirebasePush::FirebasePush(const String& host, const String& auth,
150
166
// FirebasePush
151
167
FirebaseRemove::FirebaseRemove (const String& host, const String& auth,
152
168
const String& path,
153
- HTTPClient * http)
169
+ FirebaseHttpClient * http)
154
170
: FirebaseCall(host, auth, " DELETE" , path, " " , http) {
155
171
}
156
172
157
173
// FirebaseStream
158
174
FirebaseStream::FirebaseStream (const String& host, const String& auth,
159
175
const String& path,
160
- HTTPClient * http)
176
+ FirebaseHttpClient * http)
161
177
: FirebaseCall(host, auth, " STREAM" , path, " " , http) {
162
178
}
163
179
0 commit comments