Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitd5dd303

Browse files
committed
Refactor out FirebaseCall
1 parentb2438cf commitd5dd303

File tree

4 files changed

+121
-159
lines changed

4 files changed

+121
-159
lines changed

‎Firebase.cpp

Lines changed: 66 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -18,72 +18,81 @@
1818
namespace {
1919
constchar*kFirebaseFingerprint ="7A 54 06 9B DC 7A 25 B3 86 8D 66 53 48 2C 0B 96 42 C7 B3 0A";
2020
constuint16_tkFirebasePort =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+
2134
}// namespace
2235

23-
Firebase::Firebase(const String& host) : connection_(host) {
36+
Firebase::Firebase(const String& host) : host_(host) {
37+
http_.setReuse(true);
2438
}
2539

2640
Firebase&Firebase::auth(const String& auth) {
27-
connection_.auth(auth);
41+
auth_ =auth;
2842
return *this;
2943
}
3044

31-
FirebaseResultFirebase::get(const String& path) {
32-
returnconnection_.sendRequestGetBody("GET", path);
45+
FirebaseCallFirebase::get(const String& path) {
46+
returnFirebaseCall(host_, auth_,"GET", path, &http_);
3347
}
3448

35-
FirebaseResultFirebase::push(const String& path,const String& value) {
36-
returnconnection_.sendRequestGetBody("POST", path, value);
49+
FirebaseCallFirebase::push(const String& path,const String& value) {
50+
returnFirebaseCall(host_, auth_,"POST", path, value, &http_);
3751
}
3852

39-
FirebaseResultFirebase::remove(const String& path) {
40-
returnconnection_.sendRequest("DELETE", path);
53+
FirebaseCallFirebase::remove(const String& path) {
54+
returnFirebaseCall(host_, auth_,"DELETE", path, &http_);
4155
}
4256

43-
/* FirebaseEventStream*/
44-
45-
FirebaseEventStream::FirebaseEventStream(const String& host) : connection_(host) {
57+
FirebaseEventStreamFirebase::stream(const String& path) {
58+
returnFirebaseEventStream(host_, auth_, path);
4659
}
4760

48-
FirebaseEventStream&FirebaseEventStream::auth(const String& auth) {
49-
connection_.auth(auth);
50-
return *this;
51-
}
61+
/* FirebaseEventStream*/
5262

53-
FirebaseResultFirebaseEventStream::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);
5969
constchar* 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);
6373

6474
String location;
6575
// 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);
7383
}
74-
returnFirebaseResult(statusCode);
7584
}
7685

7786
boolFirebaseEventStream::connected() {
78-
returnconnection_.httpClient().connected();
87+
returnhttp_.connected();
7988
}
8089

8190
boolFirebaseEventStream::available() {
82-
returnconnection_.httpClient().getStreamPtr()->available();
91+
returnhttp_.getStreamPtr()->available();
8392
}
8493

8594
FirebaseEventStream::EventFirebaseEventStream::read(String& event) {
86-
auto client =connection_.httpClient().getStreamPtr();
95+
auto client =http_.getStreamPtr();
8796
Event type;
8897
String typeStr = client->readStringUntil('\n').substring(7);
8998
if (typeStr =="put") {
@@ -98,75 +107,43 @@ FirebaseEventStream::Event FirebaseEventStream::read(String& event) {
98107
return type;
99108
}
100109

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(constchar* method,const String& path) {
113-
returnsendRequest(method, path,"");
114-
}
115-
116-
FirebaseResultFirebaseConnection::sendRequest(constchar* 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-
returnFirebaseResult(statusCode);
121-
}
122-
123-
FirebaseResultFirebaseConnection::sendRequestGetBody(constchar* method,const String& path) {
124-
returnsendRequestGetBody(method, path,"");
110+
boolFirebaseEventStream::isError()const {
111+
return status_ <0;
125112
}
126113

127-
FirebaseResultFirebaseConnection::sendRequestGetBody(constchar* method,const String& path,const String& value) {
128-
FirebaseResult result =sendRequest(method, path, value);
129-
returnFirebaseResult(result.httpStatus(), http_.getString());
114+
StringFirebaseEventStream::errorMessage()const {
115+
return error_message_;
130116
}
131117

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+
constchar* 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_);
136127
}
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) {
151128
}
152129

153-
FirebaseResult::FirebaseResult(constFirebaseResult& other) {
154-
status_ = other.status_;
155-
response_ = other.response_;
130+
FirebaseCall::FirebaseCall(constString& host,const String& auth,
131+
constchar* method,const String& path,
132+
HTTPClient* http) : FirebaseCall(host, auth, method, path,"", http) {
156133
}
157134

158-
boolFirebaseResult::isOk()const {
135+
boolFirebaseCall::isOk()const {
159136
return status_ == HTTP_CODE_OK;
160137
}
161138

162-
boolFirebaseResult::isError()const {
139+
boolFirebaseCall::isError()const {
163140
return status_ <0;
164141
}
165142

166-
StringFirebaseResult::errorMessage()const {
167-
returnHTTPClient::errorToString(status_);
143+
StringFirebaseCall::errorMessage()const {
144+
returnerror_message_;
168145
}
169146

170-
constString&FirebaseResult::response()const {
171-
returnresponse_;
147+
StringFirebaseCall::rawResponse() {
148+
returnhttp_->getString();
172149
}

‎Firebase.h

Lines changed: 44 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -27,59 +27,67 @@
2727

2828
//TODO(edcoyne) split these into multiple files.
2929

30-
// Result from call to Firebase backend. ALWAYS check isError() before
31-
// expecting any data.
32-
classFirebaseResult {
30+
classFirebaseCall {
3331
public:
34-
FirebaseResult(int status);
35-
FirebaseResult(int status,const String& response);
36-
FirebaseResult(const FirebaseResult& result);
32+
FirebaseCall(const String& host,const String& auth,
33+
constchar* method,const String& path,const String& value,
34+
HTTPClient* http);
35+
FirebaseCall(const String& host,const String& auth,
36+
constchar* method,const String& path,
37+
HTTPClient* http);
3738

38-
// True if there was an error completeing call.
39+
40+
// True if there was an error completing call.
3941
boolisError()const;
4042
StringerrorMessage()const;
4143

4244
// True if http status code is 200(OK).
4345
boolisOk()const;
44-
// Message sent back from Firebase backend.
45-
const String&response()const;
46+
47+
// Message sent back from Firebase backend. This pulls value to local memory,
48+
// be careful if value can be large.
49+
StringrawResponse();
4650

4751
inthttpStatus()const {
4852
return status_;
4953
}
5054

5155
private:
56+
FirebaseCall(HTTPClient* http);
57+
58+
HTTPClient* http_;
59+
5260
int status_;
53-
Stringresponse_;
61+
Stringerror_message_;
5462
};
5563

56-
// Low level connection to Firebase backend, you probably want the
57-
// Firebase class below.
58-
classFirebaseConnection {
64+
classFirebaseEventStream {
5965
public:
60-
FirebaseConnection(const String& host);
61-
FirebaseConnection&auth(const String& auth);
66+
enum Event {
67+
UNKNOWN,
68+
PUT,
69+
PATCH
70+
};
6271

63-
const String&host() {
64-
return host_;
65-
}
72+
FirebaseEventStream(const String& host,const String& auth,const String& path);
6673

67-
HTTPClient&httpClient(){
68-
return http_;
69-
}
74+
// Read next event in stream.
75+
Eventread(String& event);
7076

71-
StringmakeURL(const String& path);
77+
// True if connected to backend.
78+
boolconnected();
7279

73-
FirebaseResultsendRequest(constchar* method,const String& path,const String& value);
74-
FirebaseResultsendRequest(constchar* method,const String& path);
80+
// True if there is an event available.
81+
boolavailable();
7582

76-
FirebaseResultsendRequestGetBody(constchar* method,const String& path);
77-
FirebaseResultsendRequestGetBody(constchar* method,const String& path,const String& value);
83+
// True if there was an error.
84+
boolisError()const;
85+
StringerrorMessage()const;
7886

7987
private:
8088
HTTPClient http_;
81-
const String host_;
82-
Stringauth_;
89+
int status_;
90+
Stringerror_message_;
8391
};
8492

8593
// Primary client to the Firebase backend.
@@ -90,43 +98,20 @@ class Firebase {
9098

9199
// Fetch result at "path" to a local variable. If the value is too large you will exceed
92100
// local memory.
93-
FirebaseResultget(const String& path);
101+
FirebaseCallget(const String& path);
94102

95103
// Add new value to list at "path", will return child name of new item.
96-
FirebaseResultpush(const String& path,const String& value);
104+
FirebaseCallpush(const String& path,const String& value);
97105

98106
// Deletes value at "path" from server.
99-
FirebaseResultremove(const String& path);
107+
FirebaseCallremove(const String& path);
100108

101-
private:
102-
FirebaseConnection connection_;
103-
};
104-
105-
// Listens on a stream of events from Firebase backend.
106-
classFirebaseEventStream {
107-
public:
108-
enum Event {
109-
UNKNOWN,
110-
PUT,
111-
PATCH
112-
};
113-
114-
FirebaseEventStream(const String& host);
115-
FirebaseEventStream&auth(const String& auth);
116-
117-
// Connect to backend and start receiving events.
118-
FirebaseResultconnect(const String& path);
119-
// Read next event in stream.
120-
Eventread(String& event);
121-
122-
// True if connected to backend.
123-
boolconnected();
124-
125-
// True if there is an event available.
126-
boolavailable();
109+
// Starts a stream of events that effect object at "path".
110+
FirebaseEventStreamstream(const String& path);
127111

128112
private:
129-
FirebaseConnection connection_;
113+
HTTPClient http_;
114+
String host_;
115+
String auth_;
130116
};
131-
132117
#endif// firebase_h

‎examples/FirebasePush_ESP8266/FirebasePush_ESP8266.ino

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,20 +38,20 @@ void setup() {
3838
Serial.println(WiFi.localIP());
3939

4040
// add a new entry.
41-
FirebaseResult push = fbase.push("/logs","{\".sv\":\"timestamp\"}");
41+
FirebaseCall push = fbase.push("/logs","{\".sv\":\"timestamp\"}");
4242
if (push.isError()) {
4343
Serial.println("Firebase request failed");
4444
Serial.println(push.errorMessage());
4545
return;
4646
}
4747

4848
// print response.
49-
Serial.println(push.message());
49+
Serial.println(push.rawResponse());
5050

5151
// print all entries.
52-
FirebaseResult fetch = fbase.get("/logs");
52+
FirebaseCall fetch = fbase.get("/logs");
5353
if (!fetch.isError()) {
54-
Serial.println(fetch.message());
54+
Serial.println(fetch.rawResponse());
5555
}
5656
}
5757

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp