RestfulFunc.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #ifndef _OTHER_RESFULFUNCEXPORT_HEADER_
  2. #define _OTHER_RESFULFUNCEXPORT_HEADER_
  3. #pragma once
  4. #ifdef _WIN32
  5. #ifdef RESTFULPUBLIC_EXPORTS
  6. #ifndef RESTFULFUNC_API
  7. #define RESTFULFUNC_API __declspec(dllexport)
  8. #endif
  9. #else
  10. #ifndef RESTFULFUNC_API
  11. #define RESTFULFUNC_API __declspec(dllimport)
  12. #endif
  13. #endif
  14. #else
  15. #if ( defined(__GNUC__) && __GNUC__ >= 4 )
  16. #define RESTFULFUNC_API __attribute__((visibility("default")))
  17. #else
  18. #define RESTFULFUNC_API
  19. #endif
  20. #endif
  21. #include <string>
  22. /*
  23. * > 0 : 网站返回的错误码,比如 200,404 等
  24. * -1:未归类,具体错误信息见 out_msg 返回的内容
  25. * -2:超时
  26. * -3:地址不符合规范
  27. */
  28. RESTFULFUNC_API int HttpProbe(const std::string& url, std::string& out_msg, uint32_t wait_max_secs = (uint32_t)-1);
  29. /*!
  30. * @brief Ping protocols
  31. * @param[in]
  32. * @param[out]
  33. * @return : 0: ping connect succ; 1: ping failed
  34. */
  35. RESTFULFUNC_API int PingTest(const std::string& dst_ip);
  36. enum HttpRequestMethod
  37. {
  38. GET,
  39. POST,
  40. PUT,
  41. DEL,
  42. HEAD,
  43. OPTIONS,
  44. TRCE,
  45. CONNECT,
  46. MERGE,
  47. PATCH
  48. };
  49. enum HttpStatusCode : int
  50. {
  51. Continue = 100,
  52. SwitchingProtocols = 101,
  53. OK = 200,
  54. Created = 201,
  55. Accepted = 202,
  56. NonAuthInfo = 203,
  57. NoContent = 204,
  58. ResetContent = 205,
  59. PartialContent = 206,
  60. MultiStatus = 207,
  61. AlreadyReported = 208,
  62. MultipleChoices = 300,
  63. MovedPermanently = 301,
  64. Found = 302,
  65. SeeOther = 303,
  66. NotModified = 304,
  67. UseProxy = 305,
  68. Forbidden = 403,
  69. NotFound = 404,
  70. ServiceUnavailable = 503
  71. };
  72. struct HttpClientRequestConfig
  73. {
  74. /*
  75. * Start with "http://" or "https://"
  76. */
  77. HttpClientRequestConfig(const std::string& uri) :mMethod(HttpRequestMethod::GET), mUri(uri) {}
  78. HttpClientRequestConfig(HttpRequestMethod method) : mMethod(method) {}
  79. HttpClientRequestConfig(HttpRequestMethod method, const std::string& uri) :mMethod(method), mUri(uri) {}
  80. void SetRequestType(HttpRequestMethod method) { mMethod = method; }
  81. void SetUri(const std::string& uri) { mUri = uri; }
  82. void SetChildUri(const std::string& subUri) { mSubUri = subUri; }
  83. std::string GetBaseUri() const { return mUri; }
  84. std::string GetSubUri() const { return mSubUri; }
  85. std::string GetRequestUri() const { return mUri + mSubUri; }
  86. HttpRequestMethod GetType() const { return mMethod; }
  87. private:
  88. HttpRequestMethod mMethod;
  89. std::string mUri;
  90. std::string mSubUri;
  91. };
  92. struct HttpClientResponseResult
  93. {
  94. int statusCode;
  95. std::string content;
  96. };
  97. class RESTFULFUNC_API RestfulClient
  98. {
  99. public:
  100. static RestfulClient& getInstance(); // Singleton
  101. ~RestfulClient();
  102. void Do(const HttpClientRequestConfig& requestConfig, HttpClientResponseResult& result);
  103. private:
  104. RestfulClient();
  105. private:
  106. };
  107. #endif //_OTHER_RESFULFUNCEXPORT_HEADER_