CoreClient.cc 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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/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. using namespace AlibabaCloud;
  26. CoreClient::CoreClient(const std::string & servicename, const ClientConfiguration &configuration) :
  27. serviceName_(servicename),
  28. configuration_(configuration),
  29. httpClient_(new CurlHttpClient)
  30. {
  31. httpClient_->setProxy(configuration.proxy());
  32. }
  33. CoreClient::~CoreClient()
  34. {
  35. delete httpClient_;
  36. }
  37. ClientConfiguration CoreClient::configuration()const
  38. {
  39. return configuration_;
  40. }
  41. std::string CoreClient::serviceName()const
  42. {
  43. return serviceName_;
  44. }
  45. void CoreClient::asyncExecute(Runnable * r)const
  46. {
  47. Executor::instance()->execute(r);
  48. }
  49. HttpClient::HttpResponseOutcome CoreClient::AttemptRequest(const std::string & endpoint, const ServiceRequest & request, HttpRequest::Method method) const
  50. {
  51. auto r = buildHttpRequest(endpoint, request, method);
  52. auto outcome = httpClient_->makeRequest(r);
  53. if (!outcome.isSuccess())
  54. return outcome;
  55. if(hasResponseError(outcome.result()))
  56. return HttpClient::HttpResponseOutcome(buildCoreError(outcome.result()));
  57. else
  58. return outcome;
  59. }
  60. Error CoreClient::buildCoreError(const HttpResponse &response)const
  61. {
  62. Json::Reader reader;
  63. Json::Value value;
  64. if (!reader.parse(std::string(response.body(), response.bodySize()), value))
  65. return Error("InvalidResponse", "");
  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. return error;
  72. }
  73. bool CoreClient::hasResponseError(const HttpResponse &response)const
  74. {
  75. return response.statusCode() < 200 || response.statusCode() > 299;
  76. }