using Apewer; using System; using System.Collections.Generic; using System.Text; namespace Apewer.Models { /// <summary>WebAPI 请求。T 必须带有 System.Serializable 特性。</summary> [Serializable] internal sealed class ApiRequest<T> { private string _application = null; private string _function = null; private string _random = Guid.NewGuid().ToString("n"); private string _ticket = null; private T _data = default(T); /// <summary>创建 WebAPI 请求。</summary> public ApiRequest(string application = null, string function = null, T data = default(T)) { _application = application; _function = function; _data = data; } /// <summary>请求的 Application。</summary> public string Application { get { return _application; } set { _application = value; } } /// <summary>请求的 Function。</summary> public string Function { get { return _function; } set { _function = value; } } /// <summary>请求的 Random,服务端将原样返回。</summary> public string Ramdom { get { return _random; } set { _random = value; } } /// <summary>用户 Ticket。</summary> public string Ticket { get { return _ticket; } set { _ticket = value; } } /// <summary></summary> public T Data { get { return _data; } set { _data = value; } } /// <summary>生成 Json 对象。</summary> public Json ToJson() { var json = Json.NewObject(); json.SetProperty("application", Application); json.SetProperty("function", Function); json.SetProperty("random", Ramdom); json.SetProperty("ticket", Ticket); json.SetProperty("data", Json.Parse(Data, true)); return json; } /// <summary>生成 Json 字符串,可指定 Json 缩进。</summary> public string ToString(bool indented) { return ToJson().ToString(indented); } /// <summary>生成 Json 字符串,默认不缩进。</summary> public override string ToString() { return ToJson().ToString(false); } } /// <summary>WebAPI 请求。</summary> [Serializable] public sealed class ApiRequest { private string _application = null; private string _function = null; private string _random = Guid.NewGuid().ToString("n"); private string _ticket = null; private Json _data = Json.NewObject(); /// <summary>创建 WebAPI 请求。</summary> public ApiRequest(string application = null, string function = null, object data = null) { _application = application; _function = function; _data = data == null ? Json.NewObject() : Json.Parse(data); } /// <summary>请求的 Application。</summary> public string Application { get { return _application; } set { _application = value; } } /// <summary>请求的 Function。</summary> public string Function { get { return _function; } set { _function = value; } } /// <summary>请求的 Random,服务端将原样返回。</summary> public string Ramdom { get { return _random; } set { _random = value; } } /// <summary>用户 Ticket。</summary> public string Ticket { get { return _ticket; } set { _ticket = value; } } /// <summary></summary> public Json Data { get { return _data; } set { _data = value; } } /// <summary>生成 Json 对象。</summary> public Json ToJson() { var json = Json.NewObject(); json.SetProperty("application", Application); json.SetProperty("function", Function); json.SetProperty("random", Ramdom); json.SetProperty("ticket", Ticket); json.SetProperty("data", Data); return json; } /// <summary>生成 Json 字符串,可指定 Json 缩进。</summary> public string ToString(bool indented) { return ToJson().ToString(indented); } /// <summary>生成 Json 字符串,默认不缩进。</summary> public override string ToString() { return ToJson().ToString(false); } } }