ImgsearchClient.cc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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/imgsearch/ImgsearchClient.h>
  17. #include <alibabacloud/core/SimpleCredentialsProvider.h>
  18. using namespace AlibabaCloud;
  19. using namespace AlibabaCloud::Location;
  20. using namespace AlibabaCloud::Imgsearch;
  21. using namespace AlibabaCloud::Imgsearch::Model;
  22. namespace
  23. {
  24. const std::string SERVICE_NAME = "imgsearch";
  25. }
  26. ImgsearchClient::ImgsearchClient(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, "imgsearch");
  31. }
  32. ImgsearchClient::ImgsearchClient(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, "imgsearch");
  37. }
  38. ImgsearchClient::ImgsearchClient(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, "imgsearch");
  43. }
  44. ImgsearchClient::~ImgsearchClient()
  45. {}
  46. ImgsearchClient::AddImageOutcome ImgsearchClient::addImage(const AddImageRequest &request) const
  47. {
  48. auto endpointOutcome = endpointProvider_->getEndpoint();
  49. if (!endpointOutcome.isSuccess())
  50. return AddImageOutcome(endpointOutcome.error());
  51. auto outcome = makeRequest(endpointOutcome.result(), request);
  52. if (outcome.isSuccess())
  53. return AddImageOutcome(AddImageResult(outcome.result()));
  54. else
  55. return AddImageOutcome(outcome.error());
  56. }
  57. void ImgsearchClient::addImageAsync(const AddImageRequest& request, const AddImageAsyncHandler& handler, const std::shared_ptr<const AsyncCallerContext>& context) const
  58. {
  59. auto fn = [this, request, handler, context]()
  60. {
  61. handler(this, request, addImage(request), context);
  62. };
  63. asyncExecute(new Runnable(fn));
  64. }
  65. ImgsearchClient::AddImageOutcomeCallable ImgsearchClient::addImageCallable(const AddImageRequest &request) const
  66. {
  67. auto task = std::make_shared<std::packaged_task<AddImageOutcome()>>(
  68. [this, request]()
  69. {
  70. return this->addImage(request);
  71. });
  72. asyncExecute(new Runnable([task]() { (*task)(); }));
  73. return task->get_future();
  74. }
  75. ImgsearchClient::CreateImageDbOutcome ImgsearchClient::createImageDb(const CreateImageDbRequest &request) const
  76. {
  77. auto endpointOutcome = endpointProvider_->getEndpoint();
  78. if (!endpointOutcome.isSuccess())
  79. return CreateImageDbOutcome(endpointOutcome.error());
  80. auto outcome = makeRequest(endpointOutcome.result(), request);
  81. if (outcome.isSuccess())
  82. return CreateImageDbOutcome(CreateImageDbResult(outcome.result()));
  83. else
  84. return CreateImageDbOutcome(outcome.error());
  85. }
  86. void ImgsearchClient::createImageDbAsync(const CreateImageDbRequest& request, const CreateImageDbAsyncHandler& handler, const std::shared_ptr<const AsyncCallerContext>& context) const
  87. {
  88. auto fn = [this, request, handler, context]()
  89. {
  90. handler(this, request, createImageDb(request), context);
  91. };
  92. asyncExecute(new Runnable(fn));
  93. }
  94. ImgsearchClient::CreateImageDbOutcomeCallable ImgsearchClient::createImageDbCallable(const CreateImageDbRequest &request) const
  95. {
  96. auto task = std::make_shared<std::packaged_task<CreateImageDbOutcome()>>(
  97. [this, request]()
  98. {
  99. return this->createImageDb(request);
  100. });
  101. asyncExecute(new Runnable([task]() { (*task)(); }));
  102. return task->get_future();
  103. }
  104. ImgsearchClient::DeleteImageOutcome ImgsearchClient::deleteImage(const DeleteImageRequest &request) const
  105. {
  106. auto endpointOutcome = endpointProvider_->getEndpoint();
  107. if (!endpointOutcome.isSuccess())
  108. return DeleteImageOutcome(endpointOutcome.error());
  109. auto outcome = makeRequest(endpointOutcome.result(), request);
  110. if (outcome.isSuccess())
  111. return DeleteImageOutcome(DeleteImageResult(outcome.result()));
  112. else
  113. return DeleteImageOutcome(outcome.error());
  114. }
  115. void ImgsearchClient::deleteImageAsync(const DeleteImageRequest& request, const DeleteImageAsyncHandler& handler, const std::shared_ptr<const AsyncCallerContext>& context) const
  116. {
  117. auto fn = [this, request, handler, context]()
  118. {
  119. handler(this, request, deleteImage(request), context);
  120. };
  121. asyncExecute(new Runnable(fn));
  122. }
  123. ImgsearchClient::DeleteImageOutcomeCallable ImgsearchClient::deleteImageCallable(const DeleteImageRequest &request) const
  124. {
  125. auto task = std::make_shared<std::packaged_task<DeleteImageOutcome()>>(
  126. [this, request]()
  127. {
  128. return this->deleteImage(request);
  129. });
  130. asyncExecute(new Runnable([task]() { (*task)(); }));
  131. return task->get_future();
  132. }
  133. ImgsearchClient::DeleteImageDbOutcome ImgsearchClient::deleteImageDb(const DeleteImageDbRequest &request) const
  134. {
  135. auto endpointOutcome = endpointProvider_->getEndpoint();
  136. if (!endpointOutcome.isSuccess())
  137. return DeleteImageDbOutcome(endpointOutcome.error());
  138. auto outcome = makeRequest(endpointOutcome.result(), request);
  139. if (outcome.isSuccess())
  140. return DeleteImageDbOutcome(DeleteImageDbResult(outcome.result()));
  141. else
  142. return DeleteImageDbOutcome(outcome.error());
  143. }
  144. void ImgsearchClient::deleteImageDbAsync(const DeleteImageDbRequest& request, const DeleteImageDbAsyncHandler& handler, const std::shared_ptr<const AsyncCallerContext>& context) const
  145. {
  146. auto fn = [this, request, handler, context]()
  147. {
  148. handler(this, request, deleteImageDb(request), context);
  149. };
  150. asyncExecute(new Runnable(fn));
  151. }
  152. ImgsearchClient::DeleteImageDbOutcomeCallable ImgsearchClient::deleteImageDbCallable(const DeleteImageDbRequest &request) const
  153. {
  154. auto task = std::make_shared<std::packaged_task<DeleteImageDbOutcome()>>(
  155. [this, request]()
  156. {
  157. return this->deleteImageDb(request);
  158. });
  159. asyncExecute(new Runnable([task]() { (*task)(); }));
  160. return task->get_future();
  161. }
  162. ImgsearchClient::ListImageDbsOutcome ImgsearchClient::listImageDbs(const ListImageDbsRequest &request) const
  163. {
  164. auto endpointOutcome = endpointProvider_->getEndpoint();
  165. if (!endpointOutcome.isSuccess())
  166. return ListImageDbsOutcome(endpointOutcome.error());
  167. auto outcome = makeRequest(endpointOutcome.result(), request);
  168. if (outcome.isSuccess())
  169. return ListImageDbsOutcome(ListImageDbsResult(outcome.result()));
  170. else
  171. return ListImageDbsOutcome(outcome.error());
  172. }
  173. void ImgsearchClient::listImageDbsAsync(const ListImageDbsRequest& request, const ListImageDbsAsyncHandler& handler, const std::shared_ptr<const AsyncCallerContext>& context) const
  174. {
  175. auto fn = [this, request, handler, context]()
  176. {
  177. handler(this, request, listImageDbs(request), context);
  178. };
  179. asyncExecute(new Runnable(fn));
  180. }
  181. ImgsearchClient::ListImageDbsOutcomeCallable ImgsearchClient::listImageDbsCallable(const ListImageDbsRequest &request) const
  182. {
  183. auto task = std::make_shared<std::packaged_task<ListImageDbsOutcome()>>(
  184. [this, request]()
  185. {
  186. return this->listImageDbs(request);
  187. });
  188. asyncExecute(new Runnable([task]() { (*task)(); }));
  189. return task->get_future();
  190. }
  191. ImgsearchClient::ListImagesOutcome ImgsearchClient::listImages(const ListImagesRequest &request) const
  192. {
  193. auto endpointOutcome = endpointProvider_->getEndpoint();
  194. if (!endpointOutcome.isSuccess())
  195. return ListImagesOutcome(endpointOutcome.error());
  196. auto outcome = makeRequest(endpointOutcome.result(), request);
  197. if (outcome.isSuccess())
  198. return ListImagesOutcome(ListImagesResult(outcome.result()));
  199. else
  200. return ListImagesOutcome(outcome.error());
  201. }
  202. void ImgsearchClient::listImagesAsync(const ListImagesRequest& request, const ListImagesAsyncHandler& handler, const std::shared_ptr<const AsyncCallerContext>& context) const
  203. {
  204. auto fn = [this, request, handler, context]()
  205. {
  206. handler(this, request, listImages(request), context);
  207. };
  208. asyncExecute(new Runnable(fn));
  209. }
  210. ImgsearchClient::ListImagesOutcomeCallable ImgsearchClient::listImagesCallable(const ListImagesRequest &request) const
  211. {
  212. auto task = std::make_shared<std::packaged_task<ListImagesOutcome()>>(
  213. [this, request]()
  214. {
  215. return this->listImages(request);
  216. });
  217. asyncExecute(new Runnable([task]() { (*task)(); }));
  218. return task->get_future();
  219. }
  220. ImgsearchClient::SearchImageOutcome ImgsearchClient::searchImage(const SearchImageRequest &request) const
  221. {
  222. auto endpointOutcome = endpointProvider_->getEndpoint();
  223. if (!endpointOutcome.isSuccess())
  224. return SearchImageOutcome(endpointOutcome.error());
  225. auto outcome = makeRequest(endpointOutcome.result(), request);
  226. if (outcome.isSuccess())
  227. return SearchImageOutcome(SearchImageResult(outcome.result()));
  228. else
  229. return SearchImageOutcome(outcome.error());
  230. }
  231. void ImgsearchClient::searchImageAsync(const SearchImageRequest& request, const SearchImageAsyncHandler& handler, const std::shared_ptr<const AsyncCallerContext>& context) const
  232. {
  233. auto fn = [this, request, handler, context]()
  234. {
  235. handler(this, request, searchImage(request), context);
  236. };
  237. asyncExecute(new Runnable(fn));
  238. }
  239. ImgsearchClient::SearchImageOutcomeCallable ImgsearchClient::searchImageCallable(const SearchImageRequest &request) const
  240. {
  241. auto task = std::make_shared<std::packaged_task<SearchImageOutcome()>>(
  242. [this, request]()
  243. {
  244. return this->searchImage(request);
  245. });
  246. asyncExecute(new Runnable([task]() { (*task)(); }));
  247. return task->get_future();
  248. }