<commit_before>//
// 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.
//
#include "Firebase.h"

const char* firebaseFingerprint = "7A 54 06 9B DC 7A 25 B3 86 8D 66 53 48 2C 0B 96 42 C7 B3 0A";
const uint16_t firebasePort = 443;

Firebase::Firebase(const String& host) : _host(host) {
  _http.setReuse(true);
}

Firebase& Firebase::auth(const String& auth) {
  _auth = auth;
  return *this;
}

Firebase& Firebase::child(const String& key) {
  _path = key;
  return *this;
}

String Firebase::val() {
  return sendRequest("GET");
}

String Firebase::push(const String& value) {
  return sendRequest("POST", (uint8_t*)value.c_str(), value.length());
}

String Firebase::sendRequest(const char* method, uint8_t* value, size_t size) {
  _error.reset();
  String url = "/" + _path + ".json";
  if (_auth.length() > 0) {
    url += "?auth=" + _auth;
  }
  _http.begin(_host.c_str(), firebasePort, url.c_str(), true, firebaseFingerprint);
  int statusCode = _http.sendRequest(method, value, size);
  if (statusCode < 0) {
    _error.set(statusCode,
	       String(method) + " " + url + ": "
	       + HTTPClient::errorToString(statusCode));
    return "";
  }
  // no _http.end() because of connection reuse.
  return _http.getString();
}

Firebase& Firebase::stream() {
  _http.begin(_host.c_str(), firebasePort, _path.c_str(), true, firebaseFingerprint);
  _http.addHeader("Accept", "text/event-stream");
  int statusCode = _http.sendRequest("GET", (uint8_t*)NULL, 0);
  return *this;
}

bool Firebase::connected() {
  return _http.connected();
}

bool Firebase::available() {
  return false;
}

String Firebase::read() {
  return "";
}<commit_msg>handle stream redirect<commit_after>//
// 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.
//
#include "Firebase.h"

const char* firebaseFingerprint = "7A 54 06 9B DC 7A 25 B3 86 8D 66 53 48 2C 0B 96 42 C7 B3 0A";
const uint16_t firebasePort = 443;

Firebase::Firebase(const String& host) : _host(host) {
  _http.setReuse(true);
}

Firebase& Firebase::auth(const String& auth) {
  _auth = auth;
  return *this;
}

Firebase& Firebase::child(const String& key) {
  _path = key;
  return *this;
}

String Firebase::val() {
  return sendRequest("GET");
}

String Firebase::push(const String& value) {
  return sendRequest("POST", (uint8_t*)value.c_str(), value.length());
}

Firebase& Firebase::stream() {
  String url = "/" + _path + ".json";
  const char* headers[] = {"Location"};
  _http.setReuse(false);  
  _http.begin(_host.c_str(), firebasePort, url.c_str(), true, firebaseFingerprint);
  _http.collectHeaders(headers, 1);
  _http.addHeader("Accept", "text/event-stream");
  int statusCode = _http.sendRequest("GET", (uint8_t*)NULL, 0);
  String location = _http.header("Location");
  Serial.println(location);
  _http.setReuse(false);    
  _http.begin(location, firebaseFingerprint);
  _http.collectHeaders(headers, 1);
  statusCode = _http.sendRequest("GET", (uint8_t*)NULL, 0);
  location = _http.header("Location");
  Serial.println(location);
  _http.setReuse(false);  
  _http.begin(location, firebaseFingerprint);
  statusCode = _http.sendRequest("GET", (uint8_t*)NULL, 0);
}

String Firebase::sendRequest(const char* method, uint8_t* value, size_t size) {
  _error.reset();
  String url = "/" + _path + ".json";
  if (_auth.length() > 0) {
    url += "?auth=" + _auth;
  }
  _http.begin(_host.c_str(), firebasePort, url.c_str(), true, firebaseFingerprint);
  int statusCode = _http.sendRequest(method, value, size);
  if (statusCode < 0) {
    _error.set(statusCode,
	       String(method) + " " + url + ": "
	       + HTTPClient::errorToString(statusCode));
    return "";
  }
  // no _http.end() because of connection reuse.
  return _http.getString();
}

bool Firebase::connected() {
  return _http.connected();
}

bool Firebase::available() {
  return false;
}

String Firebase::read() {
  return "";
}
{'fraction_non_alphanumeric': 0.1457530914250963, 'fraction_numerical': 0.020271639975674032, 'mean_word_length': 4.0605128205128205, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7166083', 'n_tokens_mistral': 1699, 'n_tokens_neox': 1614, 'n_words': 468}