123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using cuidian.OpenApi.Exceptions;
- using cuidian.OpenApi.Utils;
- using Logs;
- namespace cuidian.OpenApi.Api
- {
- public abstract class Api
- {
- protected static readonly string ERR_MSG_SDK_RUNTIME_ERROR = "OpenAPI-SDK Runtime Error.";
- protected const int MAX_TIMES_TRADEID_RETRY = 20;
- private IDictionary<string, string> systemParameters = new Dictionary<string, string>();
- private string resourceId;
- private string appKey;
- private string fromAccount;
- private string toAccount;
- private string baseUrl;
- private string method;
- public static Http.WebUtils Client = new Http.WebUtils();
- public Api(string resourceId)
- {
- try
- {
- this.resourceId = resourceId;
- this.LoadConfig();
- }
- catch (Exception e)
- {
- LogHelper.Error(e.Message);
- throw new ApiException(ERR_MSG_SDK_RUNTIME_ERROR, e);
- }
- }
- public Api(string resourceId, string fromAccount, string toAccount)
- : this(resourceId)
- {
- this.fromAccount = fromAccount;
- this.toAccount = toAccount;
- }
- public Api(string resourceId, string fromAccount, string toAccount, string appKey)
- : this(resourceId, fromAccount, toAccount)
- {
- this.appKey = appKey;
- }
- public IDictionary<string, string> SystemParameters { get { return this.systemParameters; } }
- protected IDictionary<string, string> GetSystemParameters()
- {
- if (this.systemParameters.Count == 0)
- {
- this.systemParameters.Add("app_key", this.AppKey);
- if (!String.IsNullOrEmpty(this.FromAccount))
- this.systemParameters.Add("from_account", this.FromAccount);
- if (!String.IsNullOrEmpty(this.ToAccount))
- this.systemParameters.Add("to_account", this.ToAccount);
- }
- if (this.systemParameters.ContainsKey("token"))
- {
- this.systemParameters["token"] = TokenManager.GetToken();
- }
- else
- {
- this.systemParameters.Add("token", TokenManager.GetToken());
- }
- return this.systemParameters;
- }
- protected IDictionary<string, string> CombineParameters(IDictionary<string, string> systemParameters,
- IDictionary<string, string> apiParameters)
- {
- IDictionary<string, string> parameters = new Dictionary<string, string>();
- foreach (KeyValuePair<string, string> entry in systemParameters)
- {
- parameters.Add(entry);
- }
- if (apiParameters != null)
- {
- foreach (KeyValuePair<string, string> entry in apiParameters)
- {
- parameters.Add(entry);
- }
- }
- return parameters;
- }
- private void LoadConfig()
- {
- this.appKey = ApiConfig.AppKey;
- this.fromAccount = ApiConfig.FromAccount;
- this.toAccount = ApiConfig.ToAccount;
- this.baseUrl = ApiConfig.BaseUrl;
- }
- public string AppKey
- {
- get { return appKey; }
- }
- public string ToAccount
- {
- get { return toAccount; }
- }
- public string FromAccount
- {
- get { return fromAccount; }
- }
- public string Method
- {
- get
- {
- return method;
- }
- protected set
- {
- method = value;
- }
- }
- protected string BaseUrl { get { return this.baseUrl; } }
- public string Url
- {
- get
- {
- if (this.Method.Equals("list"))
- {
- return this.baseUrl + "api/" + this.resourceId + "/batch_get";
- }
- else
- {
- return this.baseUrl + "api/" + this.resourceId + "/" + this.method;
- }
- }
- }
- /// <summary>
- /// 资源名称
- /// </summary>
- public string ResourceId
- {
- get
- {
- if (this.Method.Equals("list"))
- {
- return this.resourceId + "list";
- }
- else
- {
- return this.resourceId;
- }
- }
- protected set
- {
- this.resourceId = value;
- }
- }
- }
- }
|