18
18
namespace {
19
19
const char *kFirebaseFingerprint =" 7A 54 06 9B DC 7A 25 B3 86 8D 66 53 48 2C 0B 96 42 C7 B3 0A" ;
20
20
const uint16_t kFirebasePort =443 ;
21
+
22
+ StringmakeUrl (const String& path,const String& auth) {
23
+ String url;
24
+ if (path[0 ] !=' /' ) {
25
+ url =" /" ;
26
+ }
27
+ url += path +" .json" ;
28
+ if (auth.length () >0 ) {
29
+ url +=" ?auth=" + auth;
30
+ }
31
+ return url;
32
+ }
33
+
21
34
}// namespace
22
35
23
- Firebase::Firebase (const String& host) : connection_(host) {
36
+ Firebase::Firebase (const String& host) : host_(host) {
37
+ http_.setReuse (true );
24
38
}
25
39
26
40
Firebase&Firebase::auth (const String& auth) {
27
- connection_. auth (auth) ;
41
+ auth_ = auth;
28
42
return *this ;
29
43
}
30
44
31
- FirebaseResult Firebase::get (const String& path) {
32
- return connection_. sendRequestGetBody ( " GET" , path);
45
+ FirebaseCall Firebase::get (const String& path) {
46
+ return FirebaseCall (host_, auth_, " GET" , path, &http_ );
33
47
}
34
48
35
- FirebaseResult Firebase::push (const String& path,const String& value) {
36
- return connection_. sendRequestGetBody ( " POST" , path, value);
49
+ FirebaseCall Firebase::push (const String& path,const String& value) {
50
+ return FirebaseCall (host_, auth_, " POST" , path, value, &http_ );
37
51
}
38
52
39
- FirebaseResult Firebase::remove (const String& path) {
40
- return connection_. sendRequest ( " DELETE" , path);
53
+ FirebaseCall Firebase::remove (const String& path) {
54
+ return FirebaseCall (host_, auth_, " DELETE" , path, &http_ );
41
55
}
42
56
43
- /* FirebaseEventStream*/
44
-
45
- FirebaseEventStream::FirebaseEventStream (const String& host) : connection_(host) {
57
+ FirebaseEventStreamFirebase::stream (const String& path) {
58
+ return FirebaseEventStream (host_, auth_, path);
46
59
}
47
60
48
- FirebaseEventStream&FirebaseEventStream::auth (const String& auth) {
49
- connection_.auth (auth);
50
- return *this ;
51
- }
61
+ /* FirebaseEventStream*/
52
62
53
- FirebaseResult FirebaseEventStream::connect (const String&path) {
54
- String url = connection_. makeURL ( path);
55
- auto & http = connection_. httpClient ( );
56
- http .setReuse (true );
57
- http .begin (connection_. host () .c_str (),kFirebasePort , url.c_str (),true ,
58
- kFirebaseFingerprint );
63
+ FirebaseEventStream::FirebaseEventStream (const String&host, const String& auth,
64
+ const String& path) {
65
+ const String url = makeUrl (path, auth );
66
+ http_ .setReuse (true );
67
+ http_ .begin (host.c_str (),kFirebasePort , url.c_str (),true ,
68
+ kFirebaseFingerprint );
59
69
const char * headers[] = {" Location" };
60
- http .collectHeaders (headers,1 );
61
- http .addHeader (" Accept" ," text/event-stream" );
62
- int statusCode = http .sendRequest (" GET" , (uint8_t *)NULL ,0 );
70
+ http_ .collectHeaders (headers,1 );
71
+ http_ .addHeader (" Accept" ," text/event-stream" );
72
+ status_ = http_ .sendRequest (" GET" , (uint8_t *)NULL ,0 );
63
73
64
74
String location;
65
75
// TODO(proppy): Add a max redirect check
66
- while (statusCode == HTTP_CODE_TEMPORARY_REDIRECT) {
67
- location =http .header (" Location" );
68
- http .setReuse (false );
69
- http .end ();
70
- http .setReuse (true );
71
- http .begin (location,kFirebaseFingerprint );
72
- statusCode =http .sendRequest (" GET" , (uint8_t *)NULL ,0 );
76
+ while (status_ == HTTP_CODE_TEMPORARY_REDIRECT) {
77
+ location =http_ .header (" Location" );
78
+ http_ .setReuse (false );
79
+ http_ .end ();
80
+ http_ .setReuse (true );
81
+ http_ .begin (location,kFirebaseFingerprint );
82
+ status_ =http_ .sendRequest (" GET" , (uint8_t *)NULL ,0 );
73
83
}
74
- return FirebaseResult (statusCode);
75
84
}
76
85
77
86
bool FirebaseEventStream::connected () {
78
- return connection_. httpClient () .connected ();
87
+ return http_ .connected ();
79
88
}
80
89
81
90
bool FirebaseEventStream::available () {
82
- return connection_. httpClient () .getStreamPtr ()->available ();
91
+ return http_ .getStreamPtr ()->available ();
83
92
}
84
93
85
94
FirebaseEventStream::EventFirebaseEventStream::read (String& event) {
86
- auto client =connection_. httpClient () .getStreamPtr ();
95
+ auto client =http_ .getStreamPtr ();
87
96
Event type;
88
97
String typeStr = client->readStringUntil (' \n ' ).substring (7 );
89
98
if (typeStr ==" put" ) {
@@ -98,75 +107,43 @@ FirebaseEventStream::Event FirebaseEventStream::read(String& event) {
98
107
return type;
99
108
}
100
109
101
- /* FirebaseConnection*/
102
-
103
- FirebaseConnection::FirebaseConnection (const String& host) : host_(host) {
104
- http_.setReuse (true );
105
- }
106
-
107
- FirebaseConnection&FirebaseConnection::auth (const String& auth) {
108
- auth_ = auth;
109
- return *this ;
110
- }
111
-
112
- FirebaseResultFirebaseConnection::sendRequest (const char * method,const String& path) {
113
- return sendRequest (method, path," " );
114
- }
115
-
116
- FirebaseResultFirebaseConnection::sendRequest (const char * method,const String& path,const String& value) {
117
- const String url =makeURL (path);
118
- http_.begin (host_.c_str (),kFirebasePort , url.c_str (),true ,kFirebaseFingerprint );
119
- int statusCode = http_.sendRequest (method, (uint8_t *)value.c_str (), value.length ());
120
- return FirebaseResult (statusCode);
121
- }
122
-
123
- FirebaseResultFirebaseConnection::sendRequestGetBody (const char * method,const String& path) {
124
- return sendRequestGetBody (method, path," " );
110
+ bool FirebaseEventStream::isError ()const {
111
+ return status_ <0 ;
125
112
}
126
113
127
- FirebaseResultFirebaseConnection::sendRequestGetBody (const char * method,const String& path,const String& value) {
128
- FirebaseResult result =sendRequest (method, path, value);
129
- return FirebaseResult (result.httpStatus (), http_.getString ());
114
+ StringFirebaseEventStream::errorMessage ()const {
115
+ return error_message_;
130
116
}
131
117
132
- StringFirebaseConnection::makeURL (const String& path) {
133
- String url;
134
- if (path[0 ] !=' /' ) {
135
- url =" /" ;
118
+ /* FirebaseCall*/
119
+ FirebaseCall::FirebaseCall (const String& host,const String& auth,
120
+ const char * method,const String& path,const String& value,
121
+ HTTPClient* http) : http_(http) {
122
+ const String url =makeUrl (path, auth);
123
+ http_->begin (host.c_str (),kFirebasePort , url.c_str (),true ,kFirebaseFingerprint );
124
+ status_ = http_->sendRequest (method, (uint8_t *)value.c_str (), value.length ());
125
+ if (isError ()) {
126
+ error_message_ =String (method) +" " + url +" :" +HTTPClient::errorToString (status_);
136
127
}
137
- url += path +" .json" ;
138
- if (auth_.length () >0 ) {
139
- url +=" ?auth=" + auth_;
140
- }
141
- return url;
142
- }
143
-
144
- /* FirebaseResult*/
145
-
146
- FirebaseResult::FirebaseResult (int status) : status_(status) {
147
- }
148
-
149
- FirebaseResult::FirebaseResult (int status,const String& response)
150
- : status_(status), response_(response) {
151
128
}
152
129
153
- FirebaseResult::FirebaseResult (const FirebaseResult& other) {
154
- status_ = other. status_ ;
155
- response_ = other. response_ ;
130
+ FirebaseCall::FirebaseCall (const String& host, const String& auth,
131
+ const char * method, const String& path,
132
+ HTTPClient* http) : FirebaseCall(host, auth, method, path, " " , http) {
156
133
}
157
134
158
- bool FirebaseResult ::isOk ()const {
135
+ bool FirebaseCall ::isOk ()const {
159
136
return status_ == HTTP_CODE_OK;
160
137
}
161
138
162
- bool FirebaseResult ::isError ()const {
139
+ bool FirebaseCall ::isError ()const {
163
140
return status_ <0 ;
164
141
}
165
142
166
- StringFirebaseResult ::errorMessage ()const {
167
- return HTTPClient::errorToString (status_) ;
143
+ StringFirebaseCall ::errorMessage ()const {
144
+ return error_message_ ;
168
145
}
169
146
170
- const String& FirebaseResult::response () const {
171
- return response_ ;
147
+ StringFirebaseCall::rawResponse () {
148
+ return http_-> getString () ;
172
149
}