mirror of
https://gitee.com/durcframework/SOP.git
synced 2025-08-11 21:57:56 +08:00
4.2.7
This commit is contained in:
@@ -1,10 +1,9 @@
|
||||
#include <json/json.h>
|
||||
#include "OpenClient.h"
|
||||
#include "httplib.h"
|
||||
#include "tool.h"
|
||||
#include "sign.h"
|
||||
|
||||
#include "../thirdparty/CJsonObject/CJsonObject.hpp"
|
||||
|
||||
httplib::Headers headers = {
|
||||
{"Accept-Encoding", "identity"}
|
||||
};
|
||||
@@ -13,59 +12,51 @@ const string ERROR_NODE = "error_response";
|
||||
|
||||
OpenClient::OpenClient(const string &appId, const string &privateKeyFilePath, const string &url) {
|
||||
this->appId = appId;
|
||||
this->url = url;
|
||||
this->privateKeyFilePath = privateKeyFilePath;
|
||||
|
||||
char *_url = const_cast<char *>(url.c_str());
|
||||
char *host;
|
||||
int port;
|
||||
char *path;
|
||||
tool::parse_url(_url, &host, &port, &path);
|
||||
this->hostInfo = HostInfo{
|
||||
host = host,
|
||||
port = port,
|
||||
path = path
|
||||
};
|
||||
}
|
||||
|
||||
neb::CJsonObject OpenClient::execute(BaseRequest *request) {
|
||||
Json::Value OpenClient::execute(BaseRequest *request) {
|
||||
return this->execute(request, "");
|
||||
}
|
||||
|
||||
neb::CJsonObject OpenClient::execute(BaseRequest *request, const string &token) {
|
||||
Json::Value OpenClient::execute(BaseRequest *request, const string &token) {
|
||||
string method = request->getMethod();
|
||||
string version = request->getVersion();
|
||||
RequestType requestType = request->getRequestType();
|
||||
map<string, string> bizModel = request->bizModel;
|
||||
// 创建HTTP请求客户端
|
||||
httplib::Client cli(this->hostInfo.host, this->hostInfo.port);
|
||||
// httplib::Client cli(this->hostInfo.host, this->hostInfo.port);
|
||||
httplib::Client cli(this->url.c_str());
|
||||
const char *url = this->url.c_str();
|
||||
// 构建请求参数
|
||||
map<string, string> allParams = this->buildParams(request, token);
|
||||
char *path = this->hostInfo.path;
|
||||
string responseBody;
|
||||
// 如果有文件上传
|
||||
if (!request->getFiles().empty()) {
|
||||
httplib::MultipartFormDataItems items = OpenClient::getMultipartFormDataItems(
|
||||
allParams, request->getFiles());
|
||||
responseBody = cli.Post(path, headers, items)->body;
|
||||
responseBody = cli.Post(url, headers, items)->body;
|
||||
} else {
|
||||
switch (requestType) {
|
||||
case GET: {
|
||||
responseBody = cli.Get(path, allParams, headers)->body;
|
||||
string fullPath = this->url + "?" + OpenClient::getQuery(allParams);
|
||||
responseBody = cli.Get(fullPath.c_str())->body;
|
||||
break;
|
||||
}
|
||||
case POST_FORM: {
|
||||
responseBody = cli.Post(path, headers, OpenClient::getParams(allParams))->body;
|
||||
responseBody = cli.Post(url, headers, OpenClient::getParams(allParams))->body;
|
||||
break;
|
||||
}
|
||||
case POST_JSON: {
|
||||
string json = tool::mapToJson(allParams);
|
||||
responseBody = cli.Post(path, json, "application/json")->body;
|
||||
responseBody = cli.Post(url, json, "application/json")->body;
|
||||
break;
|
||||
}
|
||||
case POST_FILE: {
|
||||
httplib::MultipartFormDataItems items = OpenClient::getMultipartFormDataItems(
|
||||
allParams, request->getFiles());
|
||||
responseBody = cli.Post(path, headers, items)->body;
|
||||
responseBody = cli.Post(url, headers, items)->body;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -82,6 +73,19 @@ httplib::Params OpenClient::getParams(map<string, string> allParams) {
|
||||
return params;
|
||||
}
|
||||
|
||||
string OpenClient::getQuery(map<string, string> allParams) {
|
||||
string query;
|
||||
map<string, string>::iterator it;
|
||||
int i = 0;
|
||||
for (it = allParams.begin(); it != allParams.end(); ++it) {
|
||||
if (i++ > 0) {
|
||||
query.append("&");
|
||||
}
|
||||
query.append(it->first).append("=").append(tool::url_encode(it->second));
|
||||
}
|
||||
return query;
|
||||
}
|
||||
|
||||
map<string, string> OpenClient::buildParams(BaseRequest *request, const string &token) {
|
||||
map<string, string> allParams;
|
||||
allParams["app_id"] = this->appId;
|
||||
@@ -90,15 +94,12 @@ map<string, string> OpenClient::buildParams(BaseRequest *request, const string &
|
||||
allParams["sign_type"] = "RSA2";
|
||||
allParams["timestamp"] = tool::getTime();
|
||||
allParams["version"] = request->getVersion();
|
||||
allParams["biz_content"] = tool::mapToJson(request->bizModel);
|
||||
|
||||
if (!token.empty()) {
|
||||
allParams["app_auth_token"] = token;
|
||||
}
|
||||
|
||||
map<string, string> bizModel = request->bizModel;
|
||||
|
||||
allParams.insert(bizModel.begin(), bizModel.end());
|
||||
|
||||
// 生成签名
|
||||
string sign = signutil::createSign(allParams, this->privateKeyFilePath, "RSA2");
|
||||
allParams["sign"] = sign;
|
||||
@@ -121,13 +122,16 @@ OpenClient::getMultipartFormDataItems(map<string, string> allParams, vector<File
|
||||
return items;
|
||||
}
|
||||
|
||||
neb::CJsonObject OpenClient::parseResponse(const string& responseBody, BaseRequest *request) {
|
||||
neb::CJsonObject oJson(responseBody);
|
||||
neb::CJsonObject data = oJson[ERROR_NODE];
|
||||
if (data.IsEmpty()) {
|
||||
Json::Value OpenClient::parseResponse(const string& responseBody, BaseRequest *request) {
|
||||
Json::Value root;
|
||||
Json::Reader reader;
|
||||
reader.parse(responseBody, root, false);
|
||||
|
||||
Json::Value data = root[ERROR_NODE];
|
||||
if (!data) {
|
||||
string method = request->getMethod();
|
||||
string nodeName = tool::replace(method.c_str(),".","_") + "_response";
|
||||
data = oJson[nodeName];
|
||||
data = root[nodeName];
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
Reference in New Issue
Block a user