Trade.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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.Http;
  8. using cuidian.OpenApi.Utils;
  9. using Logs;
  10. namespace cuidian.OpenApi
  11. {
  12. /// <summary>
  13. /// 开放平台 API 基类
  14. /// </summary>
  15. public class Trade
  16. {
  17. private static readonly string ERR_MSG_SDK_RUNTIME_ERROR = "OpenAPI-SDK Runtime Error.";
  18. private IDictionary<string, string> systemParameters = new Dictionary<string, string>();
  19. private string appKey;
  20. private string fromAccount;
  21. private string baseUrl;
  22. public static Http.WebUtils Client = new Http.WebUtils();
  23. public Trade()
  24. {
  25. try
  26. {
  27. this.LoadConfig();
  28. }
  29. catch (Exception e)
  30. {
  31. LogHelper.Error(e.Message);
  32. throw new ApiException(ERR_MSG_SDK_RUNTIME_ERROR, e);
  33. }
  34. }
  35. public Trade(string fromAccount, string appKey, string appSecret)
  36. : this()
  37. {
  38. this.fromAccount = fromAccount;
  39. this.appKey = appKey;
  40. }
  41. private void LoadConfig()
  42. {
  43. this.appKey = ApiConfig.AppKey;
  44. this.fromAccount = ApiConfig.FromAccount;
  45. this.baseUrl = ApiConfig.BaseUrl;
  46. }
  47. private IDictionary<string, string> GetSystemParameters()
  48. {
  49. if (this.systemParameters.Count == 0)
  50. {
  51. this.systemParameters.Add("app_key", this.appKey);
  52. this.systemParameters.Add("token", TokenManager.GetToken());
  53. if (!String.IsNullOrEmpty(this.fromAccount))
  54. this.systemParameters.Add("from_account", this.fromAccount);
  55. }
  56. return this.systemParameters;
  57. }
  58. private string Url
  59. {
  60. get { return this.baseUrl + "system/" + this.ResourceId; }
  61. }
  62. /// <summary>
  63. /// 资源名称
  64. /// </summary>
  65. public string ResourceId
  66. {
  67. get { return "tradeid"; }
  68. }
  69. public BusinessObject Get()
  70. {
  71. try
  72. {
  73. return BusinessObject.Get(this.ResourceId, new Response(Client.Get(this.Url, this.GetSystemParameters())));
  74. }
  75. catch (Exception e)
  76. {
  77. LogHelper.Error(e.Message);
  78. throw new ApiException(ERR_MSG_SDK_RUNTIME_ERROR, e);
  79. }
  80. }
  81. }
  82. }