TeslaDamClient.cc 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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/tesladam/TeslaDamClient.h>
  17. #include <alibabacloud/core/SimpleCredentialsProvider.h>
  18. using namespace AlibabaCloud;
  19. using namespace AlibabaCloud::Location;
  20. using namespace AlibabaCloud::TeslaDam;
  21. using namespace AlibabaCloud::TeslaDam::Model;
  22. TeslaDamClient::TeslaDamClient(const Credentials &credentials, const ClientConfiguration &configuration) :
  23. RpcServiceClient(std::make_shared<SimpleCredentialsProvider>(credentials), configuration)
  24. {
  25. auto locationClient = std::make_shared<LocationClient>(credentials, configuration);
  26. endpointProvider_ = std::make_shared<EndpointProvider>(locationClient, configuration.regionId(), "");
  27. }
  28. TeslaDamClient::TeslaDamClient(const std::shared_ptr<CredentialsProvider>& credentialsProvider, const ClientConfiguration & configuration) :
  29. RpcServiceClient(credentialsProvider, configuration)
  30. {
  31. auto locationClient = std::make_shared<LocationClient>(credentialsProvider, configuration);
  32. endpointProvider_ = std::make_shared<EndpointProvider>(locationClient, configuration.regionId(), "");
  33. }
  34. TeslaDamClient::TeslaDamClient(const std::string & accessKeyId, const std::string & accessKeySecret, const ClientConfiguration & configuration) :
  35. RpcServiceClient(std::make_shared<SimpleCredentialsProvider>(accessKeyId, accessKeySecret), configuration)
  36. {
  37. auto locationClient = std::make_shared<LocationClient>(accessKeyId, accessKeySecret, configuration);
  38. endpointProvider_ = std::make_shared<EndpointProvider>(locationClient, configuration.regionId(), "");
  39. }
  40. TeslaDamClient::~TeslaDamClient()
  41. {}
  42. CoreClient::EndpointOutcome TeslaDamClient::endpoint()const
  43. {
  44. if(!configuration().endpoint().empty())
  45. return CoreClient::EndpointOutcome(configuration().endpoint());
  46. auto endpoint = endpointProvider_->getEndpoint();
  47. if (endpoint.empty())
  48. return CoreClient::EndpointOutcome(Error("InvalidEndpoint",""));
  49. else
  50. return CoreClient::EndpointOutcome(endpoint);
  51. }
  52. TeslaDamClient::ActionOutcome TeslaDamClient::action(const ActionRequest &request) const
  53. {
  54. auto endpointOutcome = endpoint();
  55. if (!endpointOutcome.isSuccess())
  56. return ActionOutcome(endpointOutcome.error());
  57. auto outcome = makeRequest(endpointOutcome.result(), request);
  58. if (outcome.isSuccess())
  59. return ActionOutcome(ActionResult(outcome.result()));
  60. else
  61. return ActionOutcome(outcome.error());
  62. }
  63. void TeslaDamClient::actionAsync(const ActionRequest& request, const ActionAsyncHandler& handler, const std::shared_ptr<const AsyncCallerContext>& context) const
  64. {
  65. auto fn = [this, request, handler, context]()
  66. {
  67. handler(this, request, action(request), context);
  68. };
  69. asyncExecute(new Runnable(fn));
  70. }
  71. TeslaDamClient::ActionOutcomeCallable TeslaDamClient::actionCallable(const ActionRequest &request) const
  72. {
  73. auto task = std::make_shared<std::packaged_task<ActionOutcome()>>(
  74. [this, request]()
  75. {
  76. return this->action(request);
  77. });
  78. asyncExecute(new Runnable([task]() { (*task)(); }));
  79. return task->get_future();
  80. }
  81. TeslaDamClient::ActionDiskRmaOutcome TeslaDamClient::actionDiskRma(const ActionDiskRmaRequest &request) const
  82. {
  83. auto endpointOutcome = endpoint();
  84. if (!endpointOutcome.isSuccess())
  85. return ActionDiskRmaOutcome(endpointOutcome.error());
  86. auto outcome = makeRequest(endpointOutcome.result(), request);
  87. if (outcome.isSuccess())
  88. return ActionDiskRmaOutcome(ActionDiskRmaResult(outcome.result()));
  89. else
  90. return ActionDiskRmaOutcome(outcome.error());
  91. }
  92. void TeslaDamClient::actionDiskRmaAsync(const ActionDiskRmaRequest& request, const ActionDiskRmaAsyncHandler& handler, const std::shared_ptr<const AsyncCallerContext>& context) const
  93. {
  94. auto fn = [this, request, handler, context]()
  95. {
  96. handler(this, request, actionDiskRma(request), context);
  97. };
  98. asyncExecute(new Runnable(fn));
  99. }
  100. TeslaDamClient::ActionDiskRmaOutcomeCallable TeslaDamClient::actionDiskRmaCallable(const ActionDiskRmaRequest &request) const
  101. {
  102. auto task = std::make_shared<std::packaged_task<ActionDiskRmaOutcome()>>(
  103. [this, request]()
  104. {
  105. return this->actionDiskRma(request);
  106. });
  107. asyncExecute(new Runnable([task]() { (*task)(); }));
  108. return task->get_future();
  109. }
  110. TeslaDamClient::ActionDiskMaskOutcome TeslaDamClient::actionDiskMask(const ActionDiskMaskRequest &request) const
  111. {
  112. auto endpointOutcome = endpoint();
  113. if (!endpointOutcome.isSuccess())
  114. return ActionDiskMaskOutcome(endpointOutcome.error());
  115. auto outcome = makeRequest(endpointOutcome.result(), request);
  116. if (outcome.isSuccess())
  117. return ActionDiskMaskOutcome(ActionDiskMaskResult(outcome.result()));
  118. else
  119. return ActionDiskMaskOutcome(outcome.error());
  120. }
  121. void TeslaDamClient::actionDiskMaskAsync(const ActionDiskMaskRequest& request, const ActionDiskMaskAsyncHandler& handler, const std::shared_ptr<const AsyncCallerContext>& context) const
  122. {
  123. auto fn = [this, request, handler, context]()
  124. {
  125. handler(this, request, actionDiskMask(request), context);
  126. };
  127. asyncExecute(new Runnable(fn));
  128. }
  129. TeslaDamClient::ActionDiskMaskOutcomeCallable TeslaDamClient::actionDiskMaskCallable(const ActionDiskMaskRequest &request) const
  130. {
  131. auto task = std::make_shared<std::packaged_task<ActionDiskMaskOutcome()>>(
  132. [this, request]()
  133. {
  134. return this->actionDiskMask(request);
  135. });
  136. asyncExecute(new Runnable([task]() { (*task)(); }));
  137. return task->get_future();
  138. }
  139. TeslaDamClient::ActionDiskCheckOutcome TeslaDamClient::actionDiskCheck(const ActionDiskCheckRequest &request) const
  140. {
  141. auto endpointOutcome = endpoint();
  142. if (!endpointOutcome.isSuccess())
  143. return ActionDiskCheckOutcome(endpointOutcome.error());
  144. auto outcome = makeRequest(endpointOutcome.result(), request);
  145. if (outcome.isSuccess())
  146. return ActionDiskCheckOutcome(ActionDiskCheckResult(outcome.result()));
  147. else
  148. return ActionDiskCheckOutcome(outcome.error());
  149. }
  150. void TeslaDamClient::actionDiskCheckAsync(const ActionDiskCheckRequest& request, const ActionDiskCheckAsyncHandler& handler, const std::shared_ptr<const AsyncCallerContext>& context) const
  151. {
  152. auto fn = [this, request, handler, context]()
  153. {
  154. handler(this, request, actionDiskCheck(request), context);
  155. };
  156. asyncExecute(new Runnable(fn));
  157. }
  158. TeslaDamClient::ActionDiskCheckOutcomeCallable TeslaDamClient::actionDiskCheckCallable(const ActionDiskCheckRequest &request) const
  159. {
  160. auto task = std::make_shared<std::packaged_task<ActionDiskCheckOutcome()>>(
  161. [this, request]()
  162. {
  163. return this->actionDiskCheck(request);
  164. });
  165. asyncExecute(new Runnable([task]() { (*task)(); }));
  166. return task->get_future();
  167. }
  168. TeslaDamClient::HostGetsOutcome TeslaDamClient::hostGets(const HostGetsRequest &request) const
  169. {
  170. auto endpointOutcome = endpoint();
  171. if (!endpointOutcome.isSuccess())
  172. return HostGetsOutcome(endpointOutcome.error());
  173. auto outcome = makeRequest(endpointOutcome.result(), request);
  174. if (outcome.isSuccess())
  175. return HostGetsOutcome(HostGetsResult(outcome.result()));
  176. else
  177. return HostGetsOutcome(outcome.error());
  178. }
  179. void TeslaDamClient::hostGetsAsync(const HostGetsRequest& request, const HostGetsAsyncHandler& handler, const std::shared_ptr<const AsyncCallerContext>& context) const
  180. {
  181. auto fn = [this, request, handler, context]()
  182. {
  183. handler(this, request, hostGets(request), context);
  184. };
  185. asyncExecute(new Runnable(fn));
  186. }
  187. TeslaDamClient::HostGetsOutcomeCallable TeslaDamClient::hostGetsCallable(const HostGetsRequest &request) const
  188. {
  189. auto task = std::make_shared<std::packaged_task<HostGetsOutcome()>>(
  190. [this, request]()
  191. {
  192. return this->hostGets(request);
  193. });
  194. asyncExecute(new Runnable([task]() { (*task)(); }));
  195. return task->get_future();
  196. }