Skip to content

Commit 2f5767c

Browse files
committed
Add CborValue class for AWSRpcV2CborClient. Refactor template AWSProtocolClient with new changes.
1 parent 46019a9 commit 2f5767c

File tree

8 files changed

+110
-64
lines changed

8 files changed

+110
-64
lines changed

src/aws-cpp-sdk-core/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ file(GLOB UTILS_EVENT_HEADERS "include/aws/core/utils/event/*.h")
6262
file(GLOB UTILS_BASE64_HEADERS "include/aws/core/utils/base64/*.h")
6363
file(GLOB UTILS_CRYPTO_HEADERS "include/aws/core/utils/crypto/*.h")
6464
file(GLOB UTILS_JSON_HEADERS "include/aws/core/utils/json/*.h")
65+
file(GLOB UTILS_CBOR_HEADERS "include/aws/core/utils/cbor/*.h")
6566
file(GLOB UTILS_THREADING_HEADERS "include/aws/core/utils/threading/*.h")
6667
file(GLOB UTILS_XML_HEADERS "include/aws/core/utils/xml/*.h")
6768
file(GLOB UTILS_MEMORY_HEADERS "include/aws/core/utils/memory/*.h")
@@ -115,6 +116,7 @@ file(GLOB UTILS_EVENT_SOURCE "${CMAKE_CURRENT_SOURCE_DIR}/source/utils/event/*.c
115116
file(GLOB UTILS_BASE64_SOURCE "${CMAKE_CURRENT_SOURCE_DIR}/source/utils/base64/*.cpp")
116117
file(GLOB UTILS_CRYPTO_SOURCE "${CMAKE_CURRENT_SOURCE_DIR}/source/utils/crypto/*.cpp")
117118
file(GLOB UTILS_JSON_SOURCE "${CMAKE_CURRENT_SOURCE_DIR}/source/utils/json/*.cpp")
119+
file(GLOB UTILS_CBOR_SOURCE "${CMAKE_CURRENT_SOURCE_DIR}/source/utils/cbor/*.cpp")
118120
file(GLOB UTILS_THREADING_SOURCE "${CMAKE_CURRENT_SOURCE_DIR}/source/utils/threading/*.cpp")
119121
file(GLOB UTILS_XML_SOURCE "${CMAKE_CURRENT_SOURCE_DIR}/source/utils/xml/*.cpp")
120122
file(GLOB UTILS_LOGGING_SOURCE "${CMAKE_CURRENT_SOURCE_DIR}/source/utils/logging/*.cpp")
@@ -268,6 +270,7 @@ file(GLOB AWS_NATIVE_SDK_COMMON_HEADERS
268270
${UTILS_BASE64_HEADERS}
269271
${UTILS_CRYPTO_HEADERS}
270272
${UTILS_JSON_HEADERS}
273+
${UTILS_CBOR_HEADERS}
271274
${UTILS_THREADING_HEADERS}
272275
${UTILS_RETRY_HEADERS}
273276
${UTILS_XML_HEADERS}
@@ -406,6 +409,7 @@ if(MSVC)
406409
source_group("Header Files\\aws\\core\\utils\\event" FILES ${UTILS_EVENT_HEADERS})
407410
source_group("Header Files\\aws\\core\\utils\\exceptions" FILES ${UTILS_EXCEPTIONS_HEADERS})
408411
source_group("Header Files\\aws\\core\\utils\\json" FILES ${UTILS_JSON_HEADERS})
412+
source_group("Header Files\\aws\\core\\utils\\cbor" FILES ${UTILS_CBOR_HEADERS})
409413
source_group("Header Files\\aws\\core\\utils\\threading" FILES ${UTILS_THREADING_HEADERS})
410414
source_group("Header Files\\aws\\core\\utils\\xml" FILES ${UTILS_XML_HEADERS})
411415
source_group("Header Files\\aws\\core\\utils\\logging" FILES ${UTILS_LOGGING_HEADERS})
@@ -473,6 +477,7 @@ if(MSVC)
473477
source_group("Source Files\\utils\\event" FILES ${UTILS_EVENT_SOURCE})
474478
source_group("Source Files\\utils\\exceptions" FILES ${UTILS_EXCEPTIONS_SOURCE})
475479
source_group("Source Files\\utils\\json" FILES ${UTILS_JSON_SOURCE})
480+
source_group("Source Files\\utils\\cbor" FILES ${UTILS_CBOR_SOURCE})
476481
source_group("Source Files\\utils\\threading" FILES ${UTILS_THREADING_SOURCE})
477482
source_group("Source Files\\utils\\xml" FILES ${UTILS_XML_SOURCE})
478483
source_group("Source Files\\utils\\stream" FILES ${UTILS_STREAM_SOURCE})
@@ -684,6 +689,7 @@ install (FILES ${UTILS_EVENT_HEADERS} DESTINATION ${INCLUDE_DIRECTORY}/aws/core/
684689
install (FILES ${UTILS_BASE64_HEADERS} DESTINATION ${INCLUDE_DIRECTORY}/aws/core/utils/base64)
685690
install (FILES ${UTILS_CRYPTO_HEADERS} DESTINATION ${INCLUDE_DIRECTORY}/aws/core/utils/crypto)
686691
install (FILES ${UTILS_JSON_HEADERS} DESTINATION ${INCLUDE_DIRECTORY}/aws/core/utils/json)
692+
install (FILES ${UTILS_CBOR_HEADERS} DESTINATION ${INCLUDE_DIRECTORY}/aws/core/utils/cbor)
687693
install (FILES ${UTILS_RETRY_HEADERS} DESTINATION ${INCLUDE_DIRECTORY}/aws/core/utils/retry)
688694
install (FILES ${UTILS_XML_HEADERS} DESTINATION ${INCLUDE_DIRECTORY}/aws/core/utils/xml)
689695
install (FILES ${UTILS_LOGGING_HEADERS} DESTINATION ${INCLUDE_DIRECTORY}/aws/core/utils/logging)

src/aws-cpp-sdk-core/include/aws/core/client/AWSJsonClient.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,8 @@ namespace Aws
5757
friend class BidirectionalEventStreamingTask; // allow BidirectionalEventStreamingTask to MakeRequests
5858

5959
private:
60-
Utils::Json::JsonValue ParseResponse(const HttpResponseOutcome& httpOutcome) const override;
61-
bool HasParseError(const Utils::Json::JsonValue& response) const override;
6260
AWSError<CoreErrors> CreateParseError() const override;
6361
const char* GetClientLogTag() const override;
64-
Utils::Json::JsonValue CreateEmptyResponse() const override;
6562
};
6663
} // namespace Client
6764
} // namespace Aws

src/aws-cpp-sdk-core/include/aws/core/client/AWSProtocolClient.h

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -119,15 +119,15 @@ namespace Aws
119119

120120
HttpResponseOutcome httpOutcome(std::move(httpResponse));
121121
if (httpOutcome.GetResult()->GetResponseBody().tellp() > 0) {
122-
auto response = ParseResponse(httpOutcome);
123-
if (HasParseError(response)) {
122+
auto response = ResponseType(httpOutcome.GetResult()->GetResponseBody());
123+
if (!response.WasParseSuccessful()) {
124124
return OutcomeType(CreateParseError());
125125
}
126126
return OutcomeType(AmazonWebServiceResult<ResponseType>(std::move(response),
127127
httpOutcome.GetResult()->GetHeaders(),
128128
httpOutcome.GetResult()->GetResponseCode()));
129129
}
130-
return OutcomeType(AmazonWebServiceResult<ResponseType>(CreateEmptyResponse(), httpOutcome.GetResult()->GetHeaders()));
130+
return OutcomeType(AmazonWebServiceResult<ResponseType>(ResponseType(), httpOutcome.GetResult()->GetHeaders()));
131131
}
132132

133133
/**
@@ -198,8 +198,8 @@ namespace Aws
198198
if (httpOutcome.GetResult()->GetResponseBody().tellp() > 0) {
199199
return smithy::components::tracing::TracingUtils::MakeCallWithTiming<OutcomeType>(
200200
[&]() -> OutcomeType {
201-
auto response = ParseResponse(httpOutcome);
202-
if (HasParseError(response)) {
201+
auto response = ResponseType(httpOutcome.GetResult()->GetResponseBody());
202+
if (!response.WasParseSuccessful()) {
203203
return OutcomeType(CreateParseError());
204204
}
205205
return OutcomeType(AmazonWebServiceResult<ResponseType>(std::move(response),
@@ -214,7 +214,7 @@ namespace Aws
214214

215215
return smithy::components::tracing::TracingUtils::MakeCallWithTiming<OutcomeType>(
216216
[&]() -> OutcomeType {
217-
return OutcomeType(AmazonWebServiceResult<ResponseType>(CreateEmptyResponse(), httpOutcome.GetResult()->GetHeaders()));
217+
return OutcomeType(AmazonWebServiceResult<ResponseType>(ResponseType(), httpOutcome.GetResult()->GetHeaders()));
218218
},
219219
smithy::components::tracing::TracingUtils::SMITHY_CLIENT_DESERIALIZATION_METRIC,
220220
*m_telemetryProvider->getMeter(this->GetServiceClientName(), {}),
@@ -223,10 +223,7 @@ namespace Aws
223223
}
224224

225225
// Pure virtual functions for protocol-specific behavior
226-
virtual ResponseType ParseResponse(const HttpResponseOutcome& httpOutcome) const = 0;
227-
virtual bool HasParseError(const ResponseType& response) const = 0;
228226
virtual AWSError<CoreErrors> CreateParseError() const = 0;
229-
virtual ResponseType CreateEmptyResponse() const = 0;
230227
virtual const char* GetClientLogTag() const { return "AWSProtocolClient"; };
231228
};
232229
}

src/aws-cpp-sdk-core/include/aws/core/client/AWSRpcV2CborClient.h

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,21 @@
1010
#include <aws/core/Core_EXPORTS.h>
1111
#include <aws/core/client/AWSProtocolClient.h>
1212
#include <aws/crt/cbor/Cbor.h>
13+
#include <aws/core/utils/cbor/CborValue.h>
1314

1415
namespace Aws
1516
{
17+
namespace Utils
18+
{
19+
namespace Cbor
20+
{
21+
class CborValue;
22+
} // namespace Cbor
23+
} // namespace Utils
24+
1625
namespace Client
1726
{
18-
using RpcV2CborOutcome = Utils::Outcome<AmazonWebServiceResult<Aws::UniquePtr<Crt::Cbor::CborDecoder>>, AWSError<CoreErrors>>;
27+
using RpcV2CborOutcome = Utils::Outcome<AmazonWebServiceResult<Utils::Cbor::CborValue>, AWSError<CoreErrors>>;
1928

2029
template <typename OutcomeT, typename ClientT, typename AWSEndpointT, typename RequestT, typename HandlerT>
2130
class BidirectionalEventStreamingTask;
@@ -24,10 +33,10 @@ namespace Aws
2433
* AWSClient that handles marshalling cbor response bodies. You would inherit from this class
2534
* to create a client that uses Cbor as its payload format.
2635
*/
27-
class AWS_CORE_API AWSRpcV2CborClient : public AWSProtocolClient<RpcV2CborOutcome, Aws::UniquePtr<Crt::Cbor::CborDecoder>>
36+
class AWS_CORE_API AWSRpcV2CborClient : public AWSProtocolClient<RpcV2CborOutcome, Utils::Cbor::CborValue>
2837
{
2938
public:
30-
typedef AWSProtocolClient<RpcV2CborOutcome, Aws::UniquePtr<Crt::Cbor::CborDecoder>> BASECLASS;
39+
typedef AWSProtocolClient<RpcV2CborOutcome, Utils::Cbor::CborValue> BASECLASS;
3140

3241
/**
3342
* Simply calls AWSClient constructor.
@@ -50,12 +59,8 @@ namespace Aws
5059
friend class BidirectionalEventStreamingTask;
5160

5261
private:
53-
Aws::UniquePtr<Crt::Cbor::CborDecoder> ParseResponse(const HttpResponseOutcome& httpOutcome) const override;
54-
bool HasParseError(const Aws::UniquePtr<Crt::Cbor::CborDecoder>& response) const override;
5562
AWSError<CoreErrors> CreateParseError() const override;
56-
Aws::UniquePtr<Crt::Cbor::CborDecoder> CreateEmptyResponse() const override;
5763
const char* GetClientLogTag() const override;
58-
Aws::UniquePtr<Aws::Crt::Cbor::CborDecoder> CreateCborDecoder(const HttpResponseOutcome& response) const;
5964
};
6065
} // namespace Client
6166
} // namespace Aws
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/**
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0.
4+
*/
5+
6+
#pragma once
7+
8+
#include <aws/core/Core_EXPORTS.h>
9+
#include <aws/core/http/HttpResponse.h>
10+
#include <aws/core/utils/Outcome.h>
11+
#include <aws/crt/cbor/Cbor.h>
12+
#include <aws/core/client/AWSClient.h>
13+
14+
namespace Aws
15+
{
16+
namespace Utils
17+
{
18+
namespace Cbor {
19+
class AWS_CORE_API CborValue
20+
{
21+
public:
22+
/**
23+
* Constructs empty CborValue.
24+
*/
25+
CborValue();
26+
27+
/**
28+
* Constructs a CborValue by parsing the httpResponseOutCome's body.
29+
*/
30+
CborValue(Aws::IStream& istream);
31+
32+
/**
33+
* Moves the ownership of the CborValue.
34+
* No copying is performed.
35+
*/
36+
CborValue(CborValue&& value);
37+
38+
~CborValue();
39+
40+
Aws::UniquePtr<Aws::Crt::Cbor::CborDecoder> GetDecoder() const { return std::move(m_decoder); };
41+
42+
inline bool WasParseSuccessful() const { return m_decoder != nullptr; };
43+
44+
private:
45+
mutable Aws::UniquePtr<Crt::Cbor::CborDecoder> m_decoder;
46+
Aws::String m_body;
47+
};
48+
} //Cbor
49+
} // Utils
50+
} // Aws

src/aws-cpp-sdk-core/source/client/AWSJsonClient.cpp

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -38,26 +38,11 @@ AWSJsonClient::AWSJsonClient(const Aws::Client::ClientConfiguration& configurati
3838
{
3939
}
4040

41-
JsonValue AWSJsonClient::ParseResponse(const HttpResponseOutcome& httpOutcome) const
42-
{
43-
return JsonValue(httpOutcome.GetResult()->GetResponseBody());
44-
}
45-
46-
bool AWSJsonClient::HasParseError(const Aws::Utils::Json::JsonValue& response) const
47-
{
48-
return !response.WasParseSuccessful();
49-
}
50-
5141
AWSError<CoreErrors> AWSJsonClient::CreateParseError() const
5242
{
5343
return AWSError<CoreErrors>(CoreErrors::UNKNOWN, "Json Parser Error", "Failed to parse JSON response", false);
5444
}
5545

56-
JsonValue AWSJsonClient::CreateEmptyResponse() const
57-
{
58-
return JsonValue();
59-
}
60-
6146
const char* AWSJsonClient::GetClientLogTag() const
6247
{
6348
return AWS_JSON_CLIENT_LOG_TAG;

src/aws-cpp-sdk-core/source/client/AWSRpcV2CborClient.cpp

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,14 @@
99
#include <aws/core/client/ClientConfiguration.h>
1010
#include <aws/core/client/CoreErrors.h>
1111
#include <aws/core/client/RetryStrategy.h>
12-
#include <aws/core/http/HttpResponse.h>
13-
#include <aws/core/utils/Outcome.h>
14-
#include <aws/core/utils/logging/LogMacros.h>
15-
#include <aws/core/utils/memory/stl/AWSStringStream.h>
16-
#include <aws/crt/cbor/Cbor.h>
12+
#include <aws/core/utils/cbor/CborValue.h>
1713

1814
#include <cassert>
1915

20-
2116
using namespace Aws;
2217
using namespace Aws::Client;
2318
using namespace Aws::Http;
24-
using namespace Aws::Utils;
19+
using namespace Aws::Utils::Cbor;
2520
using namespace smithy::components::tracing;
2621

2722
static const char AWS_CBOR_CLIENT_LOG_TAG[] = "AWSRpcV2CborClient";
@@ -40,36 +35,13 @@ AWSRpcV2CborClient::AWSRpcV2CborClient(const Aws::Client::ClientConfiguration& c
4035
{
4136
}
4237

43-
Aws::UniquePtr<Crt::Cbor::CborDecoder> AWSRpcV2CborClient::ParseResponse(const HttpResponseOutcome& httpOutcome) const
44-
{
45-
return CreateCborDecoder(httpOutcome);
46-
}
47-
48-
bool AWSRpcV2CborClient::HasParseError(const Aws::UniquePtr<Crt::Cbor::CborDecoder>& response) const
49-
{
50-
return response == nullptr;
51-
}
52-
5338
AWSError<CoreErrors> AWSRpcV2CborClient::CreateParseError() const
5439
{
5540
return AWSError<CoreErrors>(CoreErrors::UNKNOWN, "Cbor Parser Error", "Failed to parse CBOR response", false);
5641
}
5742

58-
Aws::UniquePtr<Crt::Cbor::CborDecoder> AWSRpcV2CborClient::CreateEmptyResponse() const
59-
{
60-
return nullptr;
61-
}
62-
6343
const char* AWSRpcV2CborClient::GetClientLogTag() const
6444
{
6545
return AWS_CBOR_CLIENT_LOG_TAG;
6646
}
6747

68-
Aws::UniquePtr<Aws::Crt::Cbor::CborDecoder> AWSRpcV2CborClient::CreateCborDecoder(
69-
const HttpResponseOutcome& httpOutcome) const
70-
{
71-
Aws::StringStream ss;
72-
ss << httpOutcome.GetResult()->GetResponseBody().rdbuf();
73-
const Aws::Crt::ByteCursor cborDecoder = Aws::Crt::ByteCursorFromCString(ss.str().c_str());
74-
return Aws::MakeUnique<Crt::Cbor::CborDecoder>(GetClientLogTag(), cborDecoder);
75-
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0.
4+
*/
5+
6+
#include <aws/core/utils/cbor/CborValue.h>
7+
8+
using namespace Aws::Utils::Cbor;
9+
using namespace Aws::Crt::Cbor;
10+
11+
CborValue::CborValue() : m_decoder(nullptr)
12+
{
13+
}
14+
15+
CborValue::CborValue(Aws::IStream& istream) : m_decoder(nullptr)
16+
{
17+
Aws::StringStream ss;
18+
ss << istream.rdbuf();
19+
m_body = ss.str();
20+
auto cursor = Aws::Crt::ByteCursorFromArray(reinterpret_cast<const uint8_t*>(m_body.c_str()), m_body.length());
21+
m_decoder = Aws::MakeUnique<CborDecoder>("CborValue", cursor);
22+
23+
}
24+
25+
CborValue::CborValue(CborValue&& value) :
26+
m_body(value.m_body)
27+
{
28+
auto cursor = Aws::Crt::ByteCursorFromArray(reinterpret_cast<const uint8_t*>(m_body.c_str()), m_body.length());
29+
m_decoder = Aws::MakeUnique<CborDecoder>("CborValue", cursor);
30+
}
31+
32+
CborValue::~CborValue()
33+
{
34+
}

0 commit comments

Comments
 (0)