Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make sample service an object library #3728

Merged
merged 1 commit into from
Jun 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions samples/helpers/service/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@ set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)

add_library(
service
INTERFACE
OBJECT
inc/azure/service/client.hpp
)
src/client.cpp
)

target_include_directories(service INTERFACE inc)
target_link_libraries(service PUBLIC azure-core)

target_include_directories(service PUBLIC inc)
35 changes: 9 additions & 26 deletions samples/helpers/service/inc/azure/service/client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@

#pragma once

#include <azure/core/context.hpp>
#include <azure/core/credentials/credentials.hpp>

#include <memory>
#include <string>
#include <utility>

namespace Azure { namespace Service {
Expand All @@ -26,32 +28,13 @@ namespace Azure { namespace Service {
static_cast<void>(serviceUrl); // to suppress the "unused variable" warning.
}

void DoSomething(const Core::Context& context) const
{
static_cast<void>(context); // to suppress the "unused variable" warning.

// This method does nothing, because the purpose of this class is to demonstrate
// how Azure::Identity classes can be used with a generic Azure SDK service client.
// If we have code here that gets the token, it would be up to the user to set it up to be
// valid enough to get a token, which is not critical for the intended demonstration purposes.
// And if user runs this, and authentication is unsuccessful, it may draw an unnecessary
// attention to an irrelevant (to the demo) point.

// But an oversimplified logic of what a typical Azure SDK client does is below:
#if (0)
// Every client has its own scope. We use management.azure.com here as an example.
Core::Credentials::TokenRequestContext azureServiceClientContext;
azureServiceClientContext.Scopes = {"https://management.azure.com/"};

auto authenticationToken = m_credential->GetToken(azureServiceClientContext, context);

// Now that it has a token, Client can authorize and DoSomething().
// ...
// ...

static_cast<void>(authenticationToken); // to suppress the "unused variable" warning.
#endif
}
// This method does nothing, because the purpose of this class is to demonstrate how
// Azure::Identity classes can be used with a generic Azure SDK service client. If we have code
// here that gets the token, it would be up to the user to set it up to be valid enough to get a
// token, which is not critical for the intended demonstration purposes. And if user runs this,
// and authentication is unsuccessful, it may draw an unnecessary attention to an irrelevant (to
// the demo) point.
void DoSomething(const Core::Context& context) const;
};

}} // namespace Azure::Service
24 changes: 24 additions & 0 deletions samples/helpers/service/src/client.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// SPDX-License-Identifier: MIT

#include "azure/service/client.hpp"

void Azure::Service::Client::DoSomething(const Azure::Core::Context& context) const
{
static_cast<void>(context); // to suppress the "unused variable" warning.

// An oversimplified logic of what a typical Azure SDK client does is below:
#if (0)
// Every client has its own scope. We use management.azure.com here as an example.
Core::Credentials::TokenRequestContext azureServiceClientContext;
azureServiceClientContext.Scopes = {"https://management.azure.com/"};

auto authenticationToken = m_credential->GetToken(azureServiceClientContext, context);

// Now that it has a token, Client can authorize and DoSomething().
// ...
// ...

static_cast<void>(authenticationToken); // to suppress the "unused variable" warning.
#endif
}