base_op.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #pragma once
  2. #ifndef COS_CPP_SDK_V5_INCLUDE_OP_BASE_OP_H_
  3. #define COS_CPP_SDK_V5_INCLUDE_OP_BASE_OP_H_
  4. #include <stdint.h>
  5. #include <map>
  6. #include <string>
  7. #include "cos_config.h"
  8. #include "op/cos_result.h"
  9. #include "trsf/transfer_handler.h"
  10. #include "util/base_op_util.h"
  11. namespace qcloud_cos {
  12. class BaseReq;
  13. class BaseResp;
  14. class BaseOp {
  15. public:
  16. /// \brief BaseOp构造函数
  17. ///
  18. /// \param cos_conf Cos配置
  19. explicit BaseOp(const SharedConfig& cos_conf) : m_config(cos_conf), m_op_util(m_config) {}
  20. BaseOp() {}
  21. /// \brief BaseOp析构函数
  22. ~BaseOp() {}
  23. /// \brief 获取Cos配置
  24. CosConfig GetCosConfig() const;
  25. /// \brief 获取AppID
  26. uint64_t GetAppId() const;
  27. /// \brief 获取Region
  28. std::string GetRegion() const;
  29. /// \brief 获取AccessKey
  30. std::string GetAccessKey() const;
  31. /// \brief 获取SecretKey
  32. std::string GetSecretKey() const;
  33. /// \brief 获取Token
  34. std::string GetTmpToken() const;
  35. std::string GetDestDomain() const;
  36. bool IsDomainSameToHost() const;
  37. bool IsDefaultHost(const std::string &host) const;
  38. /// \brief 封装了cos Service/Bucket/Object 相关接口的通用操作,
  39. /// 包括签名计算、请求发送、返回内容解析等
  40. ///
  41. /// \param host 目标主机, 以http://开头
  42. /// \param path http path
  43. /// \param req http请求
  44. /// \param req_body http request的body
  45. /// \param resp http返回
  46. /// \param is_ci_req 是否为万象域名请求
  47. ///
  48. /// \return http调用情况(状态码等)
  49. CosResult NormalAction(const std::string& host, const std::string& path,
  50. const BaseReq& req, const std::string& req_body,
  51. bool check_body, BaseResp* resp, bool is_ci_req = false);
  52. /// \brief 封装了cos Service/Bucket/Object相关接口的通用操作,
  53. /// 包括签名计算、请求发送、返回内容解析等
  54. ///
  55. /// \param host 目标主机, 以http://开头
  56. /// \param path http path
  57. /// \param req http请求
  58. /// \param additional_headers http请求需要所需的额外header
  59. /// \param additional_params http请求需要所需的额外params
  60. /// \param req_body http request的body
  61. /// \param resp http返回
  62. /// \param is_ci_req 是否为万象域名请求
  63. ///
  64. /// \return http调用情况(状态码等)
  65. CosResult NormalAction(
  66. const std::string& host, const std::string& path, const BaseReq& req,
  67. const std::map<std::string, std::string>& additional_headers,
  68. const std::map<std::string, std::string>& additional_params,
  69. const std::string& req_body, bool check_body, BaseResp* resp,
  70. bool is_ci_req = false);
  71. /// \brief 下载文件并输出到流中
  72. ///
  73. /// \param host 目标主机, 以http://开头
  74. /// \param path http path
  75. /// \param req http请求
  76. /// \param resp http返回
  77. /// \param os 输出流
  78. ///
  79. /// \return http调用情况(状态码等)
  80. CosResult DownloadAction(const std::string& host, const std::string& path,
  81. const BaseReq& req, BaseResp* resp, std::ostream& os,
  82. const SharedTransferHandler& handler = nullptr);
  83. /// \brief 支持从stream中读入数据并上传
  84. ///
  85. /// \param host 目标主机, 以http://开头
  86. /// \param path http path
  87. /// \param req http请求
  88. /// \param additional_headers http请求需要所需的额外header
  89. /// \param additional_params http请求需要所需的额外params
  90. /// \param is http request的body
  91. /// \param resp http返回
  92. ///
  93. /// \return http调用情况(状态码等)
  94. CosResult UploadAction(
  95. const std::string& host, const std::string& path, const BaseReq& req,
  96. const std::map<std::string, std::string>& additional_headers,
  97. const std::map<std::string, std::string>& additional_params,
  98. std::istream& is, BaseResp* resp, const SharedTransferHandler& handler = nullptr);
  99. std::string GetRealUrl(const std::string& host, const std::string& path,
  100. bool is_https, bool is_generate_presigned_url = false);
  101. protected:
  102. bool CheckConfigValidation() const;
  103. SharedConfig m_config;
  104. BaseOpUtil m_op_util;
  105. private:
  106. CosResult NormalRequest(
  107. const std::string& host, const std::string& path, const BaseReq& req,
  108. const std::map<std::string, std::string>& additional_headers,
  109. const std::map<std::string, std::string>& additional_params,
  110. const std::string& req_body, bool check_body, BaseResp* resp,
  111. const uint32_t &request_retry_num, bool is_ci_req = false);
  112. CosResult DownloadRequest(const std::string& host, const std::string& path,
  113. const BaseReq& req, BaseResp* resp, std::ostream& os,
  114. const uint32_t &request_retry_num,
  115. const SharedTransferHandler& handler = nullptr);
  116. CosResult UploadRequest(
  117. const std::string& host, const std::string& path, const BaseReq& req,
  118. const std::map<std::string, std::string>& additional_headers,
  119. const std::map<std::string, std::string>& additional_params,
  120. std::istream& is, BaseResp* resp, const uint32_t &request_retry_num, const SharedTransferHandler& handler = nullptr);
  121. bool NoNeedRetry(const CosResult &result);
  122. };
  123. } // namespace qcloud_cos
  124. #endif // COS_CPP_SDK_V5_INCLUDE_OP_BASE_OP_H_