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,70 @@ 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 );
38
+ }
39
+
40
+ const String& Firebase::auth () const {
41
+ return auth_;
45
42
}
46
43
47
44
FirebaseGet 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 ()));
49
50
}
50
51
51
52
FirebaseSet 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 ()));
53
60
}
54
61
55
62
FirebasePush 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 ()));
57
68
}
58
69
59
70
FirebaseRemove 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 ()));
61
77
}
62
78
63
79
FirebaseStream Firebase::stream (const String& path) {
64
80
// 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 ()));
66
88
}
67
89
68
90
// FirebaseCall
69
91
FirebaseCall::FirebaseCall (const String& host, const String& auth,
70
92
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 );
75
97
76
98
bool followRedirect = false ;
77
- if (method == " STREAM" ) {
99
+ if (String ( method) == " STREAM" ) {
78
100
method = " GET" ;
79
101
http_->addHeader (" Accept" , " text/event-stream" );
80
102
followRedirect = true ;
@@ -85,26 +107,24 @@ FirebaseCall::FirebaseCall(const String& host, const String& auth,
85
107
http_->collectHeaders (headers, 1 );
86
108
}
87
109
88
- int status = http_->sendRequest (method, ( uint8_t *) data. c_str (), data. length () );
110
+ int status = http_->sendRequest (method, data);
89
111
90
112
// TODO: Add a max redirect check
91
113
if (followRedirect) {
92
- while (status == HTTP_CODE_TEMPORARY_REDIRECT ) {
114
+ while (status == HttpStatus::TEMPORARY_REDIRECT ) {
93
115
String location = http_->header (" Location" );
94
- http_->setReuse (false );
116
+ http_->setReuseConnection (false );
95
117
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 () );
99
121
}
100
122
}
101
123
102
124
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));
108
128
}
109
129
110
130
// if not streaming.
@@ -123,14 +143,14 @@ const JsonObject& FirebaseCall::json() {
123
143
// FirebaseGet
124
144
FirebaseGet::FirebaseGet (const String& host, const String& auth,
125
145
const String& path,
126
- HTTPClient * http)
146
+ FirebaseHttpClient * http)
127
147
: FirebaseCall(host, auth, " GET" , path, " " , http) {
128
148
}
129
149
130
150
// FirebaseSet
131
151
FirebaseSet::FirebaseSet (const String& host, const String& auth,
132
152
const String& path, const String& value,
133
- HTTPClient * http)
153
+ FirebaseHttpClient * http)
134
154
: FirebaseCall(host, auth, " PUT" , path, value, http) {
135
155
if (!error ()) {
136
156
// TODO: parse json
@@ -140,7 +160,7 @@ FirebaseSet::FirebaseSet(const String& host, const String& auth,
140
160
// FirebasePush
141
161
FirebasePush::FirebasePush (const String& host, const String& auth,
142
162
const String& path, const String& value,
143
- HTTPClient * http)
163
+ FirebaseHttpClient * http)
144
164
: FirebaseCall(host, auth, " POST" , path, value, http) {
145
165
if (!error ()) {
146
166
name_ = json ()[" name" ].as <const char *>();
@@ -150,14 +170,14 @@ FirebasePush::FirebasePush(const String& host, const String& auth,
150
170
// FirebasePush
151
171
FirebaseRemove::FirebaseRemove (const String& host, const String& auth,
152
172
const String& path,
153
- HTTPClient * http)
173
+ FirebaseHttpClient * http)
154
174
: FirebaseCall(host, auth, " DELETE" , path, " " , http) {
155
175
}
156
176
157
177
// FirebaseStream
158
178
FirebaseStream::FirebaseStream (const String& host, const String& auth,
159
179
const String& path,
160
- HTTPClient * http)
180
+ FirebaseHttpClient * http)
161
181
: FirebaseCall(host, auth, " STREAM" , path, " " , http) {
162
182
}
163
183
0 commit comments