CoreClient.cc 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. * Copyright 1999-2019 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/core/CoreClient.h>
  17. #include <json/json.h>
  18. #include <alibabacloud/core/Signer.h>
  19. #include "CurlHttpClient.h"
  20. #include "Executor.h"
  21. /*!
  22. * \class AlibabaCloud::CoreClient CoreClient.h <alibabacloud/core/CoreClient.h>
  23. *
  24. */
  25. namespace AlibabaCloud {
  26. CoreClient::CoreClient(const std::string & servicename,
  27. const ClientConfiguration &configuration) :
  28. serviceName_(servicename),
  29. configuration_(configuration),
  30. httpClient_(new CurlHttpClient) {
  31. httpClient_->setProxy(configuration.proxy());
  32. }
  33. CoreClient::~CoreClient() {
  34. delete httpClient_;
  35. }
  36. ClientConfiguration CoreClient::configuration()const {
  37. return configuration_;
  38. }
  39. std::string CoreClient::serviceName()const {
  40. return serviceName_;
  41. }
  42. void CoreClient::asyncExecute(Runnable * r)const {
  43. Executor::instance()->execute(r);
  44. }
  45. HttpClient::HttpResponseOutcome CoreClient::AttemptRequest(
  46. const std::string & endpoint,
  47. const ServiceRequest & request, HttpRequest::Method method) const {
  48. auto r = buildHttpRequest(endpoint, request, method);
  49. auto outcome = httpClient_->makeRequest(r);
  50. if (!outcome.isSuccess())
  51. return outcome;
  52. if (hasResponseError(outcome.result()))
  53. return HttpClient::HttpResponseOutcome(buildCoreError(outcome.result()));
  54. else
  55. return outcome;
  56. }
  57. Error CoreClient::buildCoreError(const HttpResponse &response)const {
  58. Json::Reader reader;
  59. Json::Value value;
  60. if (!reader.parse(std::string(response.body(), response.bodySize()), value)) {
  61. if (response.bodySize() > 0) {
  62. return Error("InvalidResponse", response.body());
  63. } else {
  64. return Error("InvalidResponse", "body is empty");
  65. }
  66. }
  67. Error error;
  68. error.setErrorCode(value["Code"].asString());
  69. error.setErrorMessage(value["Message"].asString());
  70. error.setHost(value["HostId"].asString());
  71. error.setRequestId(value["RequestId"].asString());
  72. if (value["Code"].asString().empty() || value["Message"].asString().empty()) {
  73. error.setDetail(std::string(response.body()));
  74. }
  75. return error;
  76. }
  77. bool CoreClient::hasResponseError(const HttpResponse &response)const {
  78. return response.statusCode() < 200 || response.statusCode() > 299;
  79. }
  80. } // namespace AlibabaCloud