RestfulFunc.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. #ifndef _OTHER_RESFULFUNCEXPORT_HEADER_
  2. #define _OTHER_RESFULFUNCEXPORT_HEADER_
  3. #pragma once
  4. #ifdef _WIN32
  5. #ifdef RVC_RESTFUL_EXPORTS
  6. #ifndef RVCRESTFULSDK_API
  7. #define RVCRESTFULSDK_API __declspec(dllexport)
  8. #endif
  9. #else
  10. #ifndef RVCRESTFULSDK_API
  11. #define RVCRESTFULSDK_API __declspec(dllimport)
  12. #endif
  13. #endif
  14. #else
  15. #if ( defined(__GNUC__) && __GNUC__ >= 4 )
  16. #define RVCRESTFULSDK_API __attribute__((visibility("default")))
  17. #else
  18. #define RVCRESTFULSDK_API
  19. #endif
  20. #endif
  21. #include <iostream>
  22. #include <string>
  23. #include <map>
  24. #include <vector>
  25. #include <sstream>
  26. enum HttpRequestMethod
  27. {
  28. GET,
  29. POST,
  30. PUT,
  31. DEL,
  32. HEAD,
  33. OPTIONS,
  34. TRCE,
  35. CONNECT,
  36. MERGE,
  37. PATCH,
  38. UPLOAD,
  39. DOWNLOAD
  40. };
  41. enum HttpStatusCode : int
  42. {
  43. Continue = 100,
  44. SwitchingProtocols = 101,
  45. OK = 200,
  46. Created = 201,
  47. Accepted = 202,
  48. NonAuthInfo = 203,
  49. NoContent = 204,
  50. ResetContent = 205,
  51. PartialContent = 206,
  52. MultiStatus = 207,
  53. AlreadyReported = 208,
  54. MultipleChoices = 300,
  55. MovedPermanently = 301,
  56. Found = 302,
  57. SeeOther = 303,
  58. NotModified = 304,
  59. UseProxy = 305,
  60. Forbidden = 403,
  61. NotFound = 404,
  62. ServiceUnavailable = 503
  63. };
  64. struct HttpClientResponseResult
  65. {
  66. int statusCode;
  67. std::string content;
  68. HttpClientResponseResult() :statusCode(HttpStatusCode::NotFound), content("") {}
  69. /** 子类继承实现 */
  70. virtual bool ResponseOK() const
  71. {
  72. return (statusCode == HttpStatusCode::OK);
  73. }
  74. virtual std::string WhatError() const
  75. {
  76. if (statusCode <= 0) {
  77. return content;
  78. } else {
  79. std::ostringstream oss;
  80. oss << "statusCode: " << statusCode;
  81. return oss.str();
  82. }
  83. }
  84. };
  85. struct HttpClientRequestConfig
  86. {
  87. /*!
  88. * @brief
  89. * @param[in] uri: 必须以 "http://" 或 "https://" 开头
  90. * @param[out]
  91. * @return :
  92. */
  93. HttpClientRequestConfig(const std::string& uri)
  94. :mMethod(HttpRequestMethod::GET), mUri(uri), mToValidCert(false)
  95. , mBodyContent(""), mContentType(""), mHeaderAcceptType("application/json"), mTimeoutSecs(0), m_withToken(true)
  96. {
  97. }
  98. HttpClientRequestConfig(HttpRequestMethod method)
  99. : mMethod(method), mToValidCert(false), mBodyContent(""), mHeaderAcceptType("application/json")
  100. , mContentType(""), mTimeoutSecs(0), m_withToken(true)
  101. {
  102. }
  103. HttpClientRequestConfig(HttpRequestMethod method, const std::string& uri)
  104. :mMethod(method), mUri(uri), mToValidCert(false), mBodyContent("")
  105. , mContentType(""), mHeaderAcceptType("application/json"), mTimeoutSecs(0), m_withToken(true)
  106. {
  107. }
  108. void SetRequestType(HttpRequestMethod method) { mMethod = method; }
  109. HttpRequestMethod GetRequestType() const { return mMethod; }
  110. // uri: 必须以 "http://" 或 "https://" 开头
  111. void SetUri(const std::string& uri) { mUri = uri; }
  112. void SetChildUri(const std::string& subUri) { mSubUri = subUri; }
  113. void AppendQuery(const std::string& name, const std::string& value) {
  114. mQueryPairs[name] = value;
  115. }
  116. std::string GetBaseUri() const { return mUri; }
  117. std::string GetSubUri() const { return mSubUri; }
  118. std::string GetRequestUri() const { return mUri + mSubUri; }
  119. void ResetQuery() { mQueryPairs.clear(); }
  120. const std::map<std::string, std::string>& GetQueryPairs() const { return mQueryPairs; }
  121. std::string GetAcceptType() const { return mHeaderAcceptType; }
  122. void SetAcceptType(const std::string& value) {
  123. mHeaderAcceptType = value;
  124. }
  125. bool NeedValidCert() const { return mToValidCert; }
  126. void SetJsonBody(const std::string& value) {
  127. SetBodyContent(value, "application/json");
  128. }
  129. void SetBodyContent(const std::string& value, const std::string& type) {
  130. mBodyContent = value;
  131. mContentType = type;
  132. }
  133. virtual std::string GetBodyContent() const { return mBodyContent; }
  134. virtual std::string GetContentType() const { return mContentType; }
  135. void SetTimeout(uint32_t timeoutSecs) { mTimeoutSecs = timeoutSecs; }
  136. uint32_t GetTimeout() const { return mTimeoutSecs; }
  137. bool getToken(std::string& t_channelId, std::string& t_token) const;
  138. virtual void PreDo() {}
  139. virtual void PostDo() {}
  140. private:
  141. HttpRequestMethod mMethod;
  142. std::string mUri;
  143. std::string mSubUri;
  144. std::string mBodyContent;
  145. std::string mContentType;
  146. std::string mHeaderAcceptType;
  147. std::map<std::string, std::string> mQueryPairs;
  148. bool m_withToken;
  149. bool mToValidCert;
  150. uint32_t mTimeoutSecs;
  151. };
  152. struct HttpClientPostTypeRequest : public HttpClientRequestConfig
  153. {
  154. HttpClientPostTypeRequest(const std::string& uri)
  155. :HttpClientRequestConfig(HttpRequestMethod::POST, uri) {}
  156. };
  157. struct HttpClientUploadRequest : public HttpClientRequestConfig
  158. {
  159. HttpClientUploadRequest(const std::string& uri)
  160. :HttpClientRequestConfig(HttpRequestMethod::DOWNLOAD, uri)
  161. {
  162. }
  163. void ClearPararm() { mParams.clear(); }
  164. void AddParams(const std::string& name, const std::string& value)
  165. {
  166. mParams.push_back(std::move(std::pair<std::string, std::string>(name, value)));
  167. }
  168. void SetDefaultParam(const std::string& value)
  169. {
  170. AddParams("params", value);
  171. }
  172. void ClearFiles() { mFiles.clear(); }
  173. void AddFiles(const std::string& filePath)
  174. {
  175. mFiles.push_back(std::move(std::pair<std::string, std::string>("file_content", filePath)));
  176. }
  177. virtual void PreDo()
  178. {
  179. auto p = BuildBodyContent();
  180. std::cout << p.second << std::endl;
  181. SetBodyContent(p.second, std::string("multipart/form-data; boundary=") + p.first);
  182. }
  183. std::pair<std::string, std::string> BuildBodyContent() const;
  184. private:
  185. std::vector<std::pair<std::string, std::string> > mParams;
  186. std::vector<std::pair<std::string, std::string> > mFiles;
  187. };
  188. struct HttpClientDownloadRequest : public HttpClientRequestConfig
  189. {
  190. HttpClientDownloadRequest(const std::string& uri)
  191. :HttpClientRequestConfig(HttpRequestMethod::DOWNLOAD, uri) {
  192. SetAcceptType("*/*");
  193. }
  194. };
  195. class RVCRESTFULSDK_API RestfulClient
  196. {
  197. public:
  198. static RestfulClient& getInstance(); // Singleton
  199. ~RestfulClient();
  200. void Do(const HttpClientRequestConfig* const pRequestConfig, HttpClientResponseResult* pResponse) const;
  201. private:
  202. RestfulClient();
  203. private:
  204. };
  205. typedef void (*GetRestfulTokenCallBack)(std::string&, std::string&);
  206. RVCRESTFULSDK_API void SetRestfulTokenCallBack(GetRestfulTokenCallBack t_callBack);
  207. #endif //_OTHER_RESFULFUNCEXPORT_HEADER_