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;
|
||||
}
|
||||
|
@@ -2,18 +2,12 @@
|
||||
#define SDK_CXX_OPENCLIENT_H
|
||||
|
||||
#include <string>
|
||||
#include <json/json.h>
|
||||
#include "httplib.h"
|
||||
#include "../request/BaseRequest.h"
|
||||
#include "../thirdparty/CJsonObject/CJsonObject.hpp"
|
||||
|
||||
using namespace std;
|
||||
|
||||
struct HostInfo {
|
||||
string host;
|
||||
int port;
|
||||
char *path;
|
||||
};
|
||||
|
||||
/**
|
||||
* 请求客户端
|
||||
*/
|
||||
@@ -21,6 +15,7 @@ class OpenClient {
|
||||
private:
|
||||
/** 应用id */
|
||||
string appId;
|
||||
string url;
|
||||
/** 私钥文件路径 */
|
||||
string privateKeyFilePath;
|
||||
|
||||
@@ -39,19 +34,17 @@ public:
|
||||
* @param token token
|
||||
* @return 返回响应结果
|
||||
*/
|
||||
neb::CJsonObject execute(BaseRequest *request, const string& token);
|
||||
Json::Value execute(BaseRequest *request, const string& token);
|
||||
|
||||
/**
|
||||
* 发送请求
|
||||
* @param request 请求对象,BaseRequest的子类
|
||||
* @return 返回响应结果
|
||||
*/
|
||||
neb::CJsonObject execute(BaseRequest *request);
|
||||
Json::Value execute(BaseRequest *request);
|
||||
|
||||
private:
|
||||
|
||||
HostInfo hostInfo;
|
||||
|
||||
map<string, string> buildParams(BaseRequest *request, const string& token);
|
||||
|
||||
static httplib::MultipartFormDataItems
|
||||
@@ -59,7 +52,9 @@ private:
|
||||
|
||||
static httplib::Params getParams(map<string, string> params);
|
||||
|
||||
static neb::CJsonObject parseResponse(const string& responseBody,BaseRequest *request);
|
||||
static string getQuery(map<string, string> params);
|
||||
|
||||
static Json::Value parseResponse(const string& responseBody,BaseRequest *request);
|
||||
};
|
||||
|
||||
|
||||
|
File diff suppressed because it is too large
Load Diff
@@ -1,5 +1,5 @@
|
||||
#include <clocale>
|
||||
#include "tool.h"
|
||||
#include "json/json.h"
|
||||
#include <map>
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
@@ -21,49 +21,6 @@ namespace tool {
|
||||
return tmp;
|
||||
}
|
||||
|
||||
int parse_url(char *url, char **serverstrp, int *portp, char **pathstrp) {
|
||||
char buf[256];
|
||||
int serverlen, numread = 0;
|
||||
/* go through the url */
|
||||
|
||||
/* reset url to point PAST the http:// */
|
||||
|
||||
/* assume it's always 7 chars! */
|
||||
string _url = url;
|
||||
bool isHttps = startWith(_url, "https");
|
||||
int len = 7;
|
||||
if (isHttps) {
|
||||
len = 8;
|
||||
}
|
||||
url = url + len;
|
||||
/* no http:// now... server is simply up to the next / or : */
|
||||
|
||||
sscanf(url, "%255[^/:]", buf);
|
||||
serverlen = strlen(buf);
|
||||
*serverstrp = (char *) malloc(serverlen + 1);
|
||||
strcpy(*serverstrp, buf);
|
||||
if (url[serverlen] == ':') {
|
||||
/* get the port */
|
||||
|
||||
sscanf(&url[serverlen + 1], "%d%n", portp, &numread);
|
||||
/* add one to go PAST it */
|
||||
|
||||
numread++;
|
||||
|
||||
} else {
|
||||
if (isHttps) {
|
||||
*portp = 443;
|
||||
} else {
|
||||
*portp = 80;
|
||||
}
|
||||
|
||||
}
|
||||
/* the path is a pointer into the rest of url */
|
||||
|
||||
*pathstrp = &url[serverlen + numread];
|
||||
return 0;
|
||||
}
|
||||
|
||||
std::string url_encode(const std::string &str) {
|
||||
std::string strTemp = "";
|
||||
size_t length = str.length();
|
||||
@@ -75,7 +32,7 @@ namespace tool {
|
||||
(str[i] == '~'))
|
||||
strTemp += str[i];
|
||||
else if (str[i] == ' ')
|
||||
strTemp += "+";
|
||||
strTemp += "%20";
|
||||
else {
|
||||
strTemp += '%';
|
||||
strTemp += ToHex((unsigned char) str[i] >> 4);
|
||||
@@ -117,13 +74,12 @@ namespace tool {
|
||||
}
|
||||
|
||||
string mapToJson(std::map<string, string> map_info) {
|
||||
// Json::Value jObject;
|
||||
// for (map<string, string>::const_iterator iter = map_info.begin( ); iter != map_info.end( ); ++iter)
|
||||
// {
|
||||
// jObject[iter->first] = iter->second;
|
||||
// }
|
||||
// return jObject.toStyledString();
|
||||
return "{}";
|
||||
Json::Value jObject;
|
||||
for (map<string, string>::const_iterator iter = map_info.begin( ); iter != map_info.end( ); ++iter)
|
||||
{
|
||||
jObject[iter->first] = iter->second;
|
||||
}
|
||||
return jObject.toStyledString();
|
||||
}
|
||||
|
||||
string getFilename(string filepath) {
|
||||
|
@@ -16,8 +16,6 @@ namespace tool {
|
||||
|
||||
string getTime();
|
||||
|
||||
int parse_url(char *url, char **serverstrp, int *portp, char **pathstrp);
|
||||
|
||||
std::string url_encode(const std::string &szToEncode);
|
||||
|
||||
std::string url_decode(const std::string &SRC);
|
||||
|
Reference in New Issue
Block a user