RestfulFunc.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. #include <map>
  23. /*
  24. * > 0 : ��վ���صĴ����룬���� 200��404 ��
  25. * -1��δ���࣬���������Ϣ�� out_msg ���ص�����
  26. * -2����ʱ
  27. * -3����ַ�����Ϲ淶
  28. */
  29. RESTFULFUNC_API int HttpProbe(const std::string& url, std::string& out_msg, uint32_t wait_max_secs = (uint32_t)-1);
  30. /*!
  31. * @brief Ping protocols
  32. * @param[in]
  33. * @param[out]
  34. * @return : 0: ping connect succ; 1: ping failed
  35. */
  36. RESTFULFUNC_API int PingTest(const std::string& dst_ip);
  37. enum HttpRequestMethod
  38. {
  39. GET,
  40. POST,
  41. PUT,
  42. DEL,
  43. HEAD,
  44. OPTIONS,
  45. TRCE,
  46. CONNECT,
  47. MERGE,
  48. PATCH
  49. };
  50. enum HttpStatusCode : int
  51. {
  52. Continue = 100,
  53. SwitchingProtocols = 101,
  54. OK = 200,
  55. Created = 201,
  56. Accepted = 202,
  57. NonAuthInfo = 203,
  58. NoContent = 204,
  59. ResetContent = 205,
  60. PartialContent = 206,
  61. MultiStatus = 207,
  62. AlreadyReported = 208,
  63. MultipleChoices = 300,
  64. MovedPermanently = 301,
  65. Found = 302,
  66. SeeOther = 303,
  67. NotModified = 304,
  68. UseProxy = 305,
  69. Forbidden = 403,
  70. NotFound = 404,
  71. ServiceUnavailable = 503
  72. };
  73. struct HttpClientRequestConfig
  74. {
  75. /*
  76. * Start with "http://" or "https://"
  77. */
  78. HttpClientRequestConfig(const std::string& uri) :mMethod(HttpRequestMethod::GET), mUri(uri) {}
  79. HttpClientRequestConfig(HttpRequestMethod method) : mMethod(method) {}
  80. HttpClientRequestConfig(HttpRequestMethod method, const std::string& uri) :mMethod(method), mUri(uri) {}
  81. void SetRequestType(HttpRequestMethod method) { mMethod = method; }
  82. void SetUri(const std::string& uri) { mUri = uri; }
  83. void SetChildUri(const std::string& subUri) { mSubUri = subUri; }
  84. void AppendQuery(const std::string& name, const std::string& value) {
  85. mQueryPairs[name] = value;
  86. }
  87. std::string GetBaseUri() const { return mUri; }
  88. std::string GetSubUri() const { return mSubUri; }
  89. const std::map<std::string, std::string>& GetQueryPairs() const { return mQueryPairs; }
  90. std::string GetRequestUri() const { return mUri + mSubUri; }
  91. HttpRequestMethod GetType() const { return mMethod; }
  92. private:
  93. HttpRequestMethod mMethod;
  94. std::string mUri;
  95. std::string mSubUri;
  96. std::map<std::string, std::string> mQueryPairs;
  97. };
  98. struct HttpClientResponseResult
  99. {
  100. int statusCode;
  101. std::string content;
  102. };
  103. class RESTFULFUNC_API RestfulClient
  104. {
  105. public:
  106. static RestfulClient& getInstance(); // Singleton
  107. ~RestfulClient();
  108. void Do(const HttpClientRequestConfig& requestConfig, HttpClientResponseResult& result);
  109. private:
  110. RestfulClient();
  111. private:
  112. };
  113. #endif //_OTHER_RESFULFUNCEXPORT_HEADER_