|
|
@@ -20,6 +20,7 @@
|
|
|
#endif
|
|
|
#endif
|
|
|
|
|
|
+#include <iostream>
|
|
|
#include <string>
|
|
|
#include <map>
|
|
|
#include <vector>
|
|
|
@@ -82,6 +83,21 @@ enum HttpStatusCode : int
|
|
|
ServiceUnavailable = 503
|
|
|
};
|
|
|
|
|
|
+
|
|
|
+struct HttpClientResponseResult
|
|
|
+{
|
|
|
+ int statusCode;
|
|
|
+ std::string content;
|
|
|
+
|
|
|
+ HttpClientResponseResult() :statusCode(HttpStatusCode::NotFound), content("") {}
|
|
|
+
|
|
|
+ /** 子类继承实现 */
|
|
|
+ virtual bool ConsiderAsSucc() const
|
|
|
+ {
|
|
|
+ return (statusCode == HttpStatusCode::OK);
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
struct HttpClientRequestConfig
|
|
|
{
|
|
|
/*!
|
|
|
@@ -92,19 +108,23 @@ struct HttpClientRequestConfig
|
|
|
*/
|
|
|
HttpClientRequestConfig(const std::string& uri)
|
|
|
:mMethod(HttpRequestMethod::GET), mUri(uri), mToValidCert(false)
|
|
|
- , mBodyContent(""), mContentType(""), mTimeoutSecs(0)
|
|
|
+ , mBodyContent(""), mContentType(""), mHeaderAcceptType("application/json"), mTimeoutSecs(0)
|
|
|
{
|
|
|
}
|
|
|
HttpClientRequestConfig(HttpRequestMethod method)
|
|
|
- : mMethod(method), mToValidCert(false), mBodyContent(""), mContentType(""), mTimeoutSecs(0)
|
|
|
+ : mMethod(method), mToValidCert(false), mBodyContent(""), mHeaderAcceptType("application/json")
|
|
|
+ , mContentType(""), mTimeoutSecs(0)
|
|
|
{
|
|
|
}
|
|
|
HttpClientRequestConfig(HttpRequestMethod method, const std::string& uri)
|
|
|
- :mMethod(method), mUri(uri), mToValidCert(false), mBodyContent(""), mContentType(""), mTimeoutSecs(0)
|
|
|
+ :mMethod(method), mUri(uri), mToValidCert(false), mBodyContent("")
|
|
|
+ , mContentType(""), mHeaderAcceptType("application/json"), mTimeoutSecs(0)
|
|
|
{
|
|
|
}
|
|
|
|
|
|
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; }
|
|
|
@@ -119,21 +139,10 @@ struct HttpClientRequestConfig
|
|
|
void ResetQuery() { mQueryPairs.clear(); }
|
|
|
const std::map<std::string, std::string>& GetQueryPairs() const { return mQueryPairs; }
|
|
|
|
|
|
- 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)));
|
|
|
+ std::string GetAcceptType() const { return mHeaderAcceptType; }
|
|
|
+ void SetAcceptType(const std::string& value) {
|
|
|
+ mHeaderAcceptType = value;
|
|
|
}
|
|
|
- std::pair<std::string, std::string> BuildBodyContent() const;
|
|
|
-
|
|
|
- HttpRequestMethod GetType() const { return mMethod; }
|
|
|
|
|
|
bool NeedValidCert() const { return mToValidCert; }
|
|
|
|
|
|
@@ -145,13 +154,15 @@ struct HttpClientRequestConfig
|
|
|
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; }
|
|
|
|
|
|
+ virtual void PreDo() {}
|
|
|
+ virtual void PostDo() {}
|
|
|
+
|
|
|
private:
|
|
|
|
|
|
HttpRequestMethod mMethod;
|
|
|
@@ -159,34 +170,62 @@ private:
|
|
|
std::string mSubUri;
|
|
|
std::string mBodyContent;
|
|
|
std::string mContentType;
|
|
|
+ std::string mHeaderAcceptType;
|
|
|
|
|
|
std::map<std::string, std::string> mQueryPairs;
|
|
|
- std::vector<std::pair<std::string, std::string> > mParams;
|
|
|
- std::vector<std::pair<std::string, std::string> > mFiles;
|
|
|
|
|
|
bool mToValidCert;
|
|
|
uint32_t mTimeoutSecs;
|
|
|
};
|
|
|
|
|
|
-
|
|
|
struct HttpClientPostTypeRequest : public HttpClientRequestConfig
|
|
|
{
|
|
|
- HttpClientPostTypeRequest(const std::string& uri) :HttpClientRequestConfig(HttpRequestMethod::POST, uri) {}
|
|
|
+ HttpClientPostTypeRequest(const std::string& uri)
|
|
|
+ :HttpClientRequestConfig(HttpRequestMethod::POST, uri) {}
|
|
|
};
|
|
|
|
|
|
-struct HttpClientDownloadRequest : public HttpClientRequestConfig
|
|
|
+struct HttpClientUploadRequest : public HttpClientRequestConfig
|
|
|
{
|
|
|
- HttpClientDownloadRequest(const std::string& uri) :HttpClientRequestConfig(HttpRequestMethod::POST, uri) {}
|
|
|
+ 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 HttpClientResponseResult
|
|
|
+struct HttpClientDownloadRequest : public HttpClientRequestConfig
|
|
|
{
|
|
|
- int statusCode;
|
|
|
- std::string content;
|
|
|
- /** 子类继承实现 */
|
|
|
- virtual bool ConsiderAsSucc() const {
|
|
|
- return (statusCode == HttpStatusCode::OK);
|
|
|
+ HttpClientDownloadRequest(const std::string& uri)
|
|
|
+ :HttpClientRequestConfig(HttpRequestMethod::DOWNLOAD, uri) {
|
|
|
+ SetAcceptType("*/*");
|
|
|
}
|
|
|
};
|
|
|
|
|
|
@@ -198,7 +237,7 @@ public:
|
|
|
static RestfulClient& getInstance(); // Singleton
|
|
|
~RestfulClient();
|
|
|
|
|
|
- void Do(const HttpClientRequestConfig& requestConfig, HttpClientResponseResult& result);
|
|
|
+ void Do(const HttpClientRequestConfig* const pRequestConfig, HttpClientResponseResult* pResponse) const;
|
|
|
|
|
|
private:
|
|
|
RestfulClient();
|