ServiceClientJobConfig.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #pragma once
  9. #include <Framework/ServiceJobConfig.h>
  10. #include <ResourceMapping/AWSResourceMappingBus.h>
  11. namespace AWSCore
  12. {
  13. /// Provides configuration needed by service jobs.
  14. class IServiceClientJobConfig
  15. : public virtual IServiceJobConfig
  16. {
  17. public:
  18. virtual AZStd::string GetServiceUrl() = 0;
  19. };
  20. /// Encapsulates what code needs to know about a service in order to
  21. /// use it with a service job. Use the DEFINE_SERVICE_TRAITS macro
  22. /// to simplify the definition of these types.
  23. ///
  24. /// \param ServiceTraitsType - must implement the following static
  25. /// functions:
  26. ///
  27. /// const char* GetServiceName();
  28. /// const char* GetRESTApiIdKeyName();
  29. /// const char* GetRESTApiStageKeyName();
  30. ///
  31. template<class ServiceTraitsType>
  32. class ServiceTraits
  33. {
  34. public:
  35. static const char* ServiceName;
  36. static const char* RESTApiIdKeyName;
  37. static const char* RESTApiStageKeyName;
  38. };
  39. template<class ServiceTraitsType>
  40. const char* ServiceTraits<ServiceTraitsType>::ServiceName = ServiceTraitsType::GetServiceName();
  41. template<class ServiceTraitsType>
  42. const char* ServiceTraits<ServiceTraitsType>::RESTApiIdKeyName = ServiceTraitsType::GetRESTApiIdKeyName();
  43. template<class ServiceTraitsType>
  44. const char* ServiceTraits<ServiceTraitsType>::RESTApiStageKeyName = ServiceTraitsType::GetRESTApiStageKeyName();
  45. #define AWS_SERVICE_TRAITS_TEMPLATE(SERVICE_NAME, RESTAPI_ID, RESTAPI_STAGE) \
  46. class SERVICE_NAME##ServiceTraits : public AWSCore::ServiceTraits<SERVICE_NAME##ServiceTraits> \
  47. { \
  48. private: \
  49. friend class AWSCore::ServiceTraits<SERVICE_NAME##ServiceTraits>; \
  50. static const char* GetServiceName() { return #SERVICE_NAME; } \
  51. static const char* GetRESTApiIdKeyName() { return RESTAPI_ID; } \
  52. static const char* GetRESTApiStageKeyName() { return RESTAPI_STAGE; } \
  53. };
  54. // warning C4250: 'AWSCore::ServiceClientJobConfig<ServiceTraitsType>' : inherits 'AWSCore::AwsApiJobConfig::AWSCore::AwsApiJobConfig::GetJobContext' via dominance
  55. // Thanks to http://stackoverflow.com/questions/11965596/diamond-inheritance-scenario-compiles-fine-in-g-but-produces-warnings-errors for the explanation
  56. // This is the expected and desired behavior. The warning is superfluous.
  57. AZ_PUSH_DISABLE_WARNING(4250, "-Wunknown-warning-option")
  58. /// Provides service job configuration using settings properties.
  59. template<class ServiceTraitsType>
  60. class ServiceClientJobConfig
  61. : public ServiceJobConfig
  62. , public virtual IServiceClientJobConfig
  63. {
  64. public:
  65. AZ_CLASS_ALLOCATOR(ServiceClientJobConfig, AZ::SystemAllocator);
  66. using ServiceClientJobConfigType = ServiceClientJobConfig<ServiceTraitsType>;
  67. using InitializerFunction = AZStd::function<void(ServiceClientJobConfig& config)>;
  68. /// Initialize an ServiceClientJobConfig object.
  69. ///
  70. /// \param defaultConfig - the config object that provides values when
  71. /// no override has been set in this object. The default is nullptr, which
  72. /// will cause a default value to be used.
  73. ///
  74. /// \param initializer - a function called to initialize this object.
  75. /// This simplifies the initialization of static instances. The default
  76. /// value is nullptr, in which case no initializer will be called.
  77. ServiceClientJobConfig(AwsApiJobConfig* defaultConfig = nullptr, InitializerFunction initializer = nullptr)
  78. : ServiceJobConfig{ defaultConfig }
  79. {
  80. if (initializer)
  81. {
  82. initializer(*this);
  83. }
  84. }
  85. /// This implementation assumes the caller will cache this value as
  86. /// needed. See it's use in ServiceRequestJobConfig.
  87. AZStd::string GetServiceUrl() override
  88. {
  89. if (endpointOverride.has_value())
  90. {
  91. return endpointOverride.value().c_str();
  92. }
  93. AZStd::string serviceUrl;
  94. if (!ServiceTraitsType::RESTApiIdKeyName && !ServiceTraitsType::RESTApiStageKeyName)
  95. {
  96. AWSResourceMappingRequestBus::BroadcastResult(
  97. serviceUrl, &AWSResourceMappingRequests::GetServiceUrlByServiceName,
  98. ServiceTraitsType::ServiceName);
  99. }
  100. else
  101. {
  102. AWSResourceMappingRequestBus::BroadcastResult(
  103. serviceUrl, &AWSResourceMappingRequests::GetServiceUrlByRESTApiIdAndStage,
  104. ServiceTraitsType::RESTApiIdKeyName, ServiceTraitsType::RESTApiStageKeyName);
  105. }
  106. return serviceUrl;
  107. }
  108. };
  109. AZ_POP_DISABLE_WARNING
  110. } // namespace AWSCore