From 6214f95e8593f890a05d7cb9a46dd779629b06a7 Mon Sep 17 00:00:00 2001 From: Alfred Gedeon Date: Sun, 3 Dec 2023 19:37:43 -0800 Subject: [PATCH] Fix samples --- .../websocket_connect/README_custom_auth.md | 35 +++++----- .../mqtt/websocket_connect/README_proxy.md | 67 ++++++++++--------- .../README_static_credentials.md | 27 ++++---- .../README_username_password.md | 31 +++++---- samples/mqtt/websocket_connect/main.cpp | 37 +++++----- 5 files changed, 106 insertions(+), 91 deletions(-) diff --git a/samples/mqtt/websocket_connect/README_custom_auth.md b/samples/mqtt/websocket_connect/README_custom_auth.md index fa5a9aec3..9f4916100 100644 --- a/samples/mqtt/websocket_connect/README_custom_auth.md +++ b/samples/mqtt/websocket_connect/README_custom_auth.md @@ -39,29 +39,30 @@ For this sample, using Websockets will attempt to fetch the AWS credentials to a
(code snipet to replace similar section)
 
-Utils::cmdData cmdData = Utils::parseSampleInputCustomAuthorizerConnect(argc, argv, &apiHandle);
-
-// Create the MQTT builder and populate it with data from cmdData.
-Aws::Iot::MqttClient client;
+void connection_setup(int argc, char *argv[], ApiHandle &apiHandle, Utils::cmdData &cmdData,
+    Aws::Iot::MqttClientConnectionConfigBuilder &clientConfigBuilder)
+{
+    cmdData = Utils::parseSampleInputCustomAuthorizerConnect(argc, argv, &apiHandle);
 
-Aws::Crt::Auth::CredentialsProviderChainDefaultConfig defaultConfig;
+    Aws::Crt::Auth::CredentialsProviderChainDefaultConfig defaultConfig;
 
-std::shared_ptr provider =
-    Aws::Crt::Auth::CredentialsProvider::CreateCredentialsProviderChainDefault(defaultConfig);
+    std::shared_ptr provider =
+        Aws::Crt::Auth::CredentialsProvider::CreateCredentialsProviderChainDefault(defaultConfig);
 
-Aws::Iot::WebsocketConfig websocketConfig((cmdData.input_signingRegion), provider);
+    Aws::Iot::WebsocketConfig websocketConfig((cmdData.input_signingRegion), provider);
 
-auto clientConfigBuilder = Aws::Iot::MqttClientConnectionConfigBuilder(websocketConfig);
+    clientConfigBuilder = Aws::Iot::MqttClientConnectionConfigBuilder(websocketConfig);
 
-clientConfigBuilder.WithEndpoint((cmdData.input_endpoint));
+    clientConfigBuilder.WithEndpoint((cmdData.input_endpoint));
 
-clientConfigBuilder.WithCustomAuthorizer(
-    (cmdData.input_customAuthUsername),
-    (cmdData.input_customAuthorizerName),
-    (cmdData.input_customAuthorizerSignature),
-    (cmdData.input_customAuthPassword),
-    (cmdData.input_customTokenKeyName),
-    (cmdData.input_customTokenValue));
+    clientConfigBuilder.WithCustomAuthorizer(
+        (cmdData.input_customAuthUsername),
+        (cmdData.input_customAuthorizerName),
+        (cmdData.input_customAuthorizerSignature),
+        (cmdData.input_customAuthPassword),
+        (cmdData.input_customTokenKeyName),
+        (cmdData.input_customTokenValue));
+}
 
 
diff --git a/samples/mqtt/websocket_connect/README_proxy.md b/samples/mqtt/websocket_connect/README_proxy.md index 578af356f..a73d685b3 100644 --- a/samples/mqtt/websocket_connect/README_proxy.md +++ b/samples/mqtt/websocket_connect/README_proxy.md @@ -36,43 +36,44 @@ For this sample, using Websockets will attempt to fetch the AWS credentials to a
- (code snipet to replace similar section) + (code snipet to replace the connection_setup function)
 
-Utils::cmdData cmdData = Utils::parseSampleInputWebsocketConnect(argc, argv, &apiHandle);
-
-// Create the MQTT builder and populate it with data from cmdData.
-Aws::Iot::MqttClient client;
-Aws::Iot::MqttClientConnectionConfigBuilder clientConfigBuilder;
-std::shared_ptr provider = nullptr;
-Aws::Crt::Auth::CredentialsProviderChainDefaultConfig defaultConfig;
-provider = Aws::Crt::Auth::CredentialsProvider::CreateCredentialsProviderChainDefault(defaultConfig);
-if (!provider)
+void connection_setup(int argc, char *argv[], ApiHandle &apiHandle, Utils::cmdData &cmdData,
+             Aws::Iot::MqttClientConnectionConfigBuilder &clientConfigBuilder)
 {
-    fprintf(stderr, "Failure to create credentials provider!\n");
-    exit(-1);
-}
-Aws::Iot::WebsocketConfig config(cmdData.input_signingRegion, provider);
-clientConfigBuilder = Aws::Iot::MqttClientConnectionConfigBuilder(config);
-if (cmdData.input_ca != "")
-{
-    clientConfigBuilder.WithCertificateAuthority(cmdData.input_ca.c_str());
-}
-if (cmdData.input_proxyHost == "")
-{
-    fprintf(stderr, "proxy address missing!\n");
-    exit(-1);
-}
-Aws::Crt::Http::HttpClientConnectionProxyOptions proxyOptions;
-proxyOptions.HostName = cmdData.input_proxyHost;
-proxyOptions.Port = static_cast(cmdData.input_proxyPort);
-proxyOptions.AuthType = Aws::Crt::Http::AwsHttpProxyAuthenticationType::None;
-clientConfigBuilder.WithHttpProxyOptions(proxyOptions);
+    Utils::cmdData cmdData = Utils::parseSampleInputWebsocketConnect(argc, argv, &apiHandle);
 
-if (cmdData.input_port != 0)
-{
-    clientConfigBuilder.WithPortOverride(static_cast(cmdData.input_port));
+    std::shared_ptr provider = nullptr;
+    Aws::Crt::Auth::CredentialsProviderChainDefaultConfig defaultConfig;
+    provider = Aws::Crt::Auth::CredentialsProvider::CreateCredentialsProviderChainDefault(defaultConfig);
+    if (!provider)
+    {
+        fprintf(stderr, "Failure to create credentials provider!\n");
+        exit(-1);
+    }
+    Aws::Iot::WebsocketConfig config(cmdData.input_signingRegion, provider);
+    clientConfigBuilder = Aws::Iot::MqttClientConnectionConfigBuilder(config);
+    if (cmdData.input_ca != "")
+    {
+        clientConfigBuilder.WithCertificateAuthority(cmdData.input_ca.c_str());
+    }
+    if (cmdData.input_proxyHost == "")
+    {
+        fprintf(stderr, "proxy address missing!\n");
+        exit(-1);
+    }
+    Aws::Crt::Http::HttpClientConnectionProxyOptions proxyOptions;
+    proxyOptions.HostName = cmdData.input_proxyHost;
+    proxyOptions.Port = static_cast(cmdData.input_proxyPort);
+    proxyOptions.AuthType = Aws::Crt::Http::AwsHttpProxyAuthenticationType::None;
+    clientConfigBuilder.WithHttpProxyOptions(proxyOptions);
+
+    if (cmdData.input_port != 0)
+    {
+        clientConfigBuilder.WithPortOverride(static_cast(cmdData.input_port));
+    }
+    clientConfigBuilder.WithEndpoint(cmdData.input_endpoint);
 }
-clientConfigBuilder.WithEndpoint(cmdData.input_endpoint);
 
 
diff --git a/samples/mqtt/websocket_connect/README_static_credentials.md b/samples/mqtt/websocket_connect/README_static_credentials.md index 118d15c45..0118dcf8b 100644 --- a/samples/mqtt/websocket_connect/README_static_credentials.md +++ b/samples/mqtt/websocket_connect/README_static_credentials.md @@ -40,23 +40,26 @@ For this sample, using Websockets will attempt to fetch the AWS credentials to a (code snipet to replace similar section)
 
 
-Utils::cmdData cmdData = Utils::parseSampleInputWebsocketConnect(argc, argv, &apiHandle);
+void connection_setup(int argc, char *argv[], ApiHandle &apiHandle, Utils::cmdData &cmdData,
+    Aws::Iot::MqttClientConnectionConfigBuilder &clientConfigBuilder)
+{
+    cmdData = Utils::parseSampleInputWebsocketConnect(argc, argv, &apiHandle);
 
-Aws::Crt::Auth::CredentialsProviderChainDefaultConfig defaultConfig;
+    Aws::Crt::Auth::CredentialsProviderChainDefaultConfig defaultConfig;
 
-std::shared_ptr provider = nullptr;
-Aws::Crt::Auth::CredentialsProviderStaticConfig providerConfig;
+    std::shared_ptr provider = nullptr;
+    Aws::Crt::Auth::CredentialsProviderStaticConfig providerConfig;
 
-providerConfig.AccessKeyId = aws_byte_cursor_from_c_str((cmdData.input_accessKeyId.c_str()));
-providerConfig.SecretAccessKey = aws_byte_cursor_from_c_str((cmdData.input_secretAccessKey.c_str()));
-providerConfig.SessionToken = aws_byte_cursor_from_c_str((cmdData.input_sessionToken.c_str()));
+    providerConfig.AccessKeyId = aws_byte_cursor_from_c_str((cmdData.input_accessKeyId.c_str()));
+    providerConfig.SecretAccessKey = aws_byte_cursor_from_c_str((cmdData.input_secretAccessKey.c_str()));
+    providerConfig.SessionToken = aws_byte_cursor_from_c_str((cmdData.input_sessionToken.c_str()));
 
-provider = Aws::Crt::Auth::CredentialsProvider::CreateCredentialsProviderStatic(providerConfig);
-Aws::Iot::WebsocketConfig config(cmdData.input_signingRegion, provider);
+    provider = Aws::Crt::Auth::CredentialsProvider::CreateCredentialsProviderStatic(providerConfig);
+    Aws::Iot::WebsocketConfig config(cmdData.input_signingRegion, provider);
 
-Aws::Iot::MqttClient client;
-auto clientConfigBuilder = Aws::Iot::MqttClientConnectionConfigBuilder(config);
-clientConfigBuilder.WithEndpoint((cmdData.input_endpoint));
+    clientConfigBuilder = Aws::Iot::MqttClientConnectionConfigBuilder(config);
+    clientConfigBuilder.WithEndpoint((cmdData.input_endpoint));
+}
 
 
diff --git a/samples/mqtt/websocket_connect/README_username_password.md b/samples/mqtt/websocket_connect/README_username_password.md index 49e454e34..44178871b 100644 --- a/samples/mqtt/websocket_connect/README_username_password.md +++ b/samples/mqtt/websocket_connect/README_username_password.md @@ -38,22 +38,25 @@ For this sample, using Websockets will attempt to fetch the AWS credentials to a
(code snipet to replace similar section)
 
-Utils::cmdData cmdData = Utils::parseSampleInputCustomAuthorizerConnect(argc, argv, &apiHandle);
+void connection_setup(int argc, char *argv[], ApiHandle &apiHandle, Utils::cmdData &cmdData,
+    Aws::Iot::MqttClientConnectionConfigBuilder &clientConfigBuilder)
+{
+    cmdData = Utils::parseSampleInputCustomAuthorizerConnect(argc, argv, &apiHandle);
 
-// Create the MQTT builder and populate it with data from cmdData.
-Aws::Iot::MqttClient client;
-Aws::Crt::Auth::CredentialsProviderChainDefaultConfig defaultConfig;
-std::shared_ptr provider =
-    Aws::Crt::Auth::CredentialsProvider::CreateCredentialsProviderChainDefault(defaultConfig);
-Aws::Iot::WebsocketConfig websocketConfig((cmdData.input_signingRegion), provider);
+    // Create the MQTT builder and populate it with data from cmdData.
+    Aws::Crt::Auth::CredentialsProviderChainDefaultConfig defaultConfig;
+    std::shared_ptr provider =
+        Aws::Crt::Auth::CredentialsProvider::CreateCredentialsProviderChainDefault(defaultConfig);
+    Aws::Iot::WebsocketConfig websocketConfig((cmdData.input_signingRegion), provider);
 
-auto clientConfigBuilder = Aws::Iot::MqttClientConnectionConfigBuilder(websocketConfig);
-clientConfigBuilder.WithEndpoint((cmdData.input_endpoint));
-clientConfigBuilder.WithCustomAuthorizer(
-    (cmdData.input_customAuthUsername),
-    (cmdData.input_customAuthorizerName),
-    (cmdData.input_customAuthorizerSignature),
-    (cmdData.input_customAuthPassword));
+    clientConfigBuilder = Aws::Iot::MqttClientConnectionConfigBuilder(websocketConfig);
+    clientConfigBuilder.WithEndpoint((cmdData.input_endpoint));
+    clientConfigBuilder.WithCustomAuthorizer(
+        (cmdData.input_customAuthUsername),
+        (cmdData.input_customAuthorizerName),
+        (cmdData.input_customAuthorizerSignature),
+        (cmdData.input_customAuthPassword));
+}
 
 
diff --git a/samples/mqtt/websocket_connect/main.cpp b/samples/mqtt/websocket_connect/main.cpp index 45cb92e44..d73562bca 100644 --- a/samples/mqtt/websocket_connect/main.cpp +++ b/samples/mqtt/websocket_connect/main.cpp @@ -17,23 +17,11 @@ using namespace Aws::Crt; -int main(int argc, char *argv[]) +void connection_setup(int argc, char *argv[], ApiHandle &apiHandle, Utils::cmdData &cmdData, + Aws::Iot::MqttClientConnectionConfigBuilder &clientConfigBuilder) { - /************************ Setup ****************************/ - - // Do the global initialization for the API. - ApiHandle apiHandle; - - /** - * cmdData is the arguments/input from the command line placed into a single struct for - * use in this sample. This handles all of the command line parsing, validating, etc. - * See the Utils/CommandLineUtils for more information. - */ - Utils::cmdData cmdData = Utils::parseSampleInputWebsocketConnect(argc, argv, &apiHandle); + cmdData = Utils::parseSampleInputWebsocketConnect(argc, argv, &apiHandle); - // Create the MQTT builder and populate it with data from cmdData. - Aws::Iot::MqttClient client; - Aws::Iot::MqttClientConnectionConfigBuilder clientConfigBuilder; std::shared_ptr provider = nullptr; Aws::Crt::Auth::CredentialsProviderChainDefaultConfig defaultConfig; provider = Aws::Crt::Auth::CredentialsProvider::CreateCredentialsProviderChainDefault(defaultConfig); @@ -61,6 +49,25 @@ int main(int argc, char *argv[]) clientConfigBuilder.WithPortOverride(static_cast(cmdData.input_port)); } clientConfigBuilder.WithEndpoint(cmdData.input_endpoint); +} + +int main(int argc, char *argv[]) +{ + /************************ Setup ****************************/ + + // Do the global initialization for the API. + ApiHandle apiHandle; + /** + * cmdData is the arguments/input from the command line placed into a single struct for + * use in this sample. This handles all of the command line parsing, validating, etc. + * See the Utils/CommandLineUtils for more information. + */ + Utils::cmdData cmdData; + Aws::Iot::MqttClient client; + // Create the MQTT builder and populate it with data from cmdData. + Aws::Iot::MqttClientConnectionConfigBuilder clientConfigBuilder; + + connection_setup(argc, argv, apiHandle, cmdData, clientConfigBuilder); // Create the MQTT connection from the MQTT builder auto clientConfig = clientConfigBuilder.Build();