RestfulFunc.h 6.7 KB

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