StsClient.cc 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. * Copyright 2009-2017 Alibaba Cloud All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <alibabacloud/sts/StsClient.h>
  17. #include <alibabacloud/core/SimpleCredentialsProvider.h>
  18. using namespace AlibabaCloud;
  19. using namespace AlibabaCloud::Location;
  20. using namespace AlibabaCloud::Sts;
  21. using namespace AlibabaCloud::Sts::Model;
  22. namespace
  23. {
  24. const std::string SERVICE_NAME = "Sts";
  25. }
  26. StsClient::StsClient(const Credentials &credentials, const ClientConfiguration &configuration) :
  27. RpcServiceClient(SERVICE_NAME, std::make_shared<SimpleCredentialsProvider>(credentials), configuration)
  28. {
  29. auto locationClient = std::make_shared<LocationClient>(credentials, configuration);
  30. endpointProvider_ = std::make_shared<EndpointProvider>(locationClient, configuration.regionId(), SERVICE_NAME, "");
  31. }
  32. StsClient::StsClient(const std::shared_ptr<CredentialsProvider>& credentialsProvider, const ClientConfiguration & configuration) :
  33. RpcServiceClient(SERVICE_NAME, credentialsProvider, configuration)
  34. {
  35. auto locationClient = std::make_shared<LocationClient>(credentialsProvider, configuration);
  36. endpointProvider_ = std::make_shared<EndpointProvider>(locationClient, configuration.regionId(), SERVICE_NAME, "");
  37. }
  38. StsClient::StsClient(const std::string & accessKeyId, const std::string & accessKeySecret, const ClientConfiguration & configuration) :
  39. RpcServiceClient(SERVICE_NAME, std::make_shared<SimpleCredentialsProvider>(accessKeyId, accessKeySecret), configuration)
  40. {
  41. auto locationClient = std::make_shared<LocationClient>(accessKeyId, accessKeySecret, configuration);
  42. endpointProvider_ = std::make_shared<EndpointProvider>(locationClient, configuration.regionId(), SERVICE_NAME, "");
  43. }
  44. StsClient::~StsClient()
  45. {}
  46. StsClient::AssumeRoleOutcome StsClient::assumeRole(const AssumeRoleRequest &request) const
  47. {
  48. auto endpointOutcome = endpointProvider_->getEndpoint();
  49. if (!endpointOutcome.isSuccess())
  50. return AssumeRoleOutcome(endpointOutcome.error());
  51. auto outcome = makeRequest(endpointOutcome.result(), request);
  52. if (outcome.isSuccess())
  53. return AssumeRoleOutcome(AssumeRoleResult(outcome.result()));
  54. else
  55. return AssumeRoleOutcome(outcome.error());
  56. }
  57. void StsClient::assumeRoleAsync(const AssumeRoleRequest& request, const AssumeRoleAsyncHandler& handler, const std::shared_ptr<const AsyncCallerContext>& context) const
  58. {
  59. auto fn = [this, request, handler, context]()
  60. {
  61. handler(this, request, assumeRole(request), context);
  62. };
  63. asyncExecute(new Runnable(fn));
  64. }
  65. StsClient::AssumeRoleOutcomeCallable StsClient::assumeRoleCallable(const AssumeRoleRequest &request) const
  66. {
  67. auto task = std::make_shared<std::packaged_task<AssumeRoleOutcome()>>(
  68. [this, request]()
  69. {
  70. return this->assumeRole(request);
  71. });
  72. asyncExecute(new Runnable([task]() { (*task)(); }));
  73. return task->get_future();
  74. }
  75. StsClient::AssumeRoleWithOIDCOutcome StsClient::assumeRoleWithOIDC(const AssumeRoleWithOIDCRequest &request) const
  76. {
  77. auto endpointOutcome = endpointProvider_->getEndpoint();
  78. if (!endpointOutcome.isSuccess())
  79. return AssumeRoleWithOIDCOutcome(endpointOutcome.error());
  80. auto outcome = makeRequest(endpointOutcome.result(), request);
  81. if (outcome.isSuccess())
  82. return AssumeRoleWithOIDCOutcome(AssumeRoleWithOIDCResult(outcome.result()));
  83. else
  84. return AssumeRoleWithOIDCOutcome(outcome.error());
  85. }
  86. void StsClient::assumeRoleWithOIDCAsync(const AssumeRoleWithOIDCRequest& request, const AssumeRoleWithOIDCAsyncHandler& handler, const std::shared_ptr<const AsyncCallerContext>& context) const
  87. {
  88. auto fn = [this, request, handler, context]()
  89. {
  90. handler(this, request, assumeRoleWithOIDC(request), context);
  91. };
  92. asyncExecute(new Runnable(fn));
  93. }
  94. StsClient::AssumeRoleWithOIDCOutcomeCallable StsClient::assumeRoleWithOIDCCallable(const AssumeRoleWithOIDCRequest &request) const
  95. {
  96. auto task = std::make_shared<std::packaged_task<AssumeRoleWithOIDCOutcome()>>(
  97. [this, request]()
  98. {
  99. return this->assumeRoleWithOIDC(request);
  100. });
  101. asyncExecute(new Runnable([task]() { (*task)(); }));
  102. return task->get_future();
  103. }
  104. StsClient::AssumeRoleWithSAMLOutcome StsClient::assumeRoleWithSAML(const AssumeRoleWithSAMLRequest &request) const
  105. {
  106. auto endpointOutcome = endpointProvider_->getEndpoint();
  107. if (!endpointOutcome.isSuccess())
  108. return AssumeRoleWithSAMLOutcome(endpointOutcome.error());
  109. auto outcome = makeRequest(endpointOutcome.result(), request);
  110. if (outcome.isSuccess())
  111. return AssumeRoleWithSAMLOutcome(AssumeRoleWithSAMLResult(outcome.result()));
  112. else
  113. return AssumeRoleWithSAMLOutcome(outcome.error());
  114. }
  115. void StsClient::assumeRoleWithSAMLAsync(const AssumeRoleWithSAMLRequest& request, const AssumeRoleWithSAMLAsyncHandler& handler, const std::shared_ptr<const AsyncCallerContext>& context) const
  116. {
  117. auto fn = [this, request, handler, context]()
  118. {
  119. handler(this, request, assumeRoleWithSAML(request), context);
  120. };
  121. asyncExecute(new Runnable(fn));
  122. }
  123. StsClient::AssumeRoleWithSAMLOutcomeCallable StsClient::assumeRoleWithSAMLCallable(const AssumeRoleWithSAMLRequest &request) const
  124. {
  125. auto task = std::make_shared<std::packaged_task<AssumeRoleWithSAMLOutcome()>>(
  126. [this, request]()
  127. {
  128. return this->assumeRoleWithSAML(request);
  129. });
  130. asyncExecute(new Runnable([task]() { (*task)(); }));
  131. return task->get_future();
  132. }
  133. StsClient::GetCallerIdentityOutcome StsClient::getCallerIdentity(const GetCallerIdentityRequest &request) const
  134. {
  135. auto endpointOutcome = endpointProvider_->getEndpoint();
  136. if (!endpointOutcome.isSuccess())
  137. return GetCallerIdentityOutcome(endpointOutcome.error());
  138. auto outcome = makeRequest(endpointOutcome.result(), request);
  139. if (outcome.isSuccess())
  140. return GetCallerIdentityOutcome(GetCallerIdentityResult(outcome.result()));
  141. else
  142. return GetCallerIdentityOutcome(outcome.error());
  143. }
  144. void StsClient::getCallerIdentityAsync(const GetCallerIdentityRequest& request, const GetCallerIdentityAsyncHandler& handler, const std::shared_ptr<const AsyncCallerContext>& context) const
  145. {
  146. auto fn = [this, request, handler, context]()
  147. {
  148. handler(this, request, getCallerIdentity(request), context);
  149. };
  150. asyncExecute(new Runnable(fn));
  151. }
  152. StsClient::GetCallerIdentityOutcomeCallable StsClient::getCallerIdentityCallable(const GetCallerIdentityRequest &request) const
  153. {
  154. auto task = std::make_shared<std::packaged_task<GetCallerIdentityOutcome()>>(
  155. [this, request]()
  156. {
  157. return this->getCallerIdentity(request);
  158. });
  159. asyncExecute(new Runnable([task]() { (*task)(); }));
  160. return task->get_future();
  161. }