From da92777b8af6aef0dd3e5aabdabbb5da1ee86906 Mon Sep 17 00:00:00 2001 From: Ahson Khan Date: Mon, 25 Nov 2024 16:12:31 -0800 Subject: [PATCH] Add basic operation (GetKeyValue) sample for AppConfig. (#6251) * Add basic operation (GetKeyValue) sample for AppConfig. * Update sample based on offline feedback. --- .../CMakeLists.txt | 4 ++ .../samples/CMakeLists.txt | 18 ++++++ .../samples/appconfig_basic_operation.cpp | 55 +++++++++++++++++++ 3 files changed, 77 insertions(+) create mode 100644 sdk/appconfiguration/azure-data-appconfiguration/samples/CMakeLists.txt create mode 100644 sdk/appconfiguration/azure-data-appconfiguration/samples/appconfig_basic_operation.cpp diff --git a/sdk/appconfiguration/azure-data-appconfiguration/CMakeLists.txt b/sdk/appconfiguration/azure-data-appconfiguration/CMakeLists.txt index 26c667190..6fa4c7b68 100644 --- a/sdk/appconfiguration/azure-data-appconfiguration/CMakeLists.txt +++ b/sdk/appconfiguration/azure-data-appconfiguration/CMakeLists.txt @@ -90,6 +90,10 @@ az_rtti_setup( "azure/data/appconfiguration/rtti.hpp" ) +if (BUILD_SAMPLES) + add_subdirectory(samples) +endif() + if(BUILD_TESTING) if (NOT AZ_ALL_LIBRARIES OR FETCH_SOURCE_DEPS) include(AddGoogleTest) diff --git a/sdk/appconfiguration/azure-data-appconfiguration/samples/CMakeLists.txt b/sdk/appconfiguration/azure-data-appconfiguration/samples/CMakeLists.txt new file mode 100644 index 000000000..db96f6cad --- /dev/null +++ b/sdk/appconfiguration/azure-data-appconfiguration/samples/CMakeLists.txt @@ -0,0 +1,18 @@ +# Copyright (c) Microsoft Corporation. +# Licensed under the MIT License. + +cmake_minimum_required (VERSION 3.13) + +project (app_config_sample LANGUAGES CXX) +set(CMAKE_CXX_STANDARD 14) +set(CMAKE_CXX_STANDARD_REQUIRED True) + +if(MSVC) + add_compile_definitions(_CRT_SECURE_NO_WARNINGS) +endif() + +add_executable(app_config_sample appconfig_basic_operation.cpp) +target_link_libraries(app_config_sample PRIVATE azure-data-appconfiguration azure-identity) +target_include_directories(app_config_sample PRIVATE .) +target_compile_definitions(app_config_sample PRIVATE _azure_BUILDING_SAMPLES) +create_per_service_target_build_for_sample(appconfiguration app_config_sample DISABLE_RUN) diff --git a/sdk/appconfiguration/azure-data-appconfiguration/samples/appconfig_basic_operation.cpp b/sdk/appconfiguration/azure-data-appconfiguration/samples/appconfig_basic_operation.cpp new file mode 100644 index 000000000..df3f03d91 --- /dev/null +++ b/sdk/appconfiguration/azure-data-appconfiguration/samples/appconfig_basic_operation.cpp @@ -0,0 +1,55 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +/** + * @brief This sample provides the code implementation to use the App Configuration client SDK for + * C++ to get one or more configuration settings. + */ + +#include +#include + +#include + +using namespace Azure::Data::AppConfiguration; +using namespace Azure::Identity; + +int main() +{ + try + { + std::string url = "https://.azconfig.io"; + auto credential = std::make_shared(); + + // create client + ConfigurationClient configurationClient(url, credential); + + Azure::Response response = configurationClient.GetKeyValue("key", "accept"); + + GetKeyValueResult result = response.Value; + Azure::Nullable valueOfKey = result.Value; + + if (valueOfKey.HasValue()) + { + std::cout << valueOfKey.Value() << std::endl; + } + else + { + std::cout << "Value for: '" + << "key" + << "' does not exist." << std::endl; + } + } + catch (Azure::Core::Credentials::AuthenticationException const& e) + { + std::cout << "Authentication error:" << std::endl << e.what() << std::endl; + return 1; + } + catch (Azure::Core::RequestFailedException const& e) + { + std::cout << "Client request failed error:" << std::endl << e.what() << std::endl; + return 1; + } + + return 0; +}