EcsMetadataFetcher.cc 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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 "EcsMetadataFetcher.h"
  17. #include <sstream>
  18. #include <memory>
  19. #include <alibabacloud/core/Url.h>
  20. #include "CurlHttpClient.h"
  21. using namespace AlibabaCloud;
  22. EcsMetadataFetcher::EcsMetadataFetcher()
  23. {
  24. }
  25. EcsMetadataFetcher::~EcsMetadataFetcher()
  26. {
  27. }
  28. std::string EcsMetadataFetcher::roleName()const
  29. {
  30. return roleName_;
  31. }
  32. void EcsMetadataFetcher::setRoleName(const std::string & roleName)
  33. {
  34. roleName_ = roleName;
  35. }
  36. std::string EcsMetadataFetcher::getMetadata(const std::string host, const std::string in_path)
  37. {
  38. std::stringstream path;
  39. path << in_path << roleName_;
  40. Url credentialUrl;
  41. credentialUrl.setHost(host);
  42. credentialUrl.setPath(path.str());
  43. auto client = std::make_shared<CurlHttpClient>();
  44. HttpRequest request;
  45. request.setUrl(credentialUrl);
  46. auto outcome = client->makeRequest(request);
  47. if (outcome.isSuccess())
  48. return std::string(outcome.result().body(), outcome.result().bodySize());
  49. else
  50. return outcome.error().errorCode();
  51. }