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 Token
{
private static readonly string ERR_MSG_SDK_RUNTIME_ERROR = "OpenAPI-SDK Runtime Error.";
private IDictionary systemParameters = new Dictionary();
private string appKey;
private string appSecret;
private string fromAccount;
private string baseUrl;
public static Http.WebUtils Client = new Http.WebUtils();
public Token()
{
try
{
this.LoadConfig();
}
catch (Exception e)
{
LogHelper.Error(e.Message);
throw new ApiException(ERR_MSG_SDK_RUNTIME_ERROR, e);
}
}
public Token(string fromAccount, string appKey, string appSecret)
: this()
{
this.fromAccount = fromAccount;
this.appKey = appKey;
this.appSecret = appSecret;
}
private void LoadConfig()
{
this.appKey = ApiConfig.AppKey;
this.appSecret = ApiConfig.AppSecret;
this.fromAccount = ApiConfig.FromAccount;
this.baseUrl = ApiConfig.BaseUrl;
}
private IDictionary GetSystemParameters()
{
if (this.systemParameters.Count == 0)
{
this.systemParameters.Add("app_key", this.appKey);
if (!String.IsNullOrEmpty(this.appSecret))
this.systemParameters.Add("app_secret", this.appSecret);
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 "token"; }
}
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);
}
}
}
}