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

Commit3d05b41

Browse files
committed
Refactoring into on-demand classes and http connections
1 parented75682 commit3d05b41

File tree

5 files changed

+109
-253
lines changed

5 files changed

+109
-253
lines changed

‎src/Firebase.cpp

Lines changed: 31 additions & 137 deletions
Original file line numberDiff line numberDiff line change
@@ -42,115 +42,16 @@ const std::string& Firebase::auth() const {
4242
return auth_;
4343
}
4444

45-
FirebaseGetFirebase::get(const std::string& path) {
46-
returnFirebaseGet(host_, auth_, path, http_);
47-
}
48-
49-
unique_ptr<FirebaseGet>Firebase::getPtr(const std::string& path) {
50-
return unique_ptr<FirebaseGet>(newFirebaseGet(host_, auth_, path, http_));
51-
}
52-
53-
FirebaseSetFirebase::set(const std::string& path,const std::string& value) {
54-
returnFirebaseSet(host_, auth_, path, value, http_);
55-
}
56-
57-
unique_ptr<FirebaseSet>Firebase::setPtr(const std::string& path,
58-
const std::string& value) {
59-
return unique_ptr<FirebaseSet>(
60-
newFirebaseSet(host_, auth_, path, value, http_));
61-
}
62-
63-
FirebasePushFirebase::push(const std::string& path,const std::string& value) {
64-
returnFirebasePush(host_, auth_, path, value, http_);
65-
}
66-
67-
unique_ptr<FirebasePush>Firebase::pushPtr(const std::string& path,const std::string& value) {
68-
return unique_ptr<FirebasePush>(
69-
newFirebasePush(host_, auth_, path, value, http_));
70-
}
71-
72-
FirebaseRemoveFirebase::remove(const std::string& path) {
73-
returnFirebaseRemove(host_, auth_, path, http_);
74-
}
75-
76-
unique_ptr<FirebaseRemove>Firebase::removePtr(const std::string& path) {
77-
return unique_ptr<FirebaseRemove>(
78-
newFirebaseRemove(host_, auth_, path, http_));
79-
}
80-
81-
FirebaseStreamFirebase::stream(const std::string& path) {
82-
// TODO: create new client dedicated to stream.
83-
returnFirebaseStream(host_, auth_, path, http_);
84-
}
85-
86-
unique_ptr<FirebaseStream>Firebase::streamPtr(const std::string& path) {
87-
// TODO: create new client dedicated to stream.
88-
return unique_ptr<FirebaseStream>(
89-
newFirebaseStream(host_, auth_, path, http_));
90-
}
91-
92-
// FirebaseCall
93-
FirebaseCall::FirebaseCall(const std::string& host,const std::string& auth,
94-
constchar* method,const std::string& path,
95-
const std::string& data,const std::shared_ptr<FirebaseHttpClient> http) : http_(http) {
96-
std::string path_with_auth =makeFirebaseURL(path, auth);
97-
if ((method =="STREAM") && (path == http->getStreamingPath())){
98-
// already streaming requested path.
99-
return;
100-
}
101-
if (http_->isStreaming()) {
102-
// closing streaming connection.
103-
http_->setReuseConnection(false);
104-
http_->end();
105-
}
106-
http_->setReuseConnection(true);
107-
http_->begin(host, path_with_auth);
108-
109-
bool followRedirect =false;
110-
if (std::string(method) =="STREAM") {
111-
method ="GET";
112-
http_->addHeader("Accept","text/event-stream");
113-
followRedirect =true;
114-
}
115-
116-
if (followRedirect) {
117-
constchar* headers[] = {"Location"};
118-
http_->collectHeaders(headers,1);
119-
}
120-
121-
int status = http_->sendRequest(method, data);
122-
123-
// TODO: Add a max redirect check
124-
if (followRedirect) {
125-
while (status == HttpStatus::TEMPORARY_REDIRECT) {
126-
std::string location = http_->header("Location");
127-
http_->setReuseConnection(false);
128-
http_->end();
129-
http_->setReuseConnection(true);
130-
http_->begin(location);
131-
status = http_->sendRequest("GET",std::string());
132-
}
133-
}
134-
135-
if (status !=200) {
45+
voidFirebaseCall::analyzeError(char* method,int status,const std::string& path_with_auth) {
46+
if (status !=200) {
13647
error_ =FirebaseError(status,
13748
std::string(method) +"" + path_with_auth +
13849
":" + http_->errorToString(status));
13950
}
140-
141-
// if not streaming.
142-
if (!followRedirect) {
143-
response_ = http_->getString();
144-
http_->setStreaming("");
145-
}else {
146-
http_->setStreaming(path);
147-
}
14851
}
14952

15053
FirebaseCall::~FirebaseCall() {
151-
if (http_ && !http_->isStreaming()) {
152-
http_->end();
153-
}
54+
http_->end();
15455
}
15556

15657
const JsonObject&FirebaseCall::json() {
@@ -162,46 +63,39 @@ const JsonObject& FirebaseCall::json() {
16263
return buffer_.get()->parseObject(response().c_str());
16364
}
16465

165-
// FirebaseGet
166-
FirebaseGet::FirebaseGet(const std::string& host,const std::string& auth,
167-
const std::string& path,
168-
const std::shared_ptr<FirebaseHttpClient> http)
169-
: FirebaseCall(host, auth,"GET", path,"", http) {
66+
// FirebaseRequest
67+
intFirebaseRequest::sendRequest(
68+
const std::string& host,const std::string& auth,
69+
char* method,const std::string& path,const std::string& data) {
70+
std::string path_with_auth =makeFirebaseURL(path, auth);
71+
http_->setReuseConnection(true);
72+
http_->begin(host, path_with_auth);
73+
int status = http_->sendRequest(method, data);
74+
analyzeError(method, status, path_with_auth);
75+
response_ = http_->getString();
17076
}
17177

172-
// FirebaseSet
173-
FirebaseSet::FirebaseSet(const std::string& host,const std::string& auth,
174-
const std::string& path,const std::string& value,
175-
const std::shared_ptr<FirebaseHttpClient> http)
176-
: FirebaseCall(host, auth,"PUT", path, value, http) {
177-
if (!error()) {
178-
// TODO: parse json
179-
json_ =response();
180-
}
181-
}
78+
// FirebaseStream
79+
voidFirebaseStream::startStreaming(const std::string& host,const std::string& auth,const std::string& path) {
80+
std::string path_with_auth =makeFirebaseURL(path, auth);
81+
http_->setReuseConnection(true);
82+
http_->begin(host, path_with_auth);
18283

183-
// FirebasePush
184-
FirebasePush::FirebasePush(const std::string& host,const std::string& auth,
185-
const std::string& path,const std::string& value,
186-
const std::shared_ptr<FirebaseHttpClient> http)
187-
: FirebaseCall(host, auth,"POST", path, value, http) {
188-
if (!error()) {
189-
name_ =json()["name"].as<constchar*>();
190-
}
191-
}
84+
http_->addHeader("Accept","text/event-stream");
85+
constchar* headers[] = {"Location"};
86+
http_->collectHeaders(headers,1);
19287

193-
// FirebaseRemove
194-
FirebaseRemove::FirebaseRemove(const std::string& host,const std::string& auth,
195-
const std::string& path,
196-
const std::shared_ptr<FirebaseHttpClient> http)
197-
: FirebaseCall(host, auth,"DELETE", path,"", http) {
198-
}
88+
int status = http_->sendRequest("GET","");
89+
analyzeError("STREAM", status, path_with_auth);
19990

200-
// FirebaseStream
201-
FirebaseStream::FirebaseStream(const std::string& host,const std::string& auth,
202-
const std::string& path,
203-
const std::shared_ptr<FirebaseHttpClient> http)
204-
: FirebaseCall(host, auth,"STREAM", path,"", http) {
91+
while (status == HttpStatus::TEMPORARY_REDIRECT) {
92+
std::string location = http_->header("Location");
93+
http_->setReuseConnection(false);
94+
http_->end();
95+
http_->setReuseConnection(true);
96+
http_->begin(location);
97+
status = http_->sendRequest("GET",std::string());
98+
}
20599
}
206100

207101
boolFirebaseStream::available() {

‎src/Firebase.h

Lines changed: 20 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,6 @@
2929
#include"FirebaseError.h"
3030
#include"FirebaseObject.h"
3131

32-
classFirebaseGet;
33-
classFirebaseSet;
34-
classFirebasePush;
35-
classFirebaseRemove;
36-
classFirebaseStream;
37-
3832
// Firebase REST API client.
3933
classFirebase {
4034
public:
@@ -43,24 +37,19 @@ class Firebase {
4337
const std::string&auth()const;
4438

4539
// Fetch json encoded `value` at `path`.
46-
FirebaseGetget(const std::string& path);
47-
virtual std::unique_ptr<FirebaseGet>getPtr(const std::string& path);
40+
voidget(const std::string& path);
4841

4942
// Set json encoded `value` at `path`.
50-
FirebaseSetset(const std::string& path,const std::string& json);
51-
virtual std::unique_ptr<FirebaseSet>setPtr(const std::string& path,const std::string& json);
43+
voidset(const std::string& path,const std::string& json);
5244

5345
// Add new json encoded `value` to list at `path`.
54-
FirebasePushpush(const std::string& path,const std::string& json);
55-
virtual std::unique_ptr<FirebasePush>pushPtr(const std::string& path,const std::string& json);
46+
voidpush(const std::string& path,const std::string& json);
5647

5748
// Delete value at `path`.
58-
FirebaseRemoveremove(const std::string& path);
59-
virtual std::unique_ptr<FirebaseRemove>removePtr(const std::string& path);
49+
voidremove(const std::string& path);
6050

6151
// Start a stream of events that affect value at `path`.
62-
FirebaseStreamstream(const std::string& path);
63-
virtual std::unique_ptr<FirebaseStream>streamPtr(const std::string& path);
52+
voidstream(const std::string& path);
6453

6554
protected:
6655
// Used for testing.
@@ -72,20 +61,19 @@ class Firebase {
7261
std::string auth_;
7362
};
7463

64+
7565
classFirebaseCall {
7666
public:
77-
FirebaseCall() {}
78-
FirebaseCall(const std::string& host,const std::string& auth,
79-
constchar* method,const std::string& path,
80-
const std::string& data ="",
81-
const std::shared_ptr<FirebaseHttpClient> http =NULL);
67+
FirebaseCall(const std::shared_ptr<FirebaseHttpClient> http =NULL) : http_(http) {}
8268
virtual~FirebaseCall();
8369

84-
virtualconst FirebaseError&error()const {
70+
const FirebaseError&error()const {
8571
return error_;
8672
}
8773

88-
virtualconst std::string&response()const {
74+
voidanalyzeError(char* method,int status,const std::string & path_with_auth);
75+
76+
const std::string&response()const {
8977
return response_;
9078
}
9179

@@ -98,59 +86,22 @@ class FirebaseCall {
9886
std::shared_ptr<StaticJsonBuffer<FIREBASE_JSONBUFFER_SIZE>> buffer_;
9987
};
10088

101-
classFirebaseGet :publicFirebaseCall {
102-
public:
103-
FirebaseGet() {}
104-
FirebaseGet(const std::string& host,const std::string& auth,
105-
const std::string& path,const std::shared_ptr<FirebaseHttpClient> http =NULL);
106-
107-
private:
108-
std::string json_;
89+
classFirebaseRequest :publicFirebaseCall {
90+
public:
91+
FirebaseRequest(const std::shared_ptr<FirebaseHttpClient> http =NULL) : FirebaseCall(http) { }
92+
virtual~FirebaseRequest() {}
93+
intsendRequest(const std::string& host,const std::string& auth,
94+
char* method,const std::string& path,const std::string& data ="");
10995
};
11096

111-
classFirebaseSet:publicFirebaseCall {
112-
public:
113-
FirebaseSet() {}
114-
FirebaseSet(const std::string& host,const std::string& auth,
115-
const std::string& path,const std::string& value,const std::shared_ptr<FirebaseHttpClient> http =NULL);
116-
117-
118-
private:
119-
std::string json_;
120-
};
121-
122-
classFirebasePush :publicFirebaseCall {
123-
public:
124-
FirebasePush() {}
125-
FirebasePush(const std::string& host,const std::string& auth,
126-
const std::string& path,const std::string& value,const std::shared_ptr<FirebaseHttpClient> http =NULL);
127-
virtual~FirebasePush() {}
128-
129-
virtualconst std::string&name()const {
130-
return name_;
131-
}
132-
133-
private:
134-
std::string name_;
135-
};
136-
137-
classFirebaseRemove :publicFirebaseCall {
138-
public:
139-
FirebaseRemove() {}
140-
FirebaseRemove(const std::string& host,const std::string& auth,
141-
const std::string& path,const std::shared_ptr<FirebaseHttpClient> http =NULL);
142-
};
143-
144-
14597
classFirebaseStream :publicFirebaseCall {
14698
public:
147-
FirebaseStream() {}
148-
FirebaseStream(const std::string& host,const std::string& auth,
149-
const std::string& path,const std::shared_ptr<FirebaseHttpClient> http =NULL);
99+
FirebaseStream(const std::shared_ptr<FirebaseHttpClient> http =NULL) : FirebaseCall(http) { }
150100
virtual~FirebaseStream() {}
151101

152102
// Return if there is any event available to read.
153-
virtualboolavailable();
103+
boolavailable();
104+
voidstartStreaming(const std::string& host,const std::string& auth,const std::string& path);
154105

155106
// Event type.
156107
enum Event {
@@ -174,13 +125,6 @@ class FirebaseStream : public FirebaseCall {
174125

175126
// Read next json encoded `event` from stream.
176127
virtual Eventread(std::string& event);
177-
178-
const FirebaseError&error()const {
179-
return _error;
180-
}
181-
182-
private:
183-
FirebaseError _error;
184128
};
185129

186130
#endif// firebase_h

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp