From bd230650278ec34ef519369323549a6ce0f2abc4 Mon Sep 17 00:00:00 2001 From: David Madison Date: Thu, 1 Oct 2020 22:29:28 -0400 Subject: [PATCH 01/11] Use constant pointers for char arguments These should be immutable. Fixes deprecated conversion warnings from string constant to 'char*' --- src/YoutubeApi.cpp | 6 +++--- src/YoutubeApi.h | 8 ++++---- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/YoutubeApi.cpp b/src/YoutubeApi.cpp index acbe6df..c1df75f 100644 --- a/src/YoutubeApi.cpp +++ b/src/YoutubeApi.cpp @@ -29,12 +29,12 @@ YoutubeApi::YoutubeApi(String apiKey, Client &client) { YoutubeApi(tempStr, client); } -YoutubeApi::YoutubeApi(char *apiKey, Client &client) { +YoutubeApi::YoutubeApi(const char *apiKey, Client &client) { _apiKey = apiKey; this->client = &client; } -int YoutubeApi::sendGetToYoutube(char *command) { +int YoutubeApi::sendGetToYoutube(const char *command) { client->flush(); client->setTimeout(YTAPI_TIMEOUT); if (!client->connect(YTAPI_HOST, YTAPI_SSL_PORT)) @@ -78,7 +78,7 @@ bool YoutubeApi::getChannelStatistics(String channelId){ return getChannelStatistics(tempStr); } -bool YoutubeApi::getChannelStatistics(char *channelId){ +bool YoutubeApi::getChannelStatistics(const char *channelId){ char command[150] = YTAPI_CHANNEL_ENDPOINT; char params[120]; sprintf(params, "?part=statistics&id=%s&key=%s", channelId, _apiKey); diff --git a/src/YoutubeApi.h b/src/YoutubeApi.h index 99e4b4e..854df0d 100644 --- a/src/YoutubeApi.h +++ b/src/YoutubeApi.h @@ -42,16 +42,16 @@ struct channelStatistics{ class YoutubeApi { public: - YoutubeApi (char *apiKey, Client &client); + YoutubeApi (const char *apiKey, Client &client); YoutubeApi (String apiKey, Client &client); - int sendGetToYoutube(char *command); - bool getChannelStatistics(char *channelId); + int sendGetToYoutube(const char *command); + bool getChannelStatistics(const char *channelId); bool getChannelStatistics(String channelId); channelStatistics channelStats; bool _debug = false; private: - char *_apiKey; + const char *_apiKey; Client *client; int getHttpStatusCode(); void skipHeaders(); From 2693ca3a1d36d6a0953021a3d4fbf350fbf7f701 Mon Sep 17 00:00:00 2001 From: David Madison Date: Fri, 2 Oct 2020 02:11:10 -0400 Subject: [PATCH 02/11] Remove String version of constructor This doesn't work and causes the ESP to crash, as the character array is cleared once the function exits. --- src/YoutubeApi.cpp | 8 -------- src/YoutubeApi.h | 1 - 2 files changed, 9 deletions(-) diff --git a/src/YoutubeApi.cpp b/src/YoutubeApi.cpp index c1df75f..014b4d0 100644 --- a/src/YoutubeApi.cpp +++ b/src/YoutubeApi.cpp @@ -21,14 +21,6 @@ #include "YoutubeApi.h" -YoutubeApi::YoutubeApi(String apiKey, Client &client) { - int strLen = apiKey.length() + 1; - char tempStr[strLen]; - apiKey.toCharArray(tempStr, strLen); - - YoutubeApi(tempStr, client); -} - YoutubeApi::YoutubeApi(const char *apiKey, Client &client) { _apiKey = apiKey; this->client = &client; diff --git a/src/YoutubeApi.h b/src/YoutubeApi.h index 854df0d..15a34bb 100644 --- a/src/YoutubeApi.h +++ b/src/YoutubeApi.h @@ -43,7 +43,6 @@ class YoutubeApi { public: YoutubeApi (const char *apiKey, Client &client); - YoutubeApi (String apiKey, Client &client); int sendGetToYoutube(const char *command); bool getChannelStatistics(const char *channelId); bool getChannelStatistics(String channelId); From 5b8f294017ddf9fa5cf7c2a10242f8ce93e4c227 Mon Sep 17 00:00:00 2001 From: David Madison Date: Fri, 2 Oct 2020 02:22:34 -0400 Subject: [PATCH 03/11] Simplify String version of getChannelStats This is immutable, so the built-in C-string function works fine. --- src/YoutubeApi.cpp | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/src/YoutubeApi.cpp b/src/YoutubeApi.cpp index 014b4d0..39f4712 100644 --- a/src/YoutubeApi.cpp +++ b/src/YoutubeApi.cpp @@ -61,15 +61,6 @@ int YoutubeApi::sendGetToYoutube(const char *command) { return statusCode; } -bool YoutubeApi::getChannelStatistics(String channelId){ - - int strLen = channelId.length() + 1; - char tempStr[strLen]; - channelId.toCharArray(tempStr, strLen); - - return getChannelStatistics(tempStr); -} - bool YoutubeApi::getChannelStatistics(const char *channelId){ char command[150] = YTAPI_CHANNEL_ENDPOINT; char params[120]; @@ -125,6 +116,10 @@ bool YoutubeApi::getChannelStatistics(const char *channelId){ return wasSuccessful; } +bool YoutubeApi::getChannelStatistics(String channelId) { + return getChannelStatistics(channelId.c_str()); +} + void YoutubeApi::skipHeaders() { // Skip HTTP headers From 555881c5784a96bc5115df3240ae7e54ccc74ebe Mon Sep 17 00:00:00 2001 From: David Madison Date: Fri, 2 Oct 2020 02:24:53 -0400 Subject: [PATCH 04/11] Refactor apiKey to remove underscore Simpler, matches the style of other private members, and avoids confusion in the constructor --- src/YoutubeApi.cpp | 6 +++--- src/YoutubeApi.h | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/YoutubeApi.cpp b/src/YoutubeApi.cpp index 39f4712..69cd85b 100644 --- a/src/YoutubeApi.cpp +++ b/src/YoutubeApi.cpp @@ -21,8 +21,8 @@ #include "YoutubeApi.h" -YoutubeApi::YoutubeApi(const char *apiKey, Client &client) { - _apiKey = apiKey; +YoutubeApi::YoutubeApi(const char * key, Client &client) { + apiKey = key; this->client = &client; } @@ -64,7 +64,7 @@ int YoutubeApi::sendGetToYoutube(const char *command) { bool YoutubeApi::getChannelStatistics(const char *channelId){ char command[150] = YTAPI_CHANNEL_ENDPOINT; char params[120]; - sprintf(params, "?part=statistics&id=%s&key=%s", channelId, _apiKey); + sprintf(params, "?part=statistics&id=%s&key=%s", channelId, apiKey); strcat(command, params); if (_debug) diff --git a/src/YoutubeApi.h b/src/YoutubeApi.h index 15a34bb..8ed85cf 100644 --- a/src/YoutubeApi.h +++ b/src/YoutubeApi.h @@ -42,7 +42,7 @@ struct channelStatistics{ class YoutubeApi { public: - YoutubeApi (const char *apiKey, Client &client); + YoutubeApi (const char *key, Client &client); int sendGetToYoutube(const char *command); bool getChannelStatistics(const char *channelId); bool getChannelStatistics(String channelId); @@ -50,7 +50,7 @@ class YoutubeApi bool _debug = false; private: - const char *_apiKey; + const char *apiKey; Client *client; int getHttpStatusCode(); void skipHeaders(); From 18ba163ecfb9b66c913adda5b173b24b73e37c50 Mon Sep 17 00:00:00 2001 From: David Madison Date: Fri, 2 Oct 2020 02:45:12 -0400 Subject: [PATCH 05/11] Use an initializer list for the constructor --- src/YoutubeApi.cpp | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/YoutubeApi.cpp b/src/YoutubeApi.cpp index 69cd85b..fc1a9c6 100644 --- a/src/YoutubeApi.cpp +++ b/src/YoutubeApi.cpp @@ -21,10 +21,9 @@ #include "YoutubeApi.h" -YoutubeApi::YoutubeApi(const char * key, Client &client) { - apiKey = key; - this->client = &client; -} +YoutubeApi::YoutubeApi(const char* key, Client &client) + : apiKey(key), client(&client) +{} int YoutubeApi::sendGetToYoutube(const char *command) { client->flush(); From bad426ae6a0c7f264e83005b6833866ee039c4af Mon Sep 17 00:00:00 2001 From: David Madison Date: Fri, 2 Oct 2020 02:54:20 -0400 Subject: [PATCH 06/11] Store client as reference This is always required and shouldn't ever need to be replaced, so this is better suited as a reference. --- src/YoutubeApi.cpp | 38 +++++++++++++++++++------------------- src/YoutubeApi.h | 2 +- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/src/YoutubeApi.cpp b/src/YoutubeApi.cpp index fc1a9c6..571e75e 100644 --- a/src/YoutubeApi.cpp +++ b/src/YoutubeApi.cpp @@ -22,13 +22,13 @@ #include "YoutubeApi.h" YoutubeApi::YoutubeApi(const char* key, Client &client) - : apiKey(key), client(&client) + : apiKey(key), client(client) {} int YoutubeApi::sendGetToYoutube(const char *command) { - client->flush(); - client->setTimeout(YTAPI_TIMEOUT); - if (!client->connect(YTAPI_HOST, YTAPI_SSL_PORT)) + client.flush(); + client.setTimeout(YTAPI_TIMEOUT); + if (!client.connect(YTAPI_HOST, YTAPI_SSL_PORT)) { Serial.println(F("Connection failed")); return false; @@ -37,17 +37,17 @@ int YoutubeApi::sendGetToYoutube(const char *command) { yield(); // Send HTTP request - client->print(F("GET ")); - client->print(command); - client->println(F(" HTTP/1.1")); + client.print(F("GET ")); + client.print(command); + client.println(F(" HTTP/1.1")); //Headers - client->print(F("Host: ")); - client->println(YTAPI_HOST); + client.print(F("Host: ")); + client.println(YTAPI_HOST); - client->println(F("Cache-Control: no-cache")); + client.println(F("Cache-Control: no-cache")); - if (client->println() == 0) + if (client.println() == 0) { Serial.println(F("Failed to send request")); return -1; @@ -88,7 +88,7 @@ bool YoutubeApi::getChannelStatistics(const char *channelId){ DynamicJsonDocument doc(bufferSize); // Parse JSON object - DeserializationError error = deserializeJson(doc, *client); + DeserializationError error = deserializeJson(doc, client); if (!error) { wasSuccessful = true; @@ -123,7 +123,7 @@ void YoutubeApi::skipHeaders() { // Skip HTTP headers char endOfHeaders[] = "\r\n\r\n"; - if (!client->find(endOfHeaders)) + if (!client.find(endOfHeaders)) { Serial.println(F("Invalid response")); return; @@ -131,10 +131,10 @@ void YoutubeApi::skipHeaders() // Was getting stray characters between the headers and the body // This should toss them away - while (client->available() && client->peek() != '{') + while (client.available() && client.peek() != '{') { char c = 0; - client->readBytes(&c, 1); + client.readBytes(&c, 1); if (_debug) { Serial.print("Tossing an unexpected character: "); @@ -146,8 +146,8 @@ void YoutubeApi::skipHeaders() int YoutubeApi::getHttpStatusCode() { // Check HTTP status - if(client->find("HTTP/1.1")){ - int statusCode = client->parseInt(); + if(client.find("HTTP/1.1")){ + int statusCode = client.parseInt(); return statusCode; } @@ -155,8 +155,8 @@ int YoutubeApi::getHttpStatusCode() } void YoutubeApi::closeClient() { - if(client->connected()) { + if(client.connected()) { if(_debug) { Serial.println(F("Closing client")); } - client->stop(); + client.stop(); } } diff --git a/src/YoutubeApi.h b/src/YoutubeApi.h index 8ed85cf..60c2b49 100644 --- a/src/YoutubeApi.h +++ b/src/YoutubeApi.h @@ -51,7 +51,7 @@ class YoutubeApi private: const char *apiKey; - Client *client; + Client &client; int getHttpStatusCode(); void skipHeaders(); void closeClient(); From 48e13b8969ff775fc356060095c55f44c22e28db Mon Sep 17 00:00:00 2001 From: David Madison Date: Fri, 2 Oct 2020 03:02:47 -0400 Subject: [PATCH 07/11] Standardize source code whitespace Previously this was a mix of 2 space, 4 space, and tab indentation. This standardizes everything using tabs for indentation and spaces for alignment. --- src/YoutubeApi.cpp | 170 ++++++++++++++++++++++----------------------- src/YoutubeApi.h | 40 +++++------ 2 files changed, 104 insertions(+), 106 deletions(-) diff --git a/src/YoutubeApi.cpp b/src/YoutubeApi.cpp index 571e75e..1458330 100644 --- a/src/YoutubeApi.cpp +++ b/src/YoutubeApi.cpp @@ -27,90 +27,90 @@ YoutubeApi::YoutubeApi(const char* key, Client &client) int YoutubeApi::sendGetToYoutube(const char *command) { client.flush(); - client.setTimeout(YTAPI_TIMEOUT); + client.setTimeout(YTAPI_TIMEOUT); if (!client.connect(YTAPI_HOST, YTAPI_SSL_PORT)) - { - Serial.println(F("Connection failed")); - return false; - } + { + Serial.println(F("Connection failed")); + return false; + } // give the esp a breather - yield(); + yield(); - // Send HTTP request - client.print(F("GET ")); - client.print(command); - client.println(F(" HTTP/1.1")); + // Send HTTP request + client.print(F("GET ")); + client.print(command); + client.println(F(" HTTP/1.1")); //Headers - client.print(F("Host: ")); - client.println(YTAPI_HOST); + client.print(F("Host: ")); + client.println(YTAPI_HOST); - client.println(F("Cache-Control: no-cache")); + client.println(F("Cache-Control: no-cache")); if (client.println() == 0) - { - Serial.println(F("Failed to send request")); - return -1; - } + { + Serial.println(F("Failed to send request")); + return -1; + } int statusCode = getHttpStatusCode(); - - // Let the caller of this method parse the JSon from the client - skipHeaders(); - return statusCode; + + // Let the caller of this method parse the JSon from the client + skipHeaders(); + return statusCode; } -bool YoutubeApi::getChannelStatistics(const char *channelId){ +bool YoutubeApi::getChannelStatistics(const char *channelId) { char command[150] = YTAPI_CHANNEL_ENDPOINT; - char params[120]; - sprintf(params, "?part=statistics&id=%s&key=%s", channelId, apiKey); - strcat(command, params); + char params[120]; + sprintf(params, "?part=statistics&id=%s&key=%s", channelId, apiKey); + strcat(command, params); - if (_debug) - { - Serial.println(command); - } + if (_debug) + { + Serial.println(command); + } bool wasSuccessful = false; - // Get from https://arduinojson.org/v6/assistant/ - const size_t bufferSize = JSON_ARRAY_SIZE(1) - + JSON_OBJECT_SIZE(2) - + 2*JSON_OBJECT_SIZE(4) - + JSON_OBJECT_SIZE(5) - + 330; + // Get from https://arduinojson.org/v6/assistant/ + const size_t bufferSize = JSON_ARRAY_SIZE(1) + + JSON_OBJECT_SIZE(2) + + 2*JSON_OBJECT_SIZE(4) + + JSON_OBJECT_SIZE(5) + + 330; - int httpStatus = sendGetToYoutube(command); + int httpStatus = sendGetToYoutube(command); - if (httpStatus == 200) - { - // Allocate DynamicJsonDocument - DynamicJsonDocument doc(bufferSize); + if (httpStatus == 200) + { + // Allocate DynamicJsonDocument + DynamicJsonDocument doc(bufferSize); - // Parse JSON object - DeserializationError error = deserializeJson(doc, client); - if (!error) - { + // Parse JSON object + DeserializationError error = deserializeJson(doc, client); + if (!error) + { wasSuccessful = true; - JsonObject itemStatistics = doc["items"][0]["statistics"]; + JsonObject itemStatistics = doc["items"][0]["statistics"]; channelStats.viewCount = itemStatistics["viewCount"].as(); channelStats.subscriberCount = itemStatistics["subscriberCount"].as(); channelStats.commentCount = itemStatistics["commentCount"].as(); channelStats.hiddenSubscriberCount = itemStatistics["hiddenSubscriberCount"].as(); channelStats.videoCount = itemStatistics["videoCount"].as(); - } - else - { - Serial.print(F("deserializeJson() failed with code ")); - Serial.println(error.c_str()); - } - } else { - Serial.print("Unexpected HTTP Status Code: "); - Serial.println(httpStatus); - } - closeClient(); + } + else + { + Serial.print(F("deserializeJson() failed with code ")); + Serial.println(error.c_str()); + } + } else { + Serial.print("Unexpected HTTP Status Code: "); + Serial.println(httpStatus); + } + closeClient(); return wasSuccessful; } @@ -119,39 +119,37 @@ bool YoutubeApi::getChannelStatistics(String channelId) { return getChannelStatistics(channelId.c_str()); } -void YoutubeApi::skipHeaders() -{ - // Skip HTTP headers - char endOfHeaders[] = "\r\n\r\n"; - if (!client.find(endOfHeaders)) - { - Serial.println(F("Invalid response")); - return; - } - - // Was getting stray characters between the headers and the body - // This should toss them away - while (client.available() && client.peek() != '{') - { - char c = 0; - client.readBytes(&c, 1); - if (_debug) - { - Serial.print("Tossing an unexpected character: "); - Serial.println(c); - } - } +void YoutubeApi::skipHeaders() { + // Skip HTTP headers + char endOfHeaders[] = "\r\n\r\n"; + if (!client.find(endOfHeaders)) + { + Serial.println(F("Invalid response")); + return; + } + + // Was getting stray characters between the headers and the body + // This should toss them away + while (client.available() && client.peek() != '{') + { + char c = 0; + client.readBytes(&c, 1); + if (_debug) + { + Serial.print("Tossing an unexpected character: "); + Serial.println(c); + } + } } -int YoutubeApi::getHttpStatusCode() -{ - // Check HTTP status - if(client.find("HTTP/1.1")){ - int statusCode = client.parseInt(); - return statusCode; - } +int YoutubeApi::getHttpStatusCode() { + // Check HTTP status + if(client.find("HTTP/1.1")){ + int statusCode = client.parseInt(); + return statusCode; + } - return -1; + return -1; } void YoutubeApi::closeClient() { diff --git a/src/YoutubeApi.h b/src/YoutubeApi.h index 60c2b49..0e3e5c9 100644 --- a/src/YoutubeApi.h +++ b/src/YoutubeApi.h @@ -31,30 +31,30 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA #define YTAPI_CHANNEL_ENDPOINT "/youtube/v3/channels" -struct channelStatistics{ - long viewCount; - long commentCount; - long subscriberCount; - bool hiddenSubscriberCount; - long videoCount; +struct channelStatistics { + long viewCount; + long commentCount; + long subscriberCount; + bool hiddenSubscriberCount; + long videoCount; }; class YoutubeApi { - public: - YoutubeApi (const char *key, Client &client); - int sendGetToYoutube(const char *command); - bool getChannelStatistics(const char *channelId); - bool getChannelStatistics(String channelId); - channelStatistics channelStats; - bool _debug = false; - - private: - const char *apiKey; - Client &client; - int getHttpStatusCode(); - void skipHeaders(); - void closeClient(); + public: + YoutubeApi (const char *key, Client &client); + int sendGetToYoutube(const char *command); + bool getChannelStatistics(const char *channelId); + bool getChannelStatistics(String channelId); + channelStatistics channelStats; + bool _debug = false; + + private: + const char *apiKey; + Client &client; + int getHttpStatusCode(); + void skipHeaders(); + void closeClient(); }; #endif From b7186edc5ac875cc2f4e27fdaf0488bc45a33d9d Mon Sep 17 00:00:00 2001 From: David Madison Date: Fri, 2 Oct 2020 08:03:48 -0400 Subject: [PATCH 08/11] Replace String version of constructor See issue #35 and discussion of replacement in #34. --- src/YoutubeApi.cpp | 4 ++++ src/YoutubeApi.h | 1 + 2 files changed, 5 insertions(+) diff --git a/src/YoutubeApi.cpp b/src/YoutubeApi.cpp index cdcca6c..821198b 100644 --- a/src/YoutubeApi.cpp +++ b/src/YoutubeApi.cpp @@ -31,6 +31,10 @@ YoutubeApi::YoutubeApi(const char* key, Client &client) : apiKey(key), client(client) {} +YoutubeApi::YoutubeApi(String &apiKey, Client &client) + : YoutubeApi(apiKey.c_str(), client) +{} + int YoutubeApi::sendGetToYoutube(const char *command) { client.flush(); client.setTimeout(YTAPI_TIMEOUT); diff --git a/src/YoutubeApi.h b/src/YoutubeApi.h index 320b7dd..fa0cda5 100644 --- a/src/YoutubeApi.h +++ b/src/YoutubeApi.h @@ -49,6 +49,7 @@ class YoutubeApi { public: YoutubeApi (const char *key, Client &client); + YoutubeApi(String& apiKey, Client& client); int sendGetToYoutube(const char *command); bool getChannelStatistics(const char *channelId); bool getChannelStatistics(String channelId); From 36b42bb8bf502f2846aea677b59e1eef2a488792 Mon Sep 17 00:00:00 2001 From: David Madison Date: Wed, 20 Jan 2021 20:52:55 -0500 Subject: [PATCH 09/11] Pass Strings by const reference Still immutable, and more efficient than making a temporary copy --- src/YoutubeApi.cpp | 4 ++-- src/YoutubeApi.h | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/YoutubeApi.cpp b/src/YoutubeApi.cpp index 821198b..e07496a 100644 --- a/src/YoutubeApi.cpp +++ b/src/YoutubeApi.cpp @@ -31,7 +31,7 @@ YoutubeApi::YoutubeApi(const char* key, Client &client) : apiKey(key), client(client) {} -YoutubeApi::YoutubeApi(String &apiKey, Client &client) +YoutubeApi::YoutubeApi(const String &apiKey, Client &client) : YoutubeApi(apiKey.c_str(), client) {} @@ -125,7 +125,7 @@ bool YoutubeApi::getChannelStatistics(const char *channelId) { return wasSuccessful; } -bool YoutubeApi::getChannelStatistics(String channelId) { +bool YoutubeApi::getChannelStatistics(const String& channelId) { return getChannelStatistics(channelId.c_str()); } diff --git a/src/YoutubeApi.h b/src/YoutubeApi.h index fa0cda5..3981f37 100644 --- a/src/YoutubeApi.h +++ b/src/YoutubeApi.h @@ -49,10 +49,10 @@ class YoutubeApi { public: YoutubeApi (const char *key, Client &client); - YoutubeApi(String& apiKey, Client& client); + YoutubeApi(const String& apiKey, Client& client); int sendGetToYoutube(const char *command); bool getChannelStatistics(const char *channelId); - bool getChannelStatistics(String channelId); + bool getChannelStatistics(const String& channelId); channelStatistics channelStats; bool _debug = false; From 52d2fa3e162a3313e574413976365607dfee4392 Mon Sep 17 00:00:00 2001 From: David Madison Date: Wed, 20 Jan 2021 20:59:58 -0500 Subject: [PATCH 10/11] Store internal API key as String Per Brian's insistance on keeping the String version of the constructor (#34), this needs to be stored as a String (or otherwise as a copy on the heap) to prevent issues where the user mutates or destroys the original object the C-string pointer was referencing. --- src/YoutubeApi.cpp | 4 ++-- src/YoutubeApi.h | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/YoutubeApi.cpp b/src/YoutubeApi.cpp index e07496a..fe2edf1 100644 --- a/src/YoutubeApi.cpp +++ b/src/YoutubeApi.cpp @@ -32,7 +32,7 @@ YoutubeApi::YoutubeApi(const char* key, Client &client) {} YoutubeApi::YoutubeApi(const String &apiKey, Client &client) - : YoutubeApi(apiKey.c_str(), client) + : YoutubeApi(apiKey.c_str(), client) // passing the key as c-string to force a copy {} int YoutubeApi::sendGetToYoutube(const char *command) { @@ -73,7 +73,7 @@ int YoutubeApi::sendGetToYoutube(const char *command) { bool YoutubeApi::getChannelStatistics(const char *channelId) { char command[150] = YTAPI_CHANNEL_ENDPOINT; char params[120]; - sprintf(params, "?part=statistics&id=%s&key=%s", channelId, apiKey); + sprintf(params, "?part=statistics&id=%s&key=%s", channelId, apiKey.c_str()); strcat(command, params); if (_debug) diff --git a/src/YoutubeApi.h b/src/YoutubeApi.h index 3981f37..2a06e08 100644 --- a/src/YoutubeApi.h +++ b/src/YoutubeApi.h @@ -48,7 +48,7 @@ struct channelStatistics { class YoutubeApi { public: - YoutubeApi (const char *key, Client &client); + YoutubeApi(const char *key, Client &client); YoutubeApi(const String& apiKey, Client& client); int sendGetToYoutube(const char *command); bool getChannelStatistics(const char *channelId); @@ -57,7 +57,7 @@ class YoutubeApi bool _debug = false; private: - const char *apiKey; + const String apiKey; Client &client; int getHttpStatusCode(); void skipHeaders(); From 00ffc8c9d1931d447960dc12446398d595af0ec2 Mon Sep 17 00:00:00 2001 From: David Madison Date: Wed, 20 Jan 2021 21:03:16 -0500 Subject: [PATCH 11/11] Create String version of the 'sendGet' command --- src/YoutubeApi.cpp | 4 ++++ src/YoutubeApi.h | 1 + 2 files changed, 5 insertions(+) diff --git a/src/YoutubeApi.cpp b/src/YoutubeApi.cpp index fe2edf1..59977c3 100644 --- a/src/YoutubeApi.cpp +++ b/src/YoutubeApi.cpp @@ -70,6 +70,10 @@ int YoutubeApi::sendGetToYoutube(const char *command) { return statusCode; } +int YoutubeApi::sendGetToYoutube(const String& command) { + return sendGetToYoutube(command.c_str()); +} + bool YoutubeApi::getChannelStatistics(const char *channelId) { char command[150] = YTAPI_CHANNEL_ENDPOINT; char params[120]; diff --git a/src/YoutubeApi.h b/src/YoutubeApi.h index 2a06e08..28169ad 100644 --- a/src/YoutubeApi.h +++ b/src/YoutubeApi.h @@ -51,6 +51,7 @@ class YoutubeApi YoutubeApi(const char *key, Client &client); YoutubeApi(const String& apiKey, Client& client); int sendGetToYoutube(const char *command); + int sendGetToYoutube(const String& command); bool getChannelStatistics(const char *channelId); bool getChannelStatistics(const String& channelId); channelStatistics channelStats;