Api.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using cuidian.OpenApi.Exceptions;
  7. using cuidian.OpenApi.Utils;
  8. using Logs;
  9. namespace cuidian.OpenApi.Api
  10. {
  11. public abstract class Api
  12. {
  13. protected static readonly string ERR_MSG_SDK_RUNTIME_ERROR = "OpenAPI-SDK Runtime Error.";
  14. protected const int MAX_TIMES_TRADEID_RETRY = 20;
  15. private IDictionary<string, string> systemParameters = new Dictionary<string, string>();
  16. private string resourceId;
  17. private string appKey;
  18. private string fromAccount;
  19. private string toAccount;
  20. private string baseUrl;
  21. private string method;
  22. public static Http.WebUtils Client = new Http.WebUtils();
  23. public Api(string resourceId)
  24. {
  25. try
  26. {
  27. this.resourceId = resourceId;
  28. this.LoadConfig();
  29. }
  30. catch (Exception e)
  31. {
  32. LogHelper.Error(e.Message);
  33. throw new ApiException(ERR_MSG_SDK_RUNTIME_ERROR, e);
  34. }
  35. }
  36. public Api(string resourceId, string fromAccount, string toAccount)
  37. : this(resourceId)
  38. {
  39. this.fromAccount = fromAccount;
  40. this.toAccount = toAccount;
  41. }
  42. public Api(string resourceId, string fromAccount, string toAccount, string appKey)
  43. : this(resourceId, fromAccount, toAccount)
  44. {
  45. this.appKey = appKey;
  46. }
  47. public IDictionary<string, string> SystemParameters { get { return this.systemParameters; } }
  48. protected IDictionary<string, string> GetSystemParameters()
  49. {
  50. if (this.systemParameters.Count == 0)
  51. {
  52. this.systemParameters.Add("app_key", this.AppKey);
  53. if (!String.IsNullOrEmpty(this.FromAccount))
  54. this.systemParameters.Add("from_account", this.FromAccount);
  55. if (!String.IsNullOrEmpty(this.ToAccount))
  56. this.systemParameters.Add("to_account", this.ToAccount);
  57. }
  58. if (this.systemParameters.ContainsKey("token"))
  59. {
  60. this.systemParameters["token"] = TokenManager.GetToken();
  61. }
  62. else
  63. {
  64. this.systemParameters.Add("token", TokenManager.GetToken());
  65. }
  66. return this.systemParameters;
  67. }
  68. protected IDictionary<string, string> CombineParameters(IDictionary<string, string> systemParameters,
  69. IDictionary<string, string> apiParameters)
  70. {
  71. IDictionary<string, string> parameters = new Dictionary<string, string>();
  72. foreach (KeyValuePair<string, string> entry in systemParameters)
  73. {
  74. parameters.Add(entry);
  75. }
  76. if (apiParameters != null)
  77. {
  78. foreach (KeyValuePair<string, string> entry in apiParameters)
  79. {
  80. parameters.Add(entry);
  81. }
  82. }
  83. return parameters;
  84. }
  85. private void LoadConfig()
  86. {
  87. this.appKey = ApiConfig.AppKey;
  88. this.fromAccount = ApiConfig.FromAccount;
  89. this.toAccount = ApiConfig.ToAccount;
  90. this.baseUrl = ApiConfig.BaseUrl;
  91. }
  92. public string AppKey
  93. {
  94. get { return appKey; }
  95. }
  96. public string ToAccount
  97. {
  98. get { return toAccount; }
  99. }
  100. public string FromAccount
  101. {
  102. get { return fromAccount; }
  103. }
  104. public string Method
  105. {
  106. get
  107. {
  108. return method;
  109. }
  110. protected set
  111. {
  112. method = value;
  113. }
  114. }
  115. protected string BaseUrl { get { return this.baseUrl; } }
  116. public string Url
  117. {
  118. get
  119. {
  120. if (this.Method.Equals("list"))
  121. {
  122. return this.baseUrl + "api/" + this.resourceId + "/batch_get";
  123. }
  124. else
  125. {
  126. return this.baseUrl + "api/" + this.resourceId + "/" + this.method;
  127. }
  128. }
  129. }
  130. /// <summary>
  131. /// 资源名称
  132. /// </summary>
  133. public string ResourceId
  134. {
  135. get
  136. {
  137. if (this.Method.Equals("list"))
  138. {
  139. return this.resourceId + "list";
  140. }
  141. else
  142. {
  143. return this.resourceId;
  144. }
  145. }
  146. protected set
  147. {
  148. this.resourceId = value;
  149. }
  150. }
  151. }
  152. }