Advertisement
Marijn78

Untitled

Jun 15th, 2021
1,228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**************************************************************************
  2. GASLib.cpp
  3. ***************************************************************************/
  4.  
  5. #include <GASLib.h>
  6. #include <WiFi.h>
  7. #include <WiFiClientSecure.h>
  8.  
  9. ////////////////////////////////////////
  10. // GASLib Class Methods
  11. ////////////////////////////////////////
  12. GASLib::GASLib(WiFiClient &client, const char *APIKey, const char *host, int port)
  13. {
  14.   _client = &client;
  15.   _apiKey = APIKey;
  16.   _host = host;
  17.   _port = port;
  18. }
  19.  
  20. void GASLib::setAPIKey(const char *APIKey)
  21. {
  22.   _apiKey = APIKey;
  23. }
  24.  
  25. void GASLib::setHubIP(const char *host)
  26. {
  27.   _host = host;
  28. }
  29.  
  30. void GASLib::setHubPort(uint8_t port)
  31. {
  32.   _port = port;
  33. }
  34.  
  35. String GASLib::gasClient(String content, bool redirect)
  36. {
  37.   String movedURL;
  38.   String line;
  39.   if (!_client->connect(_host, _port))
  40.   {
  41.     return "error";
  42.   }
  43.  
  44.   _client->print(String("POST ") + "/macros/s/" + _apiKey + "/exec" + " HTTP/1.1\r\n" +
  45.                  "Host: " + _host + "\r\n" +
  46.                  "Content-Type: application/json" + "\r\n" +
  47.                  "Content-Length: " + String(content.length()) + "\r\n\r\n" +
  48.                  content + "\r\n" +
  49.                  "Connection: close\r\n\r\n");
  50.  
  51.   while (_client->connected())
  52.   {
  53.     line = _client->readStringUntil('\n');
  54.     if (line == "\r")
  55.       break;
  56.     if (line.indexOf("Location") >= 0)
  57.     {
  58.       movedURL = line.substring(line.indexOf(":") + 2);
  59.     }
  60.   }
  61.   _client->stop();
  62.   int retCode = 0;
  63.   if (redirect)
  64.   {
  65.     movedURL.trim();
  66.     if (movedURL.length() < 10)
  67.       return "error";
  68.     if (!_client->connect(_host, _port))
  69.     {
  70.       return "error";
  71.     }
  72.  
  73.     _client->println("GET " + movedURL);
  74.     _client->println("Host: " + String(_host));
  75.     _client->println("Connection: close");
  76.     _client->println();
  77.  
  78.     while (_client->connected())
  79.     {
  80.       line = _client->readStringUntil('\n');
  81.       if (line.startsWith("HTTP/1.0"))
  82.       {
  83.         retCode = line.substring(9, 12).toInt();
  84.       }
  85.       if (line == "\r")
  86.         break;
  87.     }
  88.     if (_client->available())
  89.     {
  90.       line = _client->readStringUntil('\r');
  91.     }
  92.     _client->stop();
  93.   }
  94.   else
  95.   {
  96.     retCode = 200;
  97.   }
  98.   if (retCode == 200)
  99.   {
  100.     return line;
  101.   }
  102.   else
  103.   {
  104.     return "error";
  105.   }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement