@@ -42,115 +42,16 @@ const std::string& Firebase::auth() const {
42
42
return auth_;
43
43
}
44
44
45
- FirebaseGet Firebase::get (const std::string& path) {
46
- return FirebaseGet (host_, auth_, path, http_);
47
- }
48
-
49
- unique_ptr<FirebaseGet> Firebase::getPtr (const std::string& path) {
50
- return unique_ptr<FirebaseGet>(new FirebaseGet (host_, auth_, path, http_));
51
- }
52
-
53
- FirebaseSet Firebase::set (const std::string& path, const std::string& value) {
54
- return FirebaseSet (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
- new FirebaseSet (host_, auth_, path, value, http_));
61
- }
62
-
63
- FirebasePush Firebase::push (const std::string& path, const std::string& value) {
64
- return FirebasePush (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
- new FirebasePush (host_, auth_, path, value, http_));
70
- }
71
-
72
- FirebaseRemove Firebase::remove (const std::string& path) {
73
- return FirebaseRemove (host_, auth_, path, http_);
74
- }
75
-
76
- unique_ptr<FirebaseRemove> Firebase::removePtr (const std::string& path) {
77
- return unique_ptr<FirebaseRemove>(
78
- new FirebaseRemove (host_, auth_, path, http_));
79
- }
80
-
81
- FirebaseStream Firebase::stream (const std::string& path) {
82
- // TODO: create new client dedicated to stream.
83
- return FirebaseStream (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
- new FirebaseStream (host_, auth_, path, http_));
90
- }
91
-
92
- // FirebaseCall
93
- FirebaseCall::FirebaseCall (const std::string& host, const std::string& auth,
94
- const char * 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
- const char * 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
+ void FirebaseCall::analyzeError (char * method, int status, const std::string& path_with_auth) {
46
+ if (status != 200 ) {
136
47
error_ = FirebaseError (status,
137
48
std::string (method) + " " + path_with_auth +
138
49
" : " + http_->errorToString (status));
139
50
}
140
-
141
- // if not streaming.
142
- if (!followRedirect) {
143
- response_ = http_->getString ();
144
- http_->setStreaming (" " );
145
- } else {
146
- http_->setStreaming (path);
147
- }
148
51
}
149
52
150
53
FirebaseCall::~FirebaseCall () {
151
- if (http_ && !http_->isStreaming ()) {
152
- http_->end ();
153
- }
54
+ http_->end ();
154
55
}
155
56
156
57
const JsonObject& FirebaseCall::json () {
@@ -162,46 +63,39 @@ const JsonObject& FirebaseCall::json() {
162
63
return buffer_.get ()->parseObject (response ().c_str ());
163
64
}
164
65
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
+ int FirebaseRequest::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 ();
170
76
}
171
77
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
+ void FirebaseStream::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);
182
83
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 <const char *>();
190
- }
191
- }
84
+ http_->addHeader (" Accept" , " text/event-stream" );
85
+ const char * headers[] = {" Location" };
86
+ http_->collectHeaders (headers, 1 );
192
87
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);
199
90
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
+ }
205
99
}
206
100
207
101
bool FirebaseStream::available () {
0 commit comments