|
15 | 15 | //
|
16 | 16 | #include"Firebase.h"
|
17 | 17 |
|
18 |
| -constchar* firebaseFingerprint ="7A 54 06 9B DC 7A 25 B3 86 8D 66 53 48 2C 0B 96 42 C7 B3 0A"; |
19 |
| -constuint16_t firebasePort =443; |
| 18 | +namespace { |
| 19 | +constchar*kFirebaseFingerprint ="7A 54 06 9B DC 7A 25 B3 86 8D 66 53 48 2C 0B 96 42 C7 B3 0A"; |
| 20 | +constuint16_tkFirebasePort =443; |
20 | 21 |
|
21 |
| -Firebase::Firebase(const String& host) : _host(host) { |
22 |
| - _http.setReuse(true); |
| 22 | +StringmakeFirebaseURL(const String& path,const String& auth) { |
| 23 | + String url; |
| 24 | +if (path[0] !='/') { |
| 25 | + url ="/"; |
| 26 | + } |
| 27 | + url += path +".json"; |
| 28 | +if (auth.length() >0) { |
| 29 | + url +="?auth=" + auth; |
| 30 | + } |
| 31 | +return url; |
| 32 | +} |
| 33 | + |
| 34 | +}// namespace |
| 35 | + |
| 36 | +Firebase::Firebase(const String& host) : host_(host) { |
| 37 | + http_.setReuse(true); |
23 | 38 | }
|
24 | 39 |
|
25 | 40 | Firebase&Firebase::auth(const String& auth) {
|
26 |
| -_auth = auth; |
| 41 | +auth_ = auth; |
27 | 42 | return *this;
|
28 | 43 | }
|
29 | 44 |
|
30 |
| -StringFirebase::get(const String& path) { |
31 |
| -sendRequest("GET", path); |
32 |
| -returnreadBody(); |
| 45 | +FirebaseGetFirebase::get(const String& path) { |
| 46 | +returnFirebaseGet(host_, auth_, path, &http_); |
33 | 47 | }
|
34 | 48 |
|
35 |
| -StringFirebase::push(const String& path,const String& value) { |
36 |
| -sendRequest("POST", path, value); |
37 |
| -returnreadBody(); |
| 49 | +FirebasePushFirebase::push(const String& path,const String& value) { |
| 50 | +returnFirebasePush(host_, auth_, path, value, &http_); |
38 | 51 | }
|
39 | 52 |
|
40 |
| -voidFirebase::remove(const String& path) { |
41 |
| -sendRequest("DELETE",path); |
| 53 | +FirebaseRemoveFirebase::remove(const String& path) { |
| 54 | +returnFirebaseRemove(host_, auth_,path, &http_); |
42 | 55 | }
|
43 | 56 |
|
44 |
| -StringFirebase::set(const String& path,const String& value) { |
45 |
| -sendRequest("PUT", path, value); |
46 |
| -returnreadBody(); |
| 57 | +FirebaseStreamFirebase::stream(const String& path) { |
| 58 | +// TODO: create new client dedicated to stream. |
| 59 | +returnFirebaseStream(host_, auth_, path, &http_); |
47 | 60 | }
|
48 | 61 |
|
49 |
| -Firebase&Firebase::stream(const String& path) { |
50 |
| - _error.reset(); |
51 |
| - String url =makeURL(path); |
52 |
| -constchar* headers[] = {"Location"}; |
53 |
| - _http.setReuse(true); |
54 |
| - _http.begin(_host.c_str(), firebasePort, url.c_str(),true, firebaseFingerprint); |
55 |
| - _http.collectHeaders(headers,1); |
56 |
| - _http.addHeader("Accept","text/event-stream"); |
57 |
| -int statusCode = _http.sendRequest("GET", (uint8_t*)NULL,0); |
58 |
| - String location; |
59 |
| -// TODO(proppy): Add a max redirect check |
60 |
| -while (statusCode ==307) { |
61 |
| - location = _http.header("Location"); |
62 |
| - _http.setReuse(false); |
63 |
| - _http.end(); |
64 |
| - _http.setReuse(true); |
65 |
| - _http.begin(location, firebaseFingerprint); |
66 |
| - statusCode = _http.sendRequest("GET", (uint8_t*)NULL,0); |
| 62 | +// FirebaseCall |
| 63 | +FirebaseCall::FirebaseCall(const String& host,const String& auth, |
| 64 | +constchar* method,const String& path, |
| 65 | +const String& data, HTTPClient* http) : http_(http) { |
| 66 | + String url =makeFirebaseURL(path, auth); |
| 67 | + http_->setReuse(true); |
| 68 | + http_->begin(host,kFirebasePort, url,true,kFirebaseFingerprint); |
| 69 | + |
| 70 | +bool followRedirect =false; |
| 71 | +if (method =="STREAM") { |
| 72 | + method ="GET"; |
| 73 | + http_->addHeader("Accept","text/event-stream"); |
| 74 | + followRedirect =true; |
67 | 75 | }
|
68 |
| -if (statusCode !=200) { |
69 |
| - _error.set(statusCode, |
70 |
| -"stream" + location +":" |
71 |
| - +HTTPClient::errorToString(statusCode)); |
| 76 | + |
| 77 | +if (followRedirect) { |
| 78 | +constchar* headers[] = {"Location"}; |
| 79 | +http_->collectHeaders(headers,1); |
72 | 80 | }
|
73 |
| -return *this; |
74 |
| -} |
| 81 | + |
| 82 | +int status = http_->sendRequest(method, (uint8_t*)data.c_str(), data.length()); |
75 | 83 |
|
76 |
| -StringFirebase::makeURL(const String& path) { |
77 |
| - String url; |
78 |
| -if (path[0] !='/') { |
79 |
| - url ="/"; |
| 84 | +// TODO: Add a max redirect check |
| 85 | +if (followRedirect) { |
| 86 | +while (status == HTTP_CODE_TEMPORARY_REDIRECT) { |
| 87 | + String location = http_->header("Location"); |
| 88 | + http_->setReuse(false); |
| 89 | + http_->end(); |
| 90 | + http_->setReuse(true); |
| 91 | + http_->begin(location,kFirebaseFingerprint); |
| 92 | + status = http_->sendRequest("GET", (uint8_t*)NULL,0); |
| 93 | + } |
80 | 94 | }
|
81 |
| - url += path +".json"; |
82 |
| -if (_auth.length() >0) { |
83 |
| - url +="?auth=" + _auth; |
| 95 | + |
| 96 | +if (status !=200) { |
| 97 | + error_ =FirebaseError(status,String(method) +"" + url +":" +HTTPClient::errorToString(status)); |
| 98 | + } |
| 99 | + |
| 100 | +// if not streaming. |
| 101 | +if (!followRedirect) { |
| 102 | + response_ = http_->getString(); |
84 | 103 | }
|
85 |
| -return url; |
86 | 104 | }
|
87 | 105 |
|
88 |
| -voidFirebase::sendRequest(constchar* method,const String& path,const String& value) { |
89 |
| -String url =makeURL(path); |
90 |
| - _http.begin(_host.c_str(), firebasePort, url.c_str(),true, firebaseFingerprint); |
91 |
| -int statusCode = _http.sendRequest(method, (uint8_t*)value.c_str(), value.length()); |
92 |
| -_error.reset(); |
93 |
| -if (statusCode <0) { |
94 |
| - _error.set(statusCode, |
95 |
| -String(method) +"" + url +":" |
96 |
| - +HTTPClient::errorToString(statusCode)); |
97 |
| - } |
| 106 | +// FirebaseGet |
| 107 | +FirebaseGet::FirebaseGet(constString& host,const String& auth, |
| 108 | +const String& path, |
| 109 | + HTTPClient* http) |
| 110 | +: FirebaseCall(host, auth,"GET", path,"", http) { |
| 111 | + |
| 112 | +if (!error()) { |
| 113 | +// TODO: parse json |
| 114 | +json_ =response(); |
| 115 | + } |
98 | 116 | }
|
99 | 117 |
|
100 |
| -StringFirebase::readBody() { |
101 |
| -if (_error.code() !=0) { |
102 |
| -return""; |
| 118 | +// FirebasePush |
| 119 | +FirebasePush::FirebasePush(const String& host,const String& auth, |
| 120 | +const String& path,const String& value, |
| 121 | + HTTPClient* http) |
| 122 | + : FirebaseCall(host, auth,"POST", path, value, http) { |
| 123 | +if (!error()) { |
| 124 | +// TODO: parse name |
| 125 | + name_ =response(); |
103 | 126 | }
|
104 |
| -// no _http.end() because of connection reuse. |
105 |
| -return _http.getString(); |
106 | 127 | }
|
107 | 128 |
|
108 |
| -boolFirebase::connected() { |
109 |
| -return _http.connected(); |
| 129 | +// FirebasePush |
| 130 | +FirebaseRemove::FirebaseRemove(const String& host,const String& auth, |
| 131 | +const String& path, |
| 132 | + HTTPClient* http) |
| 133 | + : FirebaseCall(host, auth,"DELETE", path,"", http) { |
| 134 | +} |
| 135 | + |
| 136 | +// FirebaseStream |
| 137 | +FirebaseStream::FirebaseStream(const String& host,const String& auth, |
| 138 | +const String& path, |
| 139 | + HTTPClient* http) |
| 140 | + : FirebaseCall(host, auth,"STREAM", path,"", http) { |
110 | 141 | }
|
111 | 142 |
|
112 |
| -boolFirebase::available() { |
113 |
| -return_http.getStreamPtr()->available(); |
| 143 | +boolFirebaseStream::available() { |
| 144 | +returnhttp_->getStreamPtr()->available(); |
114 | 145 | }
|
115 | 146 |
|
116 |
| -Firebase::EventFirebase::read(String& event) { |
117 |
| -auto client =_http.getStreamPtr(); |
118 |
| - Event type;; |
| 147 | +FirebaseStream::EventFirebaseStream::read(String& event) { |
| 148 | +auto client =http_->getStreamPtr(); |
| 149 | + Event type; |
119 | 150 | String typeStr = client->readStringUntil('\n').substring(7);
|
120 | 151 | if (typeStr =="put") {
|
121 |
| - type =Firebase::Event::PUT; |
| 152 | + type = Event::PUT; |
122 | 153 | }elseif (typeStr =="patch") {
|
123 |
| - type =Firebase::Event::PATCH; |
| 154 | + type = Event::PATCH; |
124 | 155 | }else {
|
125 |
| - type =Firebase::Event::UNKNOWN; |
| 156 | + type = Event::UNKNOWN; |
126 | 157 | }
|
127 | 158 | event = client->readStringUntil('\n').substring(6);
|
128 | 159 | client->readStringUntil('\n');// consume separator
|
|