DefaultQimenClient.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using QimenCloud.Api.Parser;
  5. using Top.Api;
  6. using Top.Api.Parser;
  7. using Top.Api.Util;
  8. namespace Qimen.Api
  9. {
  10. public class DefaultQimenClient : IQimenClient
  11. {
  12. protected string serverUrl;
  13. protected string appKey;
  14. protected string appSecret;
  15. protected string format = Constants.FORMAT_XML;
  16. protected string signMethod = Constants.SIGN_METHOD_MD5;
  17. protected int connectTimeout = 15000; // 默认连接超时时间为15秒
  18. protected int readTimeout = 30000; // 默认响应超时时间为30秒
  19. private bool disableParser = false; // 禁用响应结果解释
  20. private bool disableTrace = false; // 禁用日志调试功能
  21. private bool useGzipEncoding = true; // 是否启用响应GZIP压缩
  22. protected WebUtils webUtils;
  23. protected ITopLogger topLogger;
  24. public DefaultQimenClient(string serverUrl, string appKey, string appSecret)
  25. {
  26. this.serverUrl = serverUrl;
  27. this.appKey = appKey;
  28. this.appSecret = appSecret;
  29. this.webUtils = new WebUtils();
  30. this.topLogger = Top.Api.Log.Instance;
  31. }
  32. public T Execute<T>(QimenRequest<T> request) where T : QimenResponse
  33. {
  34. return Execute(request, null);
  35. }
  36. public T Execute<T>(QimenRequest<T> request, string session) where T : QimenResponse
  37. {
  38. return DoExecute(request, session);
  39. }
  40. private T DoExecute<T>(QimenRequest<T> request, string session) where T : QimenResponse
  41. {
  42. long start = DateTime.Now.Ticks;
  43. // 添加协议级请求参数
  44. TopDictionary parameters = new TopDictionary();
  45. if (request.GetQueryParameters() != null)
  46. {
  47. parameters.AddAll(request.GetQueryParameters());
  48. }
  49. parameters.Add(Constants.METHOD, request.GetApiName());
  50. parameters.Add(Constants.VERSION, request.Version);
  51. parameters.Add(Constants.APP_KEY, appKey);
  52. parameters.Add(Constants.TIMESTAMP, request.Timestamp);
  53. parameters.Add(Constants.FORMAT, format);
  54. parameters.Add(Constants.SIGN_METHOD, signMethod);
  55. parameters.Add(Constants.SESSION, session);
  56. parameters.Add(Constants.PARTNER_ID, Constants.SDK_VERSION);
  57. parameters.Add(Constants.QM_CUSTOMER_ID, request.CustomerId);
  58. // 添加头部参数
  59. if (this.useGzipEncoding)
  60. {
  61. request.AddHeaderParameter(Constants.ACCEPT_ENCODING, Constants.CONTENT_ENCODING_GZIP);
  62. }
  63. try
  64. {
  65. string reqBody = request.Body;
  66. if (string.IsNullOrEmpty(reqBody))
  67. {
  68. XmlWriter writer = new XmlWriter(Constants.QM_ROOT_TAG_REQ, typeof(QimenRequest<T>));
  69. reqBody = writer.Write(request);
  70. }
  71. // 添加签名参数
  72. parameters.Add(Constants.SIGN, TopUtils.SignTopRequest(parameters, reqBody, appSecret, signMethod));
  73. string fullUrl = WebUtils.BuildRequestUrl(serverUrl, parameters);
  74. string rspBody = webUtils.DoPost(fullUrl, Encoding.UTF8.GetBytes(reqBody), Constants.QM_CONTENT_TYPE, request.GetHeaderParameters());
  75. // 解释响应结果
  76. T rsp = null;
  77. if (disableParser)
  78. {
  79. rsp = Activator.CreateInstance<T>();
  80. rsp.Body = rspBody;
  81. }
  82. else
  83. {
  84. if (Constants.FORMAT_XML.Equals(format))
  85. {
  86. ITopParser<T> tp = new QimenXmlParser<T>();
  87. rsp = tp.Parse(rspBody);
  88. }
  89. }
  90. // 追踪错误的请求
  91. if (rsp != null && rsp.IsError)
  92. {
  93. TimeSpan latency = new TimeSpan(DateTime.Now.Ticks - start);
  94. TraceApiError(appKey, request.GetApiName(), serverUrl, parameters, latency.TotalMilliseconds, rspBody);
  95. }
  96. return rsp;
  97. }
  98. catch (Exception e)
  99. {
  100. TimeSpan latency = new TimeSpan(DateTime.Now.Ticks - start);
  101. TraceApiError(appKey, request.GetApiName(), serverUrl, parameters, latency.TotalMilliseconds, e.GetType() + ": " + e.Message);
  102. throw e;
  103. }
  104. }
  105. public void SetTimeout(int timeout)
  106. {
  107. this.webUtils.Timeout = timeout;
  108. }
  109. public void SetReadWriteTimeout(int readWriteTimeout)
  110. {
  111. this.webUtils.ReadWriteTimeout = readWriteTimeout;
  112. }
  113. public void SetDisableParser(bool disableParser)
  114. {
  115. this.disableParser = disableParser;
  116. }
  117. public void SetDisableTrace(bool disableTrace)
  118. {
  119. this.disableTrace = disableTrace;
  120. }
  121. public void SetUseGzipEncoding(bool useGzipEncoding)
  122. {
  123. this.useGzipEncoding = useGzipEncoding;
  124. }
  125. public void SetIgnoreSSLCheck(bool ignore)
  126. {
  127. this.webUtils.IgnoreSSLCheck = ignore;
  128. }
  129. private void TraceApiError(string appKey, string apiName, string url, Dictionary<string, string> parameters, double latency, string errorMessage)
  130. {
  131. if (!disableTrace)
  132. {
  133. this.topLogger.TraceApiError(appKey, apiName, url, parameters, latency, errorMessage);
  134. }
  135. }
  136. }
  137. }