CoreClient.cc 2.9 KB

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