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