/** * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: Apache-2.0. */ #pragma once #include #include #include #include #include #include namespace smithy { namespace client { using TracingUtils = components::tracing::TracingUtils; using CoreErrors = Aws::Client::CoreErrors; using AWSError = Aws::Client::AWSError; using JsonValue = Aws::Utils::Json::JsonValue; using HttpResponseOutcome = Aws::Utils::Outcome, AWSError>; using JsonOutcome = Aws::Utils::Outcome, AWSError>; using TelemetryProvider = components::tracing::TelemetryProvider; class JsonOutcomeSerializer { public: explicit JsonOutcomeSerializer(const std::shared_ptr& 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 { 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 { 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 { return JsonOutcome{Aws::AmazonWebServiceResult(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 { return JsonOutcome{Aws::AmazonWebServiceResult(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 m_telemetryProvider; }; } // namespace client } // namespace smithy