Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**************************************************************************
- GASLib.cpp
- ***************************************************************************/
- #include <GASLib.h>
- #include <WiFi.h>
- #include <WiFiClientSecure.h>
- ////////////////////////////////////////
- // GASLib Class Methods
- ////////////////////////////////////////
- GASLib::GASLib(WiFiClient &client, const char *APIKey, const char *host, int port)
- {
- _client = &client;
- _apiKey = APIKey;
- _host = host;
- _port = port;
- }
- void GASLib::setAPIKey(const char *APIKey)
- {
- _apiKey = APIKey;
- }
- void GASLib::setHubIP(const char *host)
- {
- _host = host;
- }
- void GASLib::setHubPort(uint8_t port)
- {
- _port = port;
- }
- String GASLib::gasClient(String content, bool redirect)
- {
- String movedURL;
- String line;
- if (!_client->connect(_host, _port))
- {
- return "error";
- }
- _client->print(String("POST ") + "/macros/s/" + _apiKey + "/exec" + " HTTP/1.1\r\n" +
- "Host: " + _host + "\r\n" +
- "Content-Type: application/json" + "\r\n" +
- "Content-Length: " + String(content.length()) + "\r\n\r\n" +
- content + "\r\n" +
- "Connection: close\r\n\r\n");
- while (_client->connected())
- {
- line = _client->readStringUntil('\n');
- if (line == "\r")
- break;
- if (line.indexOf("Location") >= 0)
- {
- movedURL = line.substring(line.indexOf(":") + 2);
- }
- }
- _client->stop();
- int retCode = 0;
- if (redirect)
- {
- movedURL.trim();
- if (movedURL.length() < 10)
- return "error";
- if (!_client->connect(_host, _port))
- {
- return "error";
- }
- _client->println("GET " + movedURL);
- _client->println("Host: " + String(_host));
- _client->println("Connection: close");
- _client->println();
- while (_client->connected())
- {
- line = _client->readStringUntil('\n');
- if (line.startsWith("HTTP/1.0"))
- {
- retCode = line.substring(9, 12).toInt();
- }
- if (line == "\r")
- break;
- }
- if (_client->available())
- {
- line = _client->readStringUntil('\r');
- }
- _client->stop();
- }
- else
- {
- retCode = 200;
- }
- if (retCode == 200)
- {
- return line;
- }
- else
- {
- return "error";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement