using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using cuidian.OpenApi.Exceptions;
using cuidian.OpenApi.Http;
using cuidian.OpenApi.Utils;
using Logs;
namespace cuidian.OpenApi
{
///
/// 开放平台 API 基类
///
public class Trade
{
private static readonly string ERR_MSG_SDK_RUNTIME_ERROR = "OpenAPI-SDK Runtime Error.";
private IDictionary systemParameters = new Dictionary();
private string appKey;
private string fromAccount;
private string baseUrl;
public static Http.WebUtils Client = new Http.WebUtils();
public Trade()
{
try
{
this.LoadConfig();
}
catch (Exception e)
{
LogHelper.Error(e.Message);
throw new ApiException(ERR_MSG_SDK_RUNTIME_ERROR, e);
}
}
public Trade(string fromAccount, string appKey, string appSecret)
: this()
{
this.fromAccount = fromAccount;
this.appKey = appKey;
}
private void LoadConfig()
{
this.appKey = ApiConfig.AppKey;
this.fromAccount = ApiConfig.FromAccount;
this.baseUrl = ApiConfig.BaseUrl;
}
private IDictionary GetSystemParameters()
{
if (this.systemParameters.Count == 0)
{
this.systemParameters.Add("app_key", this.appKey);
this.systemParameters.Add("token", TokenManager.GetToken());
if (!String.IsNullOrEmpty(this.fromAccount))
this.systemParameters.Add("from_account", this.fromAccount);
}
return this.systemParameters;
}
private string Url
{
get { return this.baseUrl + "system/" + this.ResourceId; }
}
///
/// 资源名称
///
public string ResourceId
{
get { return "tradeid"; }
}
public BusinessObject Get()
{
try
{
return BusinessObject.Get(this.ResourceId, new Response(Client.Get(this.Url, this.GetSystemParameters())));
}
catch (Exception e)
{
LogHelper.Error(e.Message);
throw new ApiException(ERR_MSG_SDK_RUNTIME_ERROR, e);
}
}
}
}