123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- 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
- {
- /// <summary>
- /// 开放平台 API 基类
- /// </summary>
- public class Token
- {
- private static readonly string ERR_MSG_SDK_RUNTIME_ERROR = "OpenAPI-SDK Runtime Error.";
-
- private IDictionary<string, string> systemParameters = new Dictionary<string, string>();
- 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<string, string> 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; }
- }
- /// <summary>
- /// 资源名称
- /// </summary>
- 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);
- }
- }
- }
- }
|