Token.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 Token
  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 appSecret;
  21. private string fromAccount;
  22. private string baseUrl;
  23. public static Http.WebUtils Client = new Http.WebUtils();
  24. public Token()
  25. {
  26. try
  27. {
  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 Token(string fromAccount, string appKey, string appSecret)
  37. : this()
  38. {
  39. this.fromAccount = fromAccount;
  40. this.appKey = appKey;
  41. this.appSecret = appSecret;
  42. }
  43. private void LoadConfig()
  44. {
  45. this.appKey = ApiConfig.AppKey;
  46. this.appSecret = ApiConfig.AppSecret;
  47. this.fromAccount = ApiConfig.FromAccount;
  48. this.baseUrl = ApiConfig.BaseUrl;
  49. }
  50. private IDictionary<string, string> GetSystemParameters()
  51. {
  52. if (this.systemParameters.Count == 0)
  53. {
  54. this.systemParameters.Add("app_key", this.appKey);
  55. if (!String.IsNullOrEmpty(this.appSecret))
  56. this.systemParameters.Add("app_secret", this.appSecret);
  57. if (!String.IsNullOrEmpty(this.fromAccount))
  58. this.systemParameters.Add("from_account", this.fromAccount);
  59. }
  60. return this.systemParameters;
  61. }
  62. private string Url
  63. {
  64. get { return this.baseUrl + "system/" + this.ResourceId; }
  65. }
  66. /// <summary>
  67. /// 资源名称
  68. /// </summary>
  69. public string ResourceId
  70. {
  71. get { return "token"; }
  72. }
  73. public BusinessObject Get()
  74. {
  75. try
  76. {
  77. return BusinessObject.Get(this.ResourceId, new Response(Client.Get(this.Url, this.GetSystemParameters())));
  78. }
  79. catch (Exception e)
  80. {
  81. LogHelper.Error(e.Message);
  82. throw new ApiException(ERR_MSG_SDK_RUNTIME_ERROR, e);
  83. }
  84. }
  85. }
  86. }