Skip to content

firebase: void return for send request and inline checkResponse #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 20, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 12 additions & 18 deletions Firebase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,16 @@ Firebase& Firebase::auth(const String& auth) {

String Firebase::get(const String& path) {
sendRequest("GET", path);
return getBody();
return readBody();
}

String Firebase::push(const String& path, const String& value) {
sendRequest("POST", path, value);
return getBody();
return readBody();
}

bool Firebase::remove(const String& path) {
int status = sendRequest("DELETE", path);
return status == HTTP_CODE_OK;
void Firebase::remove(const String& path) {
sendRequest("DELETE", path);
}

Firebase& Firebase::stream(const String& path) {
Expand Down Expand Up @@ -81,31 +80,26 @@ String Firebase::makeURL(const String& path) {
return url;
}

int Firebase::sendRequest(const char* method, const String& path, const String& value) {
void Firebase::sendRequest(const char* method, const String& path, const String& value) {
String url = makeURL(path);
_http.begin(_host.c_str(), firebasePort, url.c_str(), true, firebaseFingerprint);
int statusCode = _http.sendRequest(method, (uint8_t*)value.c_str(), value.length());
checkResponse(method, url, statusCode);
return statusCode;
_error.reset();
if (statusCode < 0) {
_error.set(statusCode,
String(method) + " " + url + ": "
+ HTTPClient::errorToString(statusCode));
}
}

String Firebase::getBody() {
String Firebase::readBody() {
if (_error.code() != 0) {
return "";
}
// no _http.end() because of connection reuse.
return _http.getString();
}

void Firebase::checkResponse(const char* method, const String& url, int statusCode) {
_error.reset();
if (statusCode < 0) {
_error.set(statusCode,
String(method) + " " + url + ": "
+ HTTPClient::errorToString(statusCode));
}
}

bool Firebase::connected() {
return _http.connected();
}
Expand Down
7 changes: 3 additions & 4 deletions Firebase.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class Firebase {
}
String get(const String& path);
String push(const String& path, const String& value);
bool remove(const String& path);
void remove(const String& path);
bool connected();
Firebase& stream(const String& path);
bool available();
Expand All @@ -64,9 +64,8 @@ class Firebase {
Event read(String& event);
private:
String makeURL(const String& path);
int sendRequest(const char* method, const String& path, const String& value = "");
String getBody();
void checkResponse(const char* method, const String& url, int status_code);
void sendRequest(const char* method, const String& path, const String& value = "");
String readBody();

HTTPClient _http;
String _host;
Expand Down