3
0

RequestBuilder.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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/JsonWriter.h>
  10. // The AWS Native SDK AWSAllocator triggers a warning due to accessing members of std::allocator directly.
  11. // AWSAllocator.h(70): warning C4996: 'std::allocator<T>::pointer': warning STL4010: Various members of std::allocator are deprecated in C++17.
  12. // Use std::allocator_traits instead of accessing these members directly.
  13. // You can define _SILENCE_CXX17_OLD_ALLOCATOR_MEMBERS_DEPRECATION_WARNING or _SILENCE_ALL_CXX17_DEPRECATION_WARNINGS to acknowledge that you have received this warning.
  14. #include <AzCore/PlatformDef.h>
  15. AZ_PUSH_DISABLE_WARNING(4251 4996, "-Wunknown-warning-option")
  16. #include <aws/core/utils/memory/stl/AWSString.h>
  17. #include <aws/core/utils/memory/stl/AWSStringStream.h>
  18. #include <aws/core/http/HttpTypes.h>
  19. AZ_POP_DISABLE_WARNING
  20. namespace Aws
  21. {
  22. namespace Client
  23. {
  24. class AWSAuthSigner;
  25. }
  26. }
  27. namespace AWSCore
  28. {
  29. /// Provides methods for replacing url path parameters, appending
  30. /// url query string parameters, and writing body content.
  31. class RequestBuilder
  32. {
  33. public:
  34. RequestBuilder();
  35. /// Converts the provided object to JSON and sends it as the
  36. /// body of the request. The object can implement the following
  37. /// function to enable serialization:
  38. ///
  39. /// bool WriteJson(ServiceApi::JsonWriter& writer) const
  40. /// {
  41. /// bool ok = true;
  42. /// ok = ok && writer.StartObject();
  43. /// ...
  44. /// ok = ok && writer.EndObject();
  45. /// return ok;
  46. /// }
  47. ///
  48. /// Alternatively you can provide a global function with the
  49. /// following signature:
  50. ///
  51. /// namespace AWSCore {
  52. /// template<>
  53. /// bool GlobalWriteJson<MyObjectType>(ServiceApi::JsonWriter& writer, const MyObjectType& source)
  54. /// {
  55. /// bool ok = true;
  56. /// ...
  57. /// return ok;
  58. /// }
  59. /// }
  60. ///
  61. template<class ObjectType>
  62. bool WriteJsonBodyParameter(const ObjectType& bodyObject)
  63. {
  64. m_bodyContent = std::make_shared<Aws::StringStream>();
  65. JsonOutputStream stream{ *m_bodyContent.get() };
  66. return JsonWriter::WriteObject(stream, bodyObject);
  67. }
  68. bool WriteJsonBodyRaw(const AZStd::string& body)
  69. {
  70. m_bodyContent = std::make_shared<Aws::StringStream>();
  71. m_bodyContent->str(body.c_str());
  72. return true;
  73. }
  74. const Aws::String& GetRequestUrl()
  75. {
  76. return m_requestUrl;
  77. }
  78. void SetRequestUrl(const Aws::String& requestUrl)
  79. {
  80. m_requestUrl = requestUrl;
  81. }
  82. /// Replaces a key with an escaped value. Key should be
  83. /// "{foo}" to replace the "{foo}" part of "/bar/{foo}".
  84. bool SetPathParameter(const char* key, const AZStd::string& value);
  85. bool SetPathParameter(const char* key, const char* value);
  86. bool SetPathParameter(const char* key, double value);
  87. bool SetPathParameter(const char* key, bool value);
  88. bool SetPathParameter(const char* key, int value);
  89. bool SetPathParameter(const char* key, int64_t value);
  90. bool SetPathParameter(const char* key, unsigned value);
  91. bool SetPathParameter(const char* key, uint64_t value);
  92. /// Appends a query parameter to the url. An "?" or "&" is added
  93. /// as needed. The value is escaped.
  94. bool AddQueryParameter(const char* name, const AZStd::string& value);
  95. bool AddQueryParameter(const char* name, const char* value);
  96. bool AddQueryParameter(const char* name, double value);
  97. bool AddQueryParameter(const char* name, bool value);
  98. bool AddQueryParameter(const char* name, int value);
  99. bool AddQueryParameter(const char* name, int64_t value);
  100. bool AddQueryParameter(const char* name, unsigned value);
  101. bool AddQueryParameter(const char* name, uint64_t value);
  102. Aws::Http::HttpMethod GetHttpMethod()
  103. {
  104. return m_httpMethod;
  105. }
  106. void SetHttpMethod(Aws::Http::HttpMethod httpMethod)
  107. {
  108. m_httpMethod = httpMethod;
  109. }
  110. const AZStd::string& GetErrorMessage()
  111. {
  112. return m_errorMessage;
  113. }
  114. void SetErrorMessage(const AZStd::string& message)
  115. {
  116. m_errorMessage = message;
  117. }
  118. std::shared_ptr<Aws::StringStream> GetBodyContent()
  119. {
  120. return m_bodyContent;
  121. }
  122. void SetAWSAuthSigner(std::shared_ptr<Aws::Client::AWSAuthSigner> awsAuthSigner)
  123. {
  124. m_AWSAuthSigner = awsAuthSigner;
  125. }
  126. std::shared_ptr<Aws::Client::AWSAuthSigner> GetAWSAuthSigner()
  127. {
  128. return m_AWSAuthSigner;
  129. }
  130. private:
  131. bool SetPathParameterUnescaped(const char* key, const char* value);
  132. bool AddQueryParameterUnescaped(const char* name, const char* value);
  133. /// copy and assignment not supported.
  134. RequestBuilder(const RequestBuilder&) = delete;
  135. RequestBuilder& operator=(const RequestBuilder&) = delete;
  136. /// HTTP method for the request.
  137. Aws::Http::HttpMethod m_httpMethod;
  138. /// The url being modified.
  139. Aws::String m_requestUrl;
  140. /// Description of error should one occur.
  141. AZStd::string m_errorMessage;
  142. /// JSON format body content.
  143. std::shared_ptr<Aws::StringStream> m_bodyContent;
  144. /// Appends characters from value to target, escaping special characters.
  145. static Aws::String escape(const char* value);
  146. //? AWS signer to use for request signing
  147. std::shared_ptr<Aws::Client::AWSAuthSigner> m_AWSAuthSigner{ nullptr };
  148. };
  149. } // namespace AWSCore