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