You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
SOP/sop-sdk/sdk-csharp/SDKCSharp/Client/OpenClient.cs

156 lines
5.3 KiB

6 years ago
using System;
using System.Web;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Threading.Tasks;
using SDKCSharp.Common;
using SDKCSharp.Request;
using SDKCSharp.Response;
using SDKCSharp.Utility;
6 years ago
using System.IO;
6 years ago
namespace SDKCSharp.Client
{
/// <summary>
/// 客户端
/// </summary>
public class OpenClient
{
private static OpenConfig DEFAULT_CONFIG = new OpenConfig();
private const string ERROR_RESPONSE_KEY = "error_response";
6 years ago
private Dictionary<string, string> header = new Dictionary<string, string>();
private string url;
private string appId;
private string privateKey;
6 years ago
private OpenConfig openConfig;
private OpenRequest openRequest;
private DataNameBuilder dataNameBuilder;
6 years ago
public OpenClient(string url, string appId, string privateKey) : this(url, appId, privateKey,false, DEFAULT_CONFIG)
6 years ago
{
}
public OpenClient(string url, string appId, string privateKey, bool priKeyFromFile) : this(url, appId, privateKey, priKeyFromFile, DEFAULT_CONFIG)
{
}
public OpenClient(string url, string appId, string privateKey,bool priKeyFromFile, OpenConfig openConfig)
6 years ago
{
this.url = url;
this.appId = appId;
this.privateKey = privateKey;
this.openConfig = openConfig;
this.openRequest = new OpenRequest(openConfig);
6 years ago
// 如果是从文件中加载私钥
if (priKeyFromFile)
{
this.privateKey = LoadCertificateFile(privateKey);
}
this.dataNameBuilder = openConfig.DataNameBuilder;
6 years ago
}
/// <summary>
/// 加载秘钥文件
/// </summary>
/// <returns>返回私钥内容.</returns>
/// <param name="filename">文件路径.</param>
private static string LoadCertificateFile(string filename)
{
if(!File.Exists(filename))
{
throw new SopException("文件不存在," + filename);
}
using (FileStream fs = File.OpenRead(filename))
{
byte[] data = new byte[fs.Length];
fs.Read(data, 0, data.Length);
return Encoding.UTF8.GetString(data);
}
6 years ago
}
/// <summary>
/// 发送请求
/// </summary>
/// <typeparam name="T">返回的Response类</typeparam>
/// <param name="request">请求对象</param>
/// <returns>返回Response类</returns>
public virtual T Execute<T>(BaseRequest<T> request) where T : BaseResponse
{
return this.Execute<T>(request, null);
}
/// <summary>
/// 发送请求
/// </summary>
/// <typeparam name="T">返回的Response类</typeparam>
/// <param name="request">请求对象</param>
/// <param name="accessToken">accessToken</param>
/// <returns>返回Response类</returns>
public virtual T Execute<T>(BaseRequest<T> request, string accessToken) where T : BaseResponse
{
RequestForm requestForm = request.CreateRequestForm(this.openConfig);
Dictionary<string, string> form = requestForm.Form;
if (!string.IsNullOrEmpty(accessToken))
{
form[this.openConfig.AccessTokenName] = accessToken;
}
form[this.openConfig.AppKeyName] = this.appId;
string content = SopSignature.getSignContent(form);
6 years ago
string sign = SignUtil.CreateSign(form, privateKey, request.Charset, request.SignType);
6 years ago
form[this.openConfig.SignName] = sign;
string resp = this.DoExecute(url, requestForm, header);
6 years ago
return this.ParseResponse<T>(resp, request);
6 years ago
}
/// <summary>
/// 执行请求
/// </summary>
/// <param name="url">请求url</param>
/// <param name="requestForm">请求内容</param>
/// <param name="header">请求header</param>
/// <returns>返回服务器响应内容</returns>
protected virtual string DoExecute(string url, RequestForm requestForm, Dictionary<string, string> header)
6 years ago
{
return openRequest.Request(this.url, requestForm, header);
}
/// <summary>
/// 解析返回结果
/// </summary>
/// <typeparam name="T">返回的Response</typeparam>
/// <param name="resp">服务器响应内容</param>
/// <param name="request">请求Request</param>
/// <returns>返回Response</returns>
protected virtual T ParseResponse<T>(string resp, BaseRequest<T> request) where T: BaseResponse {
6 years ago
string method = request.Method;
string dataName = this.dataNameBuilder.Build(method);
6 years ago
Dictionary<string, object> jsonObject = JsonUtil.ParseToDictionary(resp);
6 years ago
bool errorResponse = jsonObject.ContainsKey(ERROR_RESPONSE_KEY);
if (errorResponse)
{
dataName = ERROR_RESPONSE_KEY;
}
6 years ago
object data = jsonObject[dataName];
string jsonData = data == null ? "{}" : data.ToString();
T t = JsonUtil.ParseObject<T>(jsonData);
t.Body = jsonData;
return t;
}
}
}