|
| 1 | +// |
| 2 | +// Copyright 2016 Google Inc. |
| 3 | +// |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// |
| 8 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | +// |
| 16 | + |
| 17 | +#include "FirebaseArduino.h" |
| 18 | + |
| 19 | +void FirebaseArduino::begin(const char* host, const char* auth) { |
| 20 | + http_.reset(FirebaseHttpClient::create()); |
| 21 | + http_->setReuseConnection(true); |
| 22 | + host_ = host; |
| 23 | + auth_ = auth; |
| 24 | +} |
| 25 | + |
| 26 | +String FirebaseArduino::FirebaseArduino::push(const String& path, const JsonVariant& value) { |
| 27 | + String buf; |
| 28 | + value.printTo(buf); |
| 29 | + auto push = FirebasePush(host_, auth_, path, buf, http_.get()); |
| 30 | + error_ = push.error(); |
| 31 | + return push.name(); |
| 32 | +} |
| 33 | + |
| 34 | +void FirebaseArduino::set(const String& path, const JsonVariant& value) { |
| 35 | + String buf; |
| 36 | + value.printTo(buf); |
| 37 | + auto set = FirebaseSet(host_, auth_, path, buf, http_.get()); |
| 38 | + error_ = set.error(); |
| 39 | +} |
| 40 | + |
| 41 | +FirebaseObject FirebaseArduino::get(const char* path) { |
| 42 | + auto get = FirebaseGet(host_, auth_, path, http_.get()); |
| 43 | + error_ = get.error(); |
| 44 | + if (!error_) { |
| 45 | + return FirebaseObject{""}; |
| 46 | + } |
| 47 | + return FirebaseObject(get.response()); |
| 48 | +} |
| 49 | + |
| 50 | +void FirebaseArduino::remove(const char* path) { |
| 51 | + auto remove = FirebaseRemove(host_, auth_, path, http_.get()); |
| 52 | + error_ = remove.error(); |
| 53 | +} |
| 54 | + |
| 55 | +void FirebaseArduino::stream(const char* path) { |
| 56 | + auto stream = FirebaseStream(host_, auth_, path, http_.get()); |
| 57 | + error_ = stream.error(); |
| 58 | +} |
| 59 | + |
| 60 | +bool FirebaseArduino::available() { |
| 61 | + return http_->getStreamPtr()->available(); |
| 62 | +} |
| 63 | + |
| 64 | +FirebaseObject FirebaseArduino::readEvent() { |
| 65 | + auto client = http_->getStreamPtr(); |
| 66 | + String type = client->readStringUntil('\n').substring(7);; |
| 67 | + String event = client->readStringUntil('\n').substring(6); |
| 68 | + client->readStringUntil('\n'); // consume separator |
| 69 | + FirebaseObject obj = FirebaseObject(event); |
| 70 | + obj["type"] = type; |
| 71 | + return obj; |
| 72 | +} |
| 73 | + |
| 74 | +bool FirebaseArduino::success() { |
| 75 | + return error_.code() == 0; |
| 76 | +} |
| 77 | + |
| 78 | +bool FirebaseArduino::failed() { |
| 79 | + return error_.code() != 0; |
| 80 | +} |
| 81 | + |
| 82 | +const String& FirebaseArduino::error() { |
| 83 | + return error_.message(); |
| 84 | +} |
| 85 | + |
| 86 | +FirebaseArduino Firebase; |
0 commit comments