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/Request/BaseRequest.cs

129 lines
3.8 KiB

6 years ago
using System;
using System.Collections.Generic;
6 years ago
using System.Text;
6 years ago
using SDKCSharp.Common;
using SDKCSharp.Utility;
namespace SDKCSharp.Request
{
/// <summary>
/// 接口请求对象,新建的Request要继承这个类
/// </summary>
/// <typeparam name="T">对应的Response对象</typeparam>
public abstract class BaseRequest<T>
{
private string method;
private string format = SdkConfig.FORMAT_TYPE;
6 years ago
private Encoding charset = SdkConfig.CHARSET;
private SignType signType = SdkConfig.SIGN_TYPE;
6 years ago
private string timestamp = DateTime.Now.ToString(SdkConfig.TIMESTAMP_PATTERN);
private string version;
private string bizContent;
private object bizModel;
private List<UploadFile> files;
public string Method { get => method; }
public List<UploadFile> Files { set => files = value; }
public string BizContent { set => bizContent = value; }
public object BizModel { set => bizModel = value; }
public string Version { get => version; set => version = value; }
6 years ago
public Encoding Charset { get => charset; set => charset = value; }
public SignType SignType { get => signType; set => signType = value; }
6 years ago
/// <summary>
/// 返回接口名
/// </summary>
/// <returns></returns>
public abstract string GetMethod();
/// <summary>
/// 返回版本号
/// </summary>
/// <returns></returns>
public virtual string GetVersion()
{
return SdkConfig.DEFAULT_VERSION;
}
/// <summary>
/// 指定HTTP请求method,默认POST
/// </summary>
/// <returns>The request method.</returns>
public virtual RequestMethod GetRequestMethod()
{
return RequestMethod.POST;
}
6 years ago
public BaseRequest()
{
this.method = this.GetMethod();
this.version = this.GetVersion();
}
protected BaseRequest(string name, string version)
{
this.method = name;
this.version = version == null ? SdkConfig.DEFAULT_VERSION : version;
}
6 years ago
/// <summary>
/// 添加上传文件
/// </summary>
/// <param name="file">File.</param>
public void AddFile(UploadFile file)
{
if(this.files == null)
{
this.files = new List<UploadFile>();
}
this.files.Add(file);
}
6 years ago
/// <summary>
/// 创建请求表单
/// </summary>
/// <returns></returns>
public RequestForm CreateRequestForm(OpenConfig openConfig)
{
Dictionary<string, string> dict = new Dictionary<string, string>();
dict[openConfig.MethodName] = this.Method;
dict[openConfig.FormatName] = this.format;
6 years ago
dict[openConfig.CharsetName] = this.charset.BodyName;
dict[openConfig.SignTypeName] = this.signType.ToString();
6 years ago
dict[openConfig.TimestampName] = this.timestamp;
dict[openConfig.VersionName] = this.version;
// 业务参数
6 years ago
String biz_content = BuildBizContent();
6 years ago
dict[openConfig.DataName] = biz_content;
RequestForm requestForm = new RequestForm(dict);
requestForm.Charset = this.charset;
requestForm.RequestMethod = GetRequestMethod();
6 years ago
requestForm.Files = this.files;
return requestForm;
}
6 years ago
protected string BuildBizContent()
6 years ago
{
if (bizModel != null)
{
return JsonUtil.ToJSONString(bizModel);
}
else
{
return this.bizContent;
}
}
}
}