cos_result.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // Copyright (c) 2017, Tencent Inc.
  2. // All rights reserved.
  3. //
  4. // Author: sevenyou <sevenyou@tencent.com>
  5. // Created: 07/25/17
  6. // Description:
  7. #ifndef COS_RESULT_H
  8. #define COS_RESULT_H
  9. #pragma once
  10. #include <map>
  11. #include <string>
  12. namespace qcloud_cos {
  13. // 封装HTTP状态码:3XX,4XX,5XX 的返回结果
  14. // 详见官网链接: https://www.qcloud.com/document/product/436/7730
  15. class CosResult {
  16. public:
  17. CosResult()
  18. : m_is_succ(false),
  19. m_http_status(-1),
  20. m_err_code(""),
  21. m_err_msg(""),
  22. m_resource_addr(""),
  23. m_x_cos_request_id(""),
  24. m_x_cos_trace_id("") {}
  25. ~CosResult() {}
  26. CosResult(const CosResult& other) {
  27. m_is_succ = other.m_is_succ;
  28. m_http_status = other.m_http_status;
  29. m_err_code = other.m_err_code;
  30. m_err_msg = other.m_err_msg;
  31. m_resource_addr = other.m_resource_addr;
  32. m_x_cos_request_id = other.m_x_cos_request_id;
  33. m_x_cos_trace_id = other.m_x_cos_trace_id;
  34. m_real_byte = other.m_real_byte;
  35. }
  36. CosResult& operator=(const CosResult& other) {
  37. if (this != &other) {
  38. m_is_succ = other.m_is_succ;
  39. m_http_status = other.m_http_status;
  40. m_err_code = other.m_err_code;
  41. m_err_msg = other.m_err_msg;
  42. m_resource_addr = other.m_resource_addr;
  43. m_x_cos_request_id = other.m_x_cos_request_id;
  44. m_x_cos_trace_id = other.m_x_cos_trace_id;
  45. m_real_byte = other.m_real_byte;
  46. }
  47. return *this;
  48. }
  49. bool IsSucc() const { return m_is_succ; }
  50. void SetSucc() { m_is_succ = true; }
  51. void SetFail() { m_is_succ = false; }
  52. void Clear() {
  53. m_is_succ = false;
  54. m_http_status = -1;
  55. m_err_code = "";
  56. m_err_msg = "";
  57. m_resource_addr = "";
  58. m_x_cos_request_id = "";
  59. m_x_cos_trace_id = "";
  60. }
  61. // 解析xml string
  62. bool ParseFromHttpResponse(const std::map<std::string, std::string>& headers,
  63. const std::string& body);
  64. // Getter
  65. int GetHttpStatus() const { return m_http_status; }
  66. std::string GetErrorCode() const { return m_err_code; }
  67. std::string GetErrorMsg() const { return m_err_msg; }
  68. std::string GetResourceAddr() const { return m_resource_addr; }
  69. std::string GetXCosRequestId() const { return m_x_cos_request_id; }
  70. std::string GetXCosTraceId() const { return m_x_cos_trace_id; }
  71. std::string GetXCosServerTime() const { return m_x_cos_server_time; }
  72. uint64_t GetRealByte() const { return m_real_byte; }
  73. // Setter
  74. void SetHttpStatus(int http_status) { m_http_status = http_status; }
  75. void SetErrorCode(const std::string& err_code) { m_err_code = err_code; }
  76. void SetErrorMsg(const std::string& err_msg) { m_err_msg = err_msg; }
  77. void SetResourceAddr(const std::string& resource_addr) {
  78. m_resource_addr = resource_addr;
  79. }
  80. void SetXCosRequestId(const std::string& x_cos_request_id) {
  81. m_x_cos_request_id = x_cos_request_id;
  82. }
  83. void SetXCosTraceId(const std::string& x_cos_trace_id) {
  84. m_x_cos_trace_id = x_cos_trace_id;
  85. }
  86. void SetRealByte(uint64_t real_byte) { m_real_byte = real_byte; }
  87. /// \brief 输出Result的具体信息
  88. std::string DebugString() const;
  89. std::string GetInitMpRequestId() const { return m_init_mp_request_id; }
  90. void SetInitMpRequestId(const std::string& request_id) {
  91. m_init_mp_request_id = request_id;
  92. }
  93. private:
  94. bool m_is_succ; // 标识HTTP调用是否成功
  95. int m_http_status; // http状态码
  96. // COS返回的错误信息
  97. std::string m_err_code;
  98. std::string m_err_msg;
  99. std::string m_resource_addr;
  100. std::string m_x_cos_request_id;
  101. std::string m_x_cos_trace_id;
  102. std::string m_x_cos_server_time;
  103. uint64_t m_real_byte;
  104. // MultiUploadObject接口中封装了init mp/upload part/complete,该成员保存init
  105. // mp的request id 如果是断点续传,则该reqeust id为空
  106. std::string m_init_mp_request_id;
  107. };
  108. } // namespace qcloud_cos
  109. #endif // COS_RESULT_H