Skip to content

Commit

Permalink
Swap dynamo client to use a smithy base class
Browse files Browse the repository at this point in the history
  • Loading branch information
sbiscigl committed Sep 18, 2024
1 parent 4cad430 commit 732c1fc
Show file tree
Hide file tree
Showing 35 changed files with 1,637 additions and 2,150 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,18 @@
#pragma once
#include <aws/dynamodb/DynamoDB_EXPORTS.h>
#include <aws/core/client/ClientConfiguration.h>
#include <aws/core/client/AWSClient.h>
#include <aws/core/client/AWSClientAsyncCRTP.h>
#include <aws/core/utils/json/JsonSerializer.h>
#include <aws/dynamodb/DynamoDBServiceClientModel.h>
#include <smithy/client/AwsSmithyClient.h>
#include <smithy/identity/auth/built-in/SigV4AuthSchemeResolver.h>
#include <smithy/identity/auth/built-in/SigV4AuthScheme.h>
#include <smithy/client/serializer/JsonOutcomeSerializer.h>

namespace Aws
{
namespace DynamoDB
{
AWS_DYNAMODB_API extern const char SERVICE_NAME[];
/**
* <fullname>Amazon DynamoDB</fullname> <p>Amazon DynamoDB is a fully managed NoSQL
* database service that provides fast and predictable performance with seamless
Expand All @@ -33,10 +36,16 @@ namespace DynamoDB
* Zones in an Amazon Web Services Region, providing built-in high availability and
* data durability.</p>
*/
class AWS_DYNAMODB_API DynamoDBClient : public Aws::Client::AWSJsonClient, public Aws::Client::ClientWithAsyncTemplateMethods<DynamoDBClient>
class AWS_DYNAMODB_API DynamoDBClient : smithy::client::AwsSmithyClientT<Aws::DynamoDB::SERVICE_NAME,
Aws::DynamoDB::DynamoDBClientConfiguration,
smithy::SigV4AuthSchemeResolver<>,
Aws::Crt::Variant<smithy::SigV4AuthScheme>,
DynamoDBEndpointProviderBase,
smithy::client::JsonOutcomeSerializer,
smithy::client::JsonOutcome>,
Aws::Client::ClientWithAsyncTemplateMethods<DynamoDBClient>
{
public:
typedef Aws::Client::AWSJsonClient BASECLASS;
static const char* GetServiceName();
static const char* GetAllocationTag();

Expand Down Expand Up @@ -2275,9 +2284,12 @@ namespace DynamoDB
friend class Aws::Client::ClientWithAsyncTemplateMethods<DynamoDBClient>;
void init(const DynamoDBClientConfiguration& clientConfiguration);

void OptionallyUpdateDescribeEndpointsCache(Aws::Endpoint::AWSEndpoint& resolvedEndpoint,
const Aws::String& operationName,
const Aws::String& endpointKey,
const Aws::DynamoDB::Model::DescribeEndpointsRequest& endpointRequest,
bool enforceDiscovery) const;
mutable Aws::Utils::ConcurrentCache<Aws::String, Aws::String> m_endpointsCache;
DynamoDBClientConfiguration m_clientConfiguration;
std::shared_ptr<DynamoDBEndpointProviderBase> m_endpointProvider;
};

} // namespace DynamoDB
Expand Down
2,645 changes: 540 additions & 2,105 deletions generated/src/aws-cpp-sdk-dynamodb/source/DynamoDBClient.cpp

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions src/aws-cpp-sdk-core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ file(GLOB SMITHY_CLIENT_HEADERS "include/smithy/client/*.h")
file(GLOB SMITHY_CLIENT_IMPL_HEADERS "include/smithy/client/impl/*.h")
file(GLOB SMITHY_CLIENT_COMMON_HEADERS "include/smithy/client/common/*.h")
file(GLOB SMITHY_CLIENT_FEATURES_HEADERS "include/smithy/client/features/*.h")
file(GLOB SMITHY_CLIENT_SERIALIZER_HEADERS "include/smithy/client/serializer/*.h")
file(GLOB SMITHY_TRACING_HEADERS "include/smithy/tracing/*.h")
file(GLOB SMITHY_IDENTITY_HEADERS "include/smithy/identity/*.h")
file(GLOB SMITHY_IDENTITY_AUTH_HEADERS "include/smithy/identity/auth/*.h")
Expand Down Expand Up @@ -346,6 +347,7 @@ file(GLOB AWS_NATIVE_SDK_COMMON_HEADERS
${SMITHY_CLIENT_IMPL_HEADERS}
${SMITHY_CLIENT_COMMON_HEADERS}
${SMITHY_CLIENT_FEATURES_HEADERS}
${SMITHY_CLIENT_SERIALIZER_HEADERS}
${SMITHY_TRACING_HEADERS}
${SMITHY_IDENTITY_HEADERS}
${SMITHY_IDENTITY_AUTH_HEADERS}
Expand Down Expand Up @@ -478,6 +480,7 @@ if(MSVC)
source_group("Header Files\\smithy\\client" FILES ${SMITHY_CLIENT_IMPL_HEADERS})
source_group("Header Files\\smithy\\client" FILES ${SMITHY_CLIENT_COMMON_HEADERS})
source_group("Header Files\\smithy\\client" FILES ${SMITHY_CLIENT_FEATURES_HEADERS})
source_group("Header Files\\smithy\\client" FILES ${SMITHY_CLIENT_SERIALIZER_HEADERS})
source_group("Header Files\\smithy\\tracing" FILES ${SMITHY_TRACING_HEADERS})
source_group("Header Files\\smithy\\identity\\auth" FILES ${SMITHY_IDENTITY_AUTH_HEADERS})
source_group("Header Files\\smithy\\identity\\auth\\impl" FILES ${SMITHY_IDENTITY_AUTH_IMPL_HEADERS})
Expand Down Expand Up @@ -751,13 +754,18 @@ install (FILES ${CJSON_HEADERS} DESTINATION ${INCLUDE_DIRECTORY}/aws/core/extern
install (FILES ${TINYXML2_HEADERS} DESTINATION ${INCLUDE_DIRECTORY}/aws/core/external/tinyxml2)
install (FILES ${SMITHY_HEADERS} DESTINATION ${INCLUDE_DIRECTORY}/smithy)
install (FILES ${SMITHY_CLIENT_HEADERS} DESTINATION ${INCLUDE_DIRECTORY}/smithy/client)
install (FILES ${SMITHY_CLIENT_IMPL_HEADERS} DESTINATION ${INCLUDE_DIRECTORY}/smithy/client/impl)
install (FILES ${SMITHY_CLIENT_COMMON_HEADERS} DESTINATION ${INCLUDE_DIRECTORY}/smithy/client/common)
install (FILES ${SMITHY_CLIENT_FEATURES_HEADERS} DESTINATION ${INCLUDE_DIRECTORY}/smithy/client/features)
install (FILES ${SMITHY_CLIENT_SERIALIZER_HEADERS} DESTINATION ${INCLUDE_DIRECTORY}/smithy/client/serializer)
install (FILES ${SMITHY_TRACING_HEADERS} DESTINATION ${INCLUDE_DIRECTORY}/smithy/tracing)
install (FILES ${SMITHY_IDENTITY_HEADERS} DESTINATION ${INCLUDE_DIRECTORY}/smithy/identity)
install (FILES ${SMITHY_IDENTITY_AUTH_HEADERS} DESTINATION ${INCLUDE_DIRECTORY}/smithy/identity/auth)
install (FILES ${SMITHY_IDENTITY_AUTH_IMPL_HEADERS} DESTINATION ${INCLUDE_DIRECTORY}/smithy/identity/auth/impl)
install (FILES ${SMITHY_IDENTITY_AUTH_BUILTIN_HEADERS} DESTINATION ${INCLUDE_DIRECTORY}/smithy/identity/auth/built-in)
install (FILES ${SMITHY_IDENTITY_IDENTITY_HEADERS} DESTINATION ${INCLUDE_DIRECTORY}/smithy/identity/identity)
install (FILES ${SMITHY_IDENTITY_RESOLVER_HEADERS} DESTINATION ${INCLUDE_DIRECTORY}/smithy/identity/resolver)
install (FILES ${SMITHY_IDENTITY_RESOLVER_BUILTIN_HEADERS} DESTINATION ${INCLUDE_DIRECTORY}/smithy/identity/resolver/built-in)
install (FILES ${SMITHY_IDENTITY_IDENTITY_IMPL_HEADERS} DESTINATION ${INCLUDE_DIRECTORY}/smithy/identity/identity/impl)
install (FILES ${SMITHY_IDENTITY_RESOLVER_IMPL_HEADERS} DESTINATION ${INCLUDE_DIRECTORY}/smithy/identity/resolver/impl)
install (FILES ${SMITHY_IDENTITY_SIGNER_HEADERS} DESTINATION ${INCLUDE_DIRECTORY}/smithy/identity/signer)
Expand Down
23 changes: 18 additions & 5 deletions src/aws-cpp-sdk-core/include/smithy/client/AwsSmithyClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ namespace client
typename ServiceClientConfigurationT,
typename ServiceAuthSchemeResolverT,
typename AuthSchemesVariantT,
typename EndpointProviderT>
typename EndpointProviderT,
typename SerializerT,
typename ResponseT>
class AwsSmithyClientT : public AwsSmithyClientBase
{
public:
Expand All @@ -38,10 +40,11 @@ namespace client
const std::shared_ptr<ServiceAuthSchemeResolverT>& authSchemeResolver,
const Aws::UnorderedMap<Aws::String, AuthSchemesVariantT>& authSchemes)
: AwsSmithyClientBase(Aws::MakeUnique<ServiceClientConfigurationT>(ServiceNameT, clientConfig), serviceName, httpClient, errorMarshaller),
m_clientConfig(*AwsSmithyClientBase::m_clientConfig.get()),
m_clientConfiguration(*static_cast<ServiceClientConfigurationT*>(AwsSmithyClientBase::m_clientConfig.get())),
m_endpointProvider(endpointProvider),
m_authSchemeResolver(authSchemeResolver),
m_authSchemes(authSchemes)
m_authSchemes(authSchemes),
m_serializer(Aws::MakeUnique<SerializerT>(ServiceNameT, m_clientConfiguration.telemetryProvider))
{
m_serviceName = ServiceNameT;
}
Expand Down Expand Up @@ -69,7 +72,7 @@ namespace client

identityParams.serviceName = m_serviceName;
identityParams.operation = ctx.m_requestName;
identityParams.region = m_clientConfig.region;
identityParams.region = m_clientConfiguration.region;

if (ctx.m_pRequest) {
// refactor once auth scheme resolver will use it's own rule set
Expand Down Expand Up @@ -118,11 +121,21 @@ namespace client
return AwsClientRequestSigning<AuthSchemesVariantT>::AdjustClockSkew(outcome, authSchemeOption, m_authSchemes);
}

ResponseT MakeRequestDeserialize(Aws::AmazonWebServiceRequest const * const request,
const char* requestName,
Aws::Http::HttpMethod method,
EndpointUpdateCallback&& endpointCallback) const
{
auto httpResponseOutcome = MakeRequestSync(request, requestName, method, std::move(endpointCallback));
return m_serializer->Deserialize(std::move(httpResponseOutcome), GetServiceClientName(), requestName);
}

protected:
ServiceClientConfigurationT& m_clientConfig;
ServiceClientConfigurationT& m_clientConfiguration;
std::shared_ptr<EndpointProviderT> m_endpointProvider{};
std::shared_ptr<ServiceAuthSchemeResolverT> m_authSchemeResolver{};
Aws::UnorderedMap<Aws::String, AuthSchemesVariantT> m_authSchemes{};
Aws::UniquePtr<SerializerT> m_serializer{};
};

} // namespace client
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@ namespace client
std::shared_ptr<Aws::Http::HttpResponse> httpResponse) const;

inline virtual const char* GetServiceClientName() const { return m_serviceName.c_str(); }
inline virtual const std::shared_ptr<Aws::Http::HttpClient>& GetHttpClient() { return m_httpClient; }
virtual void DisableRequestProcessing();

virtual ResolveEndpointOutcome ResolveEndpoint(const Aws::Endpoint::EndpointParameters& endpointParameters, EndpointUpdateCallback&& epCallback) const = 0;
virtual SelectAuthSchemeOptionOutcome SelectAuthSchemeOption(const AwsSmithyClientAsyncRequestContext& ctx) const = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ namespace smithy
return;
}

auto identityResult = identityResolver->getIdentity(m_targetAuthSchemeOption.identityProperties, m_targetAuthSchemeOption.identityProperties);
auto identityResult = identityResolver->getIdentity(m_targetAuthSchemeOption.identityProperties(), m_targetAuthSchemeOption.identityProperties());

if (!identityResult.IsSuccess())
{
Expand All @@ -133,7 +133,7 @@ namespace smithy
return;
}

result.emplace(signer->sign(m_httpRequest, *identity, m_targetAuthSchemeOption.signerProperties));
result.emplace(signer->sign(m_httpRequest, *identity, m_targetAuthSchemeOption.signerProperties()));
}
};

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/**
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0.
*/
#pragma once
#include <aws/core/AmazonWebServiceResult.h>
#include <aws/core/client/AWSError.h>
#include <aws/core/utils/Outcome.h>
#include <aws/core/utils/json/JsonSerializer.h>
#include <smithy/tracing/TracingUtils.h>
#include <smithy/tracing/TelemetryProvider.h>

namespace smithy
{
namespace client
{
using TracingUtils = components::tracing::TracingUtils;
using CoreErrors = Aws::Client::CoreErrors;
using AWSError = Aws::Client::AWSError<CoreErrors>;
using JsonValue = Aws::Utils::Json::JsonValue;
using HttpResponseOutcome = Aws::Utils::Outcome<std::shared_ptr<Aws::Http::HttpResponse>, AWSError>;
using JsonOutcome = Aws::Utils::Outcome<Aws::AmazonWebServiceResult<JsonValue>, AWSError>;
using TelemetryProvider = components::tracing::TelemetryProvider;

class JsonOutcomeSerializer
{
public:
explicit JsonOutcomeSerializer(const std::shared_ptr<TelemetryProvider>& telemetryProvider)
: m_telemetryProvider(telemetryProvider)
{
}

JsonOutcomeSerializer(const JsonOutcomeSerializer& other) = delete;
JsonOutcomeSerializer(JsonOutcomeSerializer&& other) noexcept = default;
JsonOutcomeSerializer& operator=(const JsonOutcomeSerializer& other) = delete;
JsonOutcomeSerializer& operator=(JsonOutcomeSerializer&& other) noexcept = default;
virtual ~JsonOutcomeSerializer() = default;

JsonOutcome Deserialize(HttpResponseOutcome&& httpOutcome,
const Aws::String& serviceName,
const Aws::String& requestName) const
{
if (!httpOutcome.IsSuccess())
{
return TracingUtils::MakeCallWithTiming<JsonOutcome>(
[&]() -> JsonOutcome {
return JsonOutcome{std::move(httpOutcome)};
},
TracingUtils::SMITHY_CLIENT_DESERIALIZATION_METRIC,
*m_telemetryProvider->getMeter(serviceName, {}),
{{TracingUtils::SMITHY_METHOD_DIMENSION, requestName},
{TracingUtils::SMITHY_SERVICE_DIMENSION, serviceName}});
}

if (httpOutcome.GetResult()->GetResponseBody().good() &&
httpOutcome.GetResult()->GetResponseBody().tellp() > 0)
{
JsonValue jsonValue(httpOutcome.GetResult()->GetResponseBody());
if (!jsonValue.WasParseSuccessful()) {
return TracingUtils::MakeCallWithTiming<JsonOutcome>(
[&]() -> JsonOutcome {
return JsonOutcome{AWSError(CoreErrors::UNKNOWN,
"Json Parser Error",
jsonValue.GetErrorMessage(),
false)};
},
TracingUtils::SMITHY_CLIENT_DESERIALIZATION_METRIC,
*m_telemetryProvider->getMeter(serviceName, {}),
{{TracingUtils::SMITHY_METHOD_DIMENSION, requestName},
{TracingUtils::SMITHY_SERVICE_DIMENSION, serviceName}});
}

return TracingUtils::MakeCallWithTiming<JsonOutcome>(
[&]() -> JsonOutcome {
return JsonOutcome{Aws::AmazonWebServiceResult<JsonValue>(std::move(jsonValue),
httpOutcome.GetResult()->GetHeaders(),
httpOutcome.GetResult()->GetResponseCode())};
},
TracingUtils::SMITHY_CLIENT_DESERIALIZATION_METRIC,
*m_telemetryProvider->getMeter(serviceName, {}),
{{TracingUtils::SMITHY_METHOD_DIMENSION, requestName},
{TracingUtils::SMITHY_SERVICE_DIMENSION, serviceName}});
}

return TracingUtils::MakeCallWithTiming<JsonOutcome>(
[&]() -> JsonOutcome {
return JsonOutcome{Aws::AmazonWebServiceResult<JsonValue>(JsonValue(),
httpOutcome.GetResult()->GetHeaders())};
},
TracingUtils::SMITHY_CLIENT_DESERIALIZATION_METRIC,
*m_telemetryProvider->getMeter(serviceName, {}),
{{TracingUtils::SMITHY_METHOD_DIMENSION, requestName},
{TracingUtils::SMITHY_SERVICE_DIMENSION, serviceName}});
}

private:
std::shared_ptr<TelemetryProvider> m_telemetryProvider;
};
} // namespace client
} // namespace smithy
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/**
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0.
*/
#pragma once
#include <aws/core/AmazonWebServiceResult.h>
#include <aws/core/client/AWSError.h>
#include <aws/core/utils/Outcome.h>
#include <aws/core/utils/xml/XmlSerializer.h>
#include <smithy/tracing/TracingUtils.h>
#include <smithy/tracing/TelemetryProvider.h>

namespace smithy
{
namespace client
{
using TracingUtils = components::tracing::TracingUtils;
using CoreErrors = Aws::Client::CoreErrors;
using AWSError = Aws::Client::AWSError<CoreErrors>;
using XmlDocument = Aws::Utils::Xml::XmlDocument;
using HttpResponseOutcome = Aws::Utils::Outcome<std::shared_ptr<Aws::Http::HttpResponse>, AWSError>;
using XmlServiceResult = Aws::AmazonWebServiceResult<XmlDocument>;
using XmlOutcome = Aws::Utils::Outcome<XmlServiceResult, AWSError>;
using TelemetryProvider = components::tracing::TelemetryProvider;

class XmlOutcomeSerializer
{
public:
explicit XmlOutcomeSerializer(const std::shared_ptr<TelemetryProvider>& telemetryProvider)
: m_telemetryProvider(telemetryProvider)
{
}

XmlOutcomeSerializer(const XmlOutcomeSerializer& other) = delete;
XmlOutcomeSerializer(XmlOutcomeSerializer&& other) noexcept = default;
XmlOutcomeSerializer& operator=(const XmlOutcomeSerializer& other) = delete;
XmlOutcomeSerializer& operator=(XmlOutcomeSerializer&& other) noexcept = default;
virtual ~XmlOutcomeSerializer() = default;


XmlOutcome Deserialize(HttpResponseOutcome&& httpOutcome,
const Aws::String& serviceName,
const Aws::String& requestName) const
{
if (!httpOutcome.IsSuccess())
{
return TracingUtils::MakeCallWithTiming<XmlOutcome>(
[&]() -> XmlOutcome {
return {std::move(httpOutcome)};
},
TracingUtils::SMITHY_CLIENT_DESERIALIZATION_METRIC,
*m_telemetryProvider->getMeter(serviceName, {}),
{{TracingUtils::SMITHY_METHOD_DIMENSION, requestName}, {TracingUtils::SMITHY_SERVICE_DIMENSION, serviceName}});
}

if (httpOutcome.GetResult()->GetResponseBody().good() &&
httpOutcome.GetResult()->GetResponseBody().tellp() > 0)
{
return TracingUtils::MakeCallWithTiming<XmlOutcome>(
[&]() -> XmlOutcome {
XmlDocument xmlDoc = XmlDocument::CreateFromXmlStream(httpOutcome.GetResult()->GetResponseBody());

if (!xmlDoc.WasParseSuccessful())
{
AWS_LOGSTREAM_ERROR("XmlOutcomeSerializer", "Xml parsing for error failed with message " << xmlDoc.GetErrorMessage().c_str());
return AWSError(CoreErrors::UNKNOWN,
"Xml Parse Error",
xmlDoc.GetErrorMessage(),
false);
}

return {XmlServiceResult(std::move(xmlDoc),
httpOutcome.GetResult()->GetHeaders(),
httpOutcome.GetResult()->GetResponseCode())};
},
TracingUtils::SMITHY_CLIENT_DESERIALIZATION_METRIC,
*m_telemetryProvider->getMeter(serviceName, {}),
{{TracingUtils::SMITHY_METHOD_DIMENSION, requestName}, {TracingUtils::SMITHY_SERVICE_DIMENSION, serviceName}});
}

return {XmlServiceResult(XmlDocument(), httpOutcome.GetResult()->GetHeaders())};
}
private:
std::shared_ptr<TelemetryProvider> m_telemetryProvider;
};

} // namespace client
} // namespace smithy
Loading

0 comments on commit 732c1fc

Please sign in to comment.