123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252 |
- #ifndef _OTHER_RESFULFUNCEXPORT_HEADER_
- #define _OTHER_RESFULFUNCEXPORT_HEADER_
- #pragma once
- #ifdef _WIN32
- #ifdef RVC_RESTFUL_EXPORTS
- #ifndef RVCRESTFULSDK_API
- #define RVCRESTFULSDK_API __declspec(dllexport)
- #endif
- #else
- #ifndef RVCRESTFULSDK_API
- #define RVCRESTFULSDK_API __declspec(dllimport)
- #endif
- #endif
- #else
- #if ( defined(__GNUC__) && __GNUC__ >= 4 )
- #define RVCRESTFULSDK_API __attribute__((visibility("default")))
- #else
- #define RVCRESTFULSDK_API
- #endif
- #endif
- #include <iostream>
- #include <string>
- #include <map>
- #include <vector>
- #include <sstream>
- #include "httpTrace_define.hpp"
- enum HttpRequestMethod
- {
- GET,
- POST,
- PUT,
- DEL,
- HEAD,
- OPTIONS,
- TRCE,
- CONNECT,
- MERGE,
- PATCH,
- UPLOAD,
- DOWNLOAD
- };
- 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 HttpClientResponseResult
- {
- int statusCode;
- std::string content;
- HttpClientResponseResult() :statusCode(HttpStatusCode::NotFound), content("") {}
- /** 子类继承实现 */
- virtual bool ResponseOK() const
- {
- return (statusCode == HttpStatusCode::OK);
- }
- virtual std::string WhatError() const
- {
- if (statusCode <= 0) {
- return content;
- } else {
- std::ostringstream oss;
- oss << "statusCode: " << statusCode;
- return oss.str();
- }
- }
- };
- struct HttpClientRequestConfig
- {
- /*!
- * @brief
- * @param[in] uri: 必须以 "http://" 或 "https://" 开头
- * @param[out]
- * @return :
- */
- HttpClientRequestConfig(const std::string& uri)
- :mMethod(HttpRequestMethod::GET), mUri(uri), mToValidCert(false)
- , mBodyContent(""), mContentType(""), mHeaderAcceptType("application/json"), mTimeoutSecs(0), m_withToken(true)
- {
- }
- HttpClientRequestConfig(HttpRequestMethod method)
- : mMethod(method), mToValidCert(false), mBodyContent(""), mHeaderAcceptType("application/json")
- , mContentType(""), mTimeoutSecs(0), m_withToken(true)
- {
- }
- HttpClientRequestConfig(HttpRequestMethod method, const std::string& uri)
- :mMethod(method), mUri(uri), mToValidCert(false), mBodyContent("")
- , mContentType(""), mHeaderAcceptType("application/json"), mTimeoutSecs(0), m_withToken(true)
- {
- }
- void SetRequestType(HttpRequestMethod method) { mMethod = method; }
- HttpRequestMethod GetRequestType() const { return mMethod; }
- // uri: 必须以 "http://" 或 "https://" 开头
- 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; }
- std::string GetRequestUri() const { return mUri + mSubUri; }
- void ResetQuery() { mQueryPairs.clear(); }
- const std::map<std::string, std::string>& GetQueryPairs() const { return mQueryPairs; }
- std::string GetAcceptType() const { return mHeaderAcceptType; }
- void SetAcceptType(const std::string& value) {
- mHeaderAcceptType = value;
- }
- bool NeedValidCert() const { return mToValidCert; }
- void SetJsonBody(const std::string& value) {
- SetBodyContent(value, "application/json");
- }
- void SetBodyContent(const std::string& value, const std::string& type) {
- mBodyContent = value;
- mContentType = type;
- }
- virtual std::string GetBodyContent() const { return mBodyContent; }
- virtual std::string GetContentType() const { return mContentType; }
- void SetTimeout(uint32_t timeoutSecs) { mTimeoutSecs = timeoutSecs; }
- uint32_t GetTimeout() const { return mTimeoutSecs; }
- bool getToken(std::string& t_channelId, std::string& t_token, std::string& terminalNo, std::string& reserve1) const;
- void setWithToken(bool t_withToken) {
- m_withToken = t_withToken;
- }
- virtual void PreDo() {}
- virtual void PostDo() {}
- private:
- HttpRequestMethod mMethod;
- std::string mUri;
- std::string mSubUri;
- std::string mBodyContent;
- std::string mContentType;
- std::string mHeaderAcceptType;
- std::map<std::string, std::string> mQueryPairs;
- bool m_withToken;
- bool mToValidCert;
- uint32_t mTimeoutSecs;
- };
- struct HttpClientPostTypeRequest : public HttpClientRequestConfig
- {
- HttpClientPostTypeRequest(const std::string& uri)
- :HttpClientRequestConfig(HttpRequestMethod::POST, uri) {}
- };
- struct HttpClientUploadRequest : public HttpClientRequestConfig
- {
- HttpClientUploadRequest(const std::string& uri)
- :HttpClientRequestConfig(HttpRequestMethod::DOWNLOAD, uri)
- {
- }
- void ClearPararm() { mParams.clear(); }
- void AddParams(const std::string& name, const std::string& value)
- {
- mParams.push_back(std::move(std::pair<std::string, std::string>(name, value)));
- }
- void SetDefaultParam(const std::string& value)
- {
- AddParams("params", value);
- }
- void ClearFiles() { mFiles.clear(); }
- void AddFiles(const std::string& filePath)
- {
- mFiles.push_back(std::move(std::pair<std::string, std::string>("file_content", filePath)));
- }
- virtual void PreDo()
- {
- auto p = BuildBodyContent();
- std::cout << p.second << std::endl;
- SetBodyContent(p.second, std::string("multipart/form-data; boundary=") + p.first);
- }
- std::pair<std::string, std::string> BuildBodyContent() const;
- private:
- std::vector<std::pair<std::string, std::string> > mParams;
- std::vector<std::pair<std::string, std::string> > mFiles;
- };
- struct HttpClientDownloadRequest : public HttpClientRequestConfig
- {
- HttpClientDownloadRequest(const std::string& uri)
- :HttpClientRequestConfig(HttpRequestMethod::DOWNLOAD, uri) {
- SetAcceptType("*/*");
- }
- };
- class RVCRESTFULSDK_API RestfulClient
- {
- public:
- static RestfulClient& getInstance(); // Singleton
- ~RestfulClient();
- void Do(const HttpClientRequestConfig* const pRequestConfig, HttpClientResponseResult* pResponse, HttpClientTraceLink* pTrace) const;
- private:
- RestfulClient();
- private:
-
- };
- typedef void (*GetRestfulTokenCallBack)(std::string&, std::string&, std::string&, std::string&);
- RVCRESTFULSDK_API void SetRestfulTokenCallBack(GetRestfulTokenCallBack t_callBack);
- #endif //_OTHER_RESFULFUNCEXPORT_HEADER_
|