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,45 +33,75 @@ String makeFirebaseURL(const String& path, const String& auth) {
41
33
} // namespace
42
34
43
35
Firebase::Firebase (const String& host) : host_(host) {
44
- http_.setReuse (true );
36
+ http_.reset (FirebaseHttpClient::create ());
37
+ http_->setReuseConnection (true );
45
38
}
46
39
47
40
Firebase& Firebase::auth (const String& auth) {
48
41
auth_ = auth;
49
42
return *this ;
50
43
}
51
44
45
+ const String& Firebase::auth () {
46
+ return auth_;
47
+ }
48
+
52
49
FirebaseGet 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 ()));
54
55
}
55
56
56
57
FirebaseSet 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 ()));
58
65
}
59
66
60
67
FirebasePush 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 ()));
62
73
}
63
74
64
75
FirebaseRemove 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 ()));
66
82
}
67
83
68
84
FirebaseStream Firebase::stream (const String& path) {
69
85
// 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 ()));
71
93
}
72
94
73
95
// FirebaseCall
74
96
FirebaseCall::FirebaseCall (const String& host, const String& auth,
75
97
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 );
80
102
81
103
bool followRedirect = false ;
82
- if (method == " STREAM" ) {
104
+ if (String ( method) == " STREAM" ) {
83
105
method = " GET" ;
84
106
http_->addHeader (" Accept" , " text/event-stream" );
85
107
followRedirect = true ;
@@ -90,26 +112,24 @@ FirebaseCall::FirebaseCall(const String& host, const String& auth,
90
112
http_->collectHeaders (headers, 1 );
91
113
}
92
114
93
- int status = http_->sendRequest (method, ( uint8_t *) data. c_str (), data. length () );
115
+ int status = http_->sendRequest (method, data);
94
116
95
117
// TODO: Add a max redirect check
96
118
if (followRedirect) {
97
- while (status == HTTP_CODE_TEMPORARY_REDIRECT ) {
119
+ while (status == HttpStatus::TEMPORARY_REDIRECT ) {
98
120
String location = http_->header (" Location" );
99
- http_->setReuse (false );
121
+ http_->setReuseConnection (false );
100
122
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 () );
104
126
}
105
127
}
106
128
107
129
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));
113
133
}
114
134
115
135
// if not streaming.
@@ -128,14 +148,14 @@ const JsonObject& FirebaseCall::json() {
128
148
// FirebaseGet
129
149
FirebaseGet::FirebaseGet (const String& host, const String& auth,
130
150
const String& path,
131
- HTTPClient * http)
151
+ FirebaseHttpClient * http)
132
152
: FirebaseCall(host, auth, " GET" , path, " " , http) {
133
153
}
134
154
135
155
// FirebaseSet
136
156
FirebaseSet::FirebaseSet (const String& host, const String& auth,
137
157
const String& path, const String& value,
138
- HTTPClient * http)
158
+ FirebaseHttpClient * http)
139
159
: FirebaseCall(host, auth, " PUT" , path, value, http) {
140
160
if (!error ()) {
141
161
// TODO: parse json
@@ -145,24 +165,24 @@ FirebaseSet::FirebaseSet(const String& host, const String& auth,
145
165
// FirebasePush
146
166
FirebasePush::FirebasePush (const String& host, const String& auth,
147
167
const String& path, const String& value,
148
- HTTPClient * http)
168
+ FirebaseHttpClient * http)
149
169
: FirebaseCall(host, auth, " POST" , path, value, http) {
150
170
if (!error ()) {
151
- name_ = json ()[" name" ].as <const char *>();
171
+ // name_ = json()["name"].as<const char*>();
152
172
}
153
173
}
154
174
155
175
// FirebasePush
156
176
FirebaseRemove::FirebaseRemove (const String& host, const String& auth,
157
177
const String& path,
158
- HTTPClient * http)
178
+ FirebaseHttpClient * http)
159
179
: FirebaseCall(host, auth, " DELETE" , path, " " , http) {
160
180
}
161
181
162
182
// FirebaseStream
163
183
FirebaseStream::FirebaseStream (const String& host, const String& auth,
164
184
const String& path,
165
- HTTPClient * http)
185
+ FirebaseHttpClient * http)
166
186
: FirebaseCall(host, auth, " STREAM" , path, " " , http) {
167
187
}
168
188
0 commit comments