Add basic operation (GetKeyValue) sample for AppConfig. (#6251)

* Add basic operation (GetKeyValue) sample for AppConfig.

* Update sample based on offline feedback.
This commit is contained in:
Ahson Khan 2024-11-25 16:12:31 -08:00 committed by GitHub
parent 6d5c55a2c7
commit da92777b8a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 77 additions and 0 deletions

View File

@ -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)

View File

@ -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)

View File

@ -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 <azure/data/appconfiguration.hpp>
#include <azure/identity.hpp>
#include <iostream>
using namespace Azure::Data::AppConfiguration;
using namespace Azure::Identity;
int main()
{
try
{
std::string url = "https://<your-appconfig-name>.azconfig.io";
auto credential = std::make_shared<Azure::Identity::DefaultAzureCredential>();
// create client
ConfigurationClient configurationClient(url, credential);
Azure::Response<GetKeyValueResult> response = configurationClient.GetKeyValue("key", "accept");
GetKeyValueResult result = response.Value;
Azure::Nullable<std::string> 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;
}