forked from FirebaseExtended/firebase-arduino
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathFirebase.h
121 lines (93 loc) · 2.93 KB
/
Firebase.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
//
// Copyright 2015 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// firebase-arduino is an Arduino client for Firebase.
// It is currently limited to the ESP8266 board family.
#ifndef firebase_h
#define firebase_h
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <ESP8266HTTPClient.h>
//TODO(edcoyne) split these into multiple files.
class FirebaseCall;
class FirebaseEventStream;
// Primary client to the Firebase backend.
class Firebase {
public:
Firebase(const String& host);
Firebase& auth(const String& auth);
// Fetch result at "path" to a local variable. If the value is too large you will exceed
// local memory.
FirebaseCall get(const String& path);
// Add new value to list at "path", will return child name of new item.
FirebaseCall push(const String& path, const String& value);
// Deletes value at "path" from server.
FirebaseCall remove(const String& path);
// Starts a stream of events that effect object at "path".
FirebaseEventStream stream(const String& path);
private:
HTTPClient http_;
String host_;
String auth_;
};
class FirebaseCall {
public:
FirebaseCall(const String& host, const String& auth,
const char* method, const String& path, const String& value,
HTTPClient* http);
FirebaseCall(const String& host, const String& auth,
const char* method, const String& path,
HTTPClient* http);
// True if there was an error completing call.
bool isError() const;
String errorMessage() const;
// True if http status code is 200(OK).
bool isOk() const;
// Message sent back from Firebase backend. This pulls value to local memory,
// be careful if value can be large.
String rawResponse();
int httpStatus() const {
return status_;
}
private:
FirebaseCall(HTTPClient* http);
HTTPClient* http_;
int status_;
String error_message_;
};
class FirebaseEventStream {
public:
enum Event {
UNKNOWN,
PUT,
PATCH
};
FirebaseEventStream(const String& host, const String& auth, const String& path);
// Read next event in stream.
Event read(String& event);
// True if connected to backend.
bool connected();
// True if there is an event available.
bool available();
// True if there was an error.
bool isError() const;
String errorMessage() const;
private:
HTTPClient http_;
int status_;
String error_message_;
};
#endif // firebase_h