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;
using System.IO;
namespace SDKCSharp.Client
{
///
/// 客户端
///
public class OpenClient
{
private static OpenConfig DEFAULT_CONFIG = new OpenConfig();
private const string ERROR_RESPONSE_KEY = "error_response";
private Dictionary header = new Dictionary();
private string url;
private string appId;
private string privateKey;
private OpenConfig openConfig;
private OpenRequest openRequest;
private DataNameBuilder dataNameBuilder;
public OpenClient(string url, string appId, string privateKey) : this(url, appId, privateKey,false, DEFAULT_CONFIG)
{
}
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)
{
this.url = url;
this.appId = appId;
this.privateKey = privateKey;
this.openConfig = openConfig;
this.openRequest = new OpenRequest(openConfig);
// 如果是从文件中加载私钥
if (priKeyFromFile)
{
this.privateKey = LoadCertificateFile(privateKey);
}
this.dataNameBuilder = openConfig.DataNameBuilder;
}
///
/// 加载秘钥文件
///
/// 返回私钥内容.
/// 文件路径.
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);
}
}
///
/// 发送请求
///
/// 返回的Response类
/// 请求对象
/// 返回Response类
public virtual T Execute(BaseRequest request) where T : BaseResponse
{
return this.Execute(request, null);
}
///
/// 发送请求
///
/// 返回的Response类
/// 请求对象
/// accessToken
/// 返回Response类
public virtual T Execute(BaseRequest request, string accessToken) where T : BaseResponse
{
RequestForm requestForm = request.CreateRequestForm(this.openConfig);
Dictionary form = requestForm.Form;
if (!string.IsNullOrEmpty(accessToken))
{
form[this.openConfig.AccessTokenName] = accessToken;
}
form[this.openConfig.AppKeyName] = this.appId;
string content = SopSignature.getSignContent(form);
string sign = SignUtil.CreateSign(form, privateKey, request.Charset, request.SignType);
form[this.openConfig.SignName] = sign;
string resp = this.DoExecute(url, requestForm, header);
return this.ParseResponse(resp, request);
}
///
/// 执行请求
///
/// 请求url
/// 请求内容
/// 请求header
/// 返回服务器响应内容
protected virtual string DoExecute(string url, RequestForm requestForm, Dictionary header)
{
return openRequest.Request(this.url, requestForm, header);
}
///
/// 解析返回结果
///
/// 返回的Response
/// 服务器响应内容
/// 请求Request
/// 返回Response
protected virtual T ParseResponse(string resp, BaseRequest request) where T: BaseResponse {
string method = request.Method;
string dataName = this.dataNameBuilder.Build(method);
Dictionary jsonObject = JsonUtil.ParseToDictionary(resp);
bool errorResponse = jsonObject.ContainsKey(ERROR_RESPONSE_KEY);
if (errorResponse)
{
dataName = ERROR_RESPONSE_KEY;
}
object data = jsonObject[dataName];
string jsonData = data == null ? "{}" : data.ToString();
T t = JsonUtil.ParseObject(jsonData);
t.Body = jsonData;
return t;
}
}
}