123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- #ifndef _OTHER_RESFULFUNCEXPORT_HEADER_
- #define _OTHER_RESFULFUNCEXPORT_HEADER_
- #pragma once
- #ifdef _WIN32
- #ifdef RESTFULPUBLIC_EXPORTS
- #ifndef RESTFULFUNC_API
- #define RESTFULFUNC_API __declspec(dllexport)
- #endif
- #else
- #ifndef RESTFULFUNC_API
- #define RESTFULFUNC_API __declspec(dllimport)
- #endif
- #endif
- #else
- #if ( defined(__GNUC__) && __GNUC__ >= 4 )
- #define RESTFULFUNC_API __attribute__((visibility("default")))
- #else
- #define RESTFULFUNC_API
- #endif
- #endif
- #include <string>
- #include <map>
- /*
- * > 0 : ��վ���صĴ����룬���� 200��404 ��
- * -1��δ���࣬���������Ϣ�� out_msg ���ص�����
- * -2����ʱ
- * -3����ַ�����Ϲ淶
- */
- RESTFULFUNC_API int HttpProbe(const std::string& url, std::string& out_msg, uint32_t wait_max_secs = (uint32_t)-1);
- /*!
- * @brief Ping protocols
- * @param[in]
- * @param[out]
- * @return : 0: ping connect succ; 1: ping failed
- */
- RESTFULFUNC_API int PingTest(const std::string& dst_ip);
- enum HttpRequestMethod
- {
- GET,
- POST,
- PUT,
- DEL,
- HEAD,
- OPTIONS,
- TRCE,
- CONNECT,
- MERGE,
- PATCH
- };
- enum HttpStatusCode : int
- {
- Continue = 100,
- SwitchingProtocols = 101,
- OK = 200,
- Created = 201,
- Accepted = 202,
- NonAuthInfo = 203,
- NoContent = 204,
- ResetContent = 205,
- PartialContent = 206,
- MultiStatus = 207,
- AlreadyReported = 208,
- MultipleChoices = 300,
- MovedPermanently = 301,
- Found = 302,
- SeeOther = 303,
- NotModified = 304,
- UseProxy = 305,
- Forbidden = 403,
- NotFound = 404,
- ServiceUnavailable = 503
- };
- struct HttpClientRequestConfig
- {
- /*
- * Start with "http://" or "https://"
- */
- HttpClientRequestConfig(const std::string& uri) :mMethod(HttpRequestMethod::GET), mUri(uri) {}
- HttpClientRequestConfig(HttpRequestMethod method) : mMethod(method) {}
- HttpClientRequestConfig(HttpRequestMethod method, const std::string& uri) :mMethod(method), mUri(uri) {}
- void SetRequestType(HttpRequestMethod method) { mMethod = method; }
- void SetUri(const std::string& uri) { mUri = uri; }
- void SetChildUri(const std::string& subUri) { mSubUri = subUri; }
- void AppendQuery(const std::string& name, const std::string& value) {
- mQueryPairs[name] = value;
- }
- std::string GetBaseUri() const { return mUri; }
- std::string GetSubUri() const { return mSubUri; }
- const std::map<std::string, std::string>& GetQueryPairs() const { return mQueryPairs; }
- std::string GetRequestUri() const { return mUri + mSubUri; }
- HttpRequestMethod GetType() const { return mMethod; }
- private:
- HttpRequestMethod mMethod;
- std::string mUri;
- std::string mSubUri;
- std::map<std::string, std::string> mQueryPairs;
- };
- struct HttpClientResponseResult
- {
- int statusCode;
- std::string content;
- };
- class RESTFULFUNC_API RestfulClient
- {
- public:
- static RestfulClient& getInstance(); // Singleton
- ~RestfulClient();
- void Do(const HttpClientRequestConfig& requestConfig, HttpClientResponseResult& result);
- private:
- RestfulClient();
- private:
- };
- #endif //_OTHER_RESFULFUNCEXPORT_HEADER_
|