void JSONAPI::_headerStart (const char *method) {
  // If an old connection is active, close.
  if (wifly.isConnected()) {
    wifly.close();
  }

  if (!wifly.open(this->host, 80)) {
    Serial.println(F("Failed to connect to host."));
  }
  
  wifly.print(method);
  wifly.print(" ");
  // wifly.print("http://");
  // wifly.print(this->host);
}
void JSONAPI::_headerEnd () {
  wifly.println(" HTTP/1.1"); // paste your number here
  wifly.print("Host: ");
  wifly.println(this->host);
  wifly.println("User-Agent: lifegraph/0.0.1");
  if (this->hasBody) {
    wifly.println("Content-Type: application/x-www-form-urlencoded");
    wifly.println("Transfer-Encoding: chunked");
  }
  wifly.println();
}
int LifegraphAPI::connect (uint8_t uid[], int uidLength) {
  this->hasBody = false;
  this->_headerStart("GET");
  this->_headerPath("tokens");
  wifly.print("/");
  char token[3];
  for (int i = 0; i < uidLength; i++) {
    snprintf(token, 3, "%02x", uid[i]);
    wifly.print(token);
  }
  wifly.print("?namespace=");
  wifly.print(this->ns);
  wifly.print("&key=");
  wifly.print(this->key);
  wifly.print("&secret=");
  wifly.print(this->secret);
  this->_headerEnd();
  return this->request( NULL );
}
void FacebookAPI::_headerPath (const char *path, const char *access_token) {
  wifly.print(this->base);
  wifly.print("/");
  wifly.print(path);
  if (access_token != 0) {
    wifly.print(strstr(path, "?") == 0 ? "?" : "&");
    wifly.print("access_token=");
    wifly.print(access_token);
  }
  wifly.print(strstr(path, "?") == 0 ? "?" : "&");
  wifly.print("namespace=");
  wifly.print(Lifegraph.ns);
  wifly.print("&key=");
  wifly.print(Lifegraph.key);
  wifly.print("&secret=");
  wifly.print(Lifegraph.secret);
}
void JSONAPI::_headerPath (const char *path) {
  wifly.print(this->base);
  wifly.print("/");
  wifly.print(path);
}