ServiceJobUtil.h 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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 <AzCore/std/string/tokenize.h>
  10. #include <Framework/ServiceJob.h>
  11. namespace AWSCore
  12. {
  13. inline void ConfigureJsonServiceRequest(HttpRequestJob& request, AZStd::string jsonBody)
  14. {
  15. size_t len = jsonBody.length();
  16. if (len > 0)
  17. {
  18. AZStd::string lenStr = AZStd::string::format("%zu", len);
  19. request.SetContentLength(lenStr);
  20. request.SetContentType("application/json");
  21. request.SetBody(std::move(jsonBody));
  22. }
  23. request.SetAccept("application/json");
  24. request.SetAcceptCharSet("utf-8");
  25. }
  26. inline Aws::String DetermineRegionFromServiceUrl(const Aws::String& serviceUrl)
  27. {
  28. // Assumes that API Gateway URLs have either of the following two forms:
  29. // https://{custom_domain_name}/{region}.{stage}.{rest-api-id}/{path}
  30. // https://{rest-api-id}.execute-api.{region}.amazonaws.com/{stage}/{path}
  31. const int ExpectedUrlSections = 3;
  32. AZStd::vector<AZStd::string> urlSections;
  33. AZStd::string url(serviceUrl.c_str());
  34. AZStd::tokenize(url, AZStd::string("/"), urlSections);
  35. if (urlSections.size() > ExpectedUrlSections)
  36. {
  37. int i = static_cast<int>(urlSections[ExpectedUrlSections - 1].find('.'));
  38. if (i != -1)
  39. {
  40. // Handle APIGateway URLs with custom domains:
  41. // https://{custom_domain_name}/{region}.{stage}.{rest-api-id}/{path}
  42. return urlSections[ExpectedUrlSections - 1].substr(0, i).c_str();
  43. }
  44. else
  45. {
  46. // API Gateway URLs have the form:
  47. // https://{rest-api-id}.execute-api.{region}.amazonaws.com/{stage}/{path}
  48. const int RegionIndex = 2;
  49. AZStd::vector<AZStd::string> domainSections;
  50. AZStd::tokenize(urlSections[ExpectedUrlSections - 2], AZStd::string("."), domainSections);
  51. return domainSections[RegionIndex].c_str();
  52. }
  53. }
  54. return "";
  55. }
  56. } // namespace AWSCore