Client.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. // This file is auto-generated, don't edit it. Thanks.
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Threading.Tasks;
  7. using Tea;
  8. using Tea.Utils;
  9. using Alipay.EasySDK.Security.TextRisk.Models;
  10. namespace Alipay.EasySDK.Security.TextRisk
  11. {
  12. public class Client
  13. {
  14. protected Alipay.EasySDK.Kernel.Client _kernel;
  15. public Client(Alipay.EasySDK.Kernel.Client kernel)
  16. {
  17. this._kernel = kernel;
  18. }
  19. public AlipaySecurityRiskContentDetectResponse Detect(string content)
  20. {
  21. Dictionary<string, object> runtime_ = new Dictionary<string, object>
  22. {
  23. {"connectTimeout", 15000},
  24. {"readTimeout", 15000},
  25. {"retry", new Dictionary<string, int?>
  26. {
  27. {"maxAttempts", 0},
  28. }},
  29. };
  30. TeaRequest _lastRequest = null;
  31. Exception _lastException = null;
  32. long _now = System.DateTime.Now.Millisecond;
  33. int _retryTimes = 0;
  34. while (TeaCore.AllowRetry((IDictionary) runtime_["retry"], _retryTimes, _now))
  35. {
  36. if (_retryTimes > 0)
  37. {
  38. int backoffTime = TeaCore.GetBackoffTime((IDictionary)runtime_["backoff"], _retryTimes);
  39. if (backoffTime > 0)
  40. {
  41. TeaCore.Sleep(backoffTime);
  42. }
  43. }
  44. _retryTimes = _retryTimes + 1;
  45. try
  46. {
  47. TeaRequest request_ = new TeaRequest();
  48. Dictionary<string, string> systemParams = new Dictionary<string, string>
  49. {
  50. {"method", "alipay.security.risk.content.detect"},
  51. {"app_id", this._kernel.GetConfig("appId")},
  52. {"timestamp", this._kernel.GetTimestamp()},
  53. {"format", "json"},
  54. {"version", "1.0"},
  55. {"alipay_sdk", this._kernel.GetSdkVersion()},
  56. {"charset", "UTF-8"},
  57. {"sign_type", this._kernel.GetConfig("signType")},
  58. {"app_cert_sn", this._kernel.GetMerchantCertSN()},
  59. {"alipay_root_cert_sn", this._kernel.GetAlipayRootCertSN()},
  60. };
  61. Dictionary<string, object> bizParams = new Dictionary<string, object>
  62. {
  63. {"content", content},
  64. };
  65. Dictionary<string, string> textParams = new Dictionary<string, string>(){};
  66. request_.Protocol = this._kernel.GetConfig("protocol");
  67. request_.Method = "POST";
  68. request_.Pathname = "/gateway.do";
  69. request_.Headers = new Dictionary<string, string>
  70. {
  71. {"host", this._kernel.GetConfig("gatewayHost")},
  72. {"content-type", "application/x-www-form-urlencoded;charset=utf-8"},
  73. };
  74. request_.Query = this._kernel.SortMap(TeaConverter.merge<string>
  75. (
  76. new Dictionary<string, string>()
  77. {
  78. {"sign", this._kernel.Sign(systemParams, bizParams, textParams, this._kernel.GetConfig("merchantPrivateKey"))},
  79. },
  80. systemParams,
  81. textParams
  82. ));
  83. request_.Body = TeaCore.BytesReadable(this._kernel.ToUrlEncodedRequestBody(bizParams));
  84. _lastRequest = request_;
  85. TeaResponse response_ = TeaCore.DoAction(request_, runtime_);
  86. Dictionary<string, object> respMap = this._kernel.ReadAsJson(response_, "alipay.security.risk.content.detect");
  87. if (this._kernel.IsCertMode())
  88. {
  89. if (this._kernel.Verify(respMap, this._kernel.ExtractAlipayPublicKey(this._kernel.GetAlipayCertSN(respMap))))
  90. {
  91. return TeaModel.ToObject<AlipaySecurityRiskContentDetectResponse>(this._kernel.ToRespModel(respMap));
  92. }
  93. }
  94. else
  95. {
  96. if (this._kernel.Verify(respMap, this._kernel.GetConfig("alipayPublicKey")))
  97. {
  98. return TeaModel.ToObject<AlipaySecurityRiskContentDetectResponse>(this._kernel.ToRespModel(respMap));
  99. }
  100. }
  101. throw new TeaException(new Dictionary<string, string>
  102. {
  103. {"message", "验签失败,请检查支付宝公钥设置是否正确。"},
  104. });
  105. }
  106. catch (Exception e)
  107. {
  108. if (TeaCore.IsRetryable(e))
  109. {
  110. _lastException = e;
  111. continue;
  112. }
  113. throw e;
  114. }
  115. }
  116. throw new TeaUnretryableException(_lastRequest, _lastException);
  117. }
  118. public async Task<AlipaySecurityRiskContentDetectResponse> DetectAsync(string content)
  119. {
  120. Dictionary<string, object> runtime_ = new Dictionary<string, object>
  121. {
  122. {"connectTimeout", 15000},
  123. {"readTimeout", 15000},
  124. {"retry", new Dictionary<string, int?>
  125. {
  126. {"maxAttempts", 0},
  127. }},
  128. };
  129. TeaRequest _lastRequest = null;
  130. Exception _lastException = null;
  131. long _now = System.DateTime.Now.Millisecond;
  132. int _retryTimes = 0;
  133. while (TeaCore.AllowRetry((IDictionary) runtime_["retry"], _retryTimes, _now))
  134. {
  135. if (_retryTimes > 0)
  136. {
  137. int backoffTime = TeaCore.GetBackoffTime((IDictionary)runtime_["backoff"], _retryTimes);
  138. if (backoffTime > 0)
  139. {
  140. TeaCore.Sleep(backoffTime);
  141. }
  142. }
  143. _retryTimes = _retryTimes + 1;
  144. try
  145. {
  146. TeaRequest request_ = new TeaRequest();
  147. Dictionary<string, string> systemParams = new Dictionary<string, string>
  148. {
  149. {"method", "alipay.security.risk.content.detect"},
  150. {"app_id", this._kernel.GetConfig("appId")},
  151. {"timestamp", this._kernel.GetTimestamp()},
  152. {"format", "json"},
  153. {"version", "1.0"},
  154. {"alipay_sdk", this._kernel.GetSdkVersion()},
  155. {"charset", "UTF-8"},
  156. {"sign_type", this._kernel.GetConfig("signType")},
  157. {"app_cert_sn", this._kernel.GetMerchantCertSN()},
  158. {"alipay_root_cert_sn", this._kernel.GetAlipayRootCertSN()},
  159. };
  160. Dictionary<string, object> bizParams = new Dictionary<string, object>
  161. {
  162. {"content", content},
  163. };
  164. Dictionary<string, string> textParams = new Dictionary<string, string>(){};
  165. request_.Protocol = this._kernel.GetConfig("protocol");
  166. request_.Method = "POST";
  167. request_.Pathname = "/gateway.do";
  168. request_.Headers = new Dictionary<string, string>
  169. {
  170. {"host", this._kernel.GetConfig("gatewayHost")},
  171. {"content-type", "application/x-www-form-urlencoded;charset=utf-8"},
  172. };
  173. request_.Query = this._kernel.SortMap(TeaConverter.merge<string>
  174. (
  175. new Dictionary<string, string>()
  176. {
  177. {"sign", this._kernel.Sign(systemParams, bizParams, textParams, this._kernel.GetConfig("merchantPrivateKey"))},
  178. },
  179. systemParams,
  180. textParams
  181. ));
  182. request_.Body = TeaCore.BytesReadable(this._kernel.ToUrlEncodedRequestBody(bizParams));
  183. _lastRequest = request_;
  184. TeaResponse response_ = await TeaCore.DoActionAsync(request_, runtime_);
  185. Dictionary<string, object> respMap = await this._kernel.ReadAsJsonAsync(response_, "alipay.security.risk.content.detect");
  186. if (this._kernel.IsCertMode())
  187. {
  188. if (this._kernel.Verify(respMap, this._kernel.ExtractAlipayPublicKey(this._kernel.GetAlipayCertSN(respMap))))
  189. {
  190. return TeaModel.ToObject<AlipaySecurityRiskContentDetectResponse>(this._kernel.ToRespModel(respMap));
  191. }
  192. }
  193. else
  194. {
  195. if (this._kernel.Verify(respMap, this._kernel.GetConfig("alipayPublicKey")))
  196. {
  197. return TeaModel.ToObject<AlipaySecurityRiskContentDetectResponse>(this._kernel.ToRespModel(respMap));
  198. }
  199. }
  200. throw new TeaException(new Dictionary<string, string>
  201. {
  202. {"message", "验签失败,请检查支付宝公钥设置是否正确。"},
  203. });
  204. }
  205. catch (Exception e)
  206. {
  207. if (TeaCore.IsRetryable(e))
  208. {
  209. _lastException = e;
  210. continue;
  211. }
  212. throw e;
  213. }
  214. }
  215. throw new TeaUnretryableException(_lastRequest, _lastException);
  216. }
  217. /// <summary>
  218. /// ISV代商户代用,指定appAuthToken
  219. /// </summary>
  220. /// <param name="appAuthToken">代调用token</param>
  221. /// <returns>本客户端,便于链式调用</returns>
  222. public Client Agent(string appAuthToken)
  223. {
  224. _kernel.InjectTextParam("app_auth_token", appAuthToken);
  225. return this;
  226. }
  227. /// <summary>
  228. /// 用户授权调用,指定authToken
  229. /// </summary>
  230. /// <param name="authToken">用户授权token</param>
  231. /// <returns>本客户端,便于链式调用</returns>
  232. public Client Auth(string authToken)
  233. {
  234. _kernel.InjectTextParam("auth_token", authToken);
  235. return this;
  236. }
  237. /// <summary>
  238. /// 设置异步通知回调地址,此处设置将在本调用中覆盖Config中的全局配置
  239. /// </summary>
  240. /// <param name="url">异步通知回调地址,例如:https://www.test.com/callback </param>
  241. /// <returns>本客户端,便于链式调用</returns>
  242. public Client AsyncNotify(string url)
  243. {
  244. _kernel.InjectTextParam("notify_url", url);
  245. return this;
  246. }
  247. /// <summary>
  248. /// 将本次调用强制路由到后端系统的测试地址上,常用于线下环境内外联调,沙箱与线上环境设置无效
  249. /// </summary>
  250. /// <param name="testUrl">后端系统测试地址</param>
  251. /// <returns>本客户端,便于链式调用</returns>
  252. public Client Route(string testUrl)
  253. {
  254. _kernel.InjectTextParam("ws_service_url", testUrl);
  255. return this;
  256. }
  257. /// <summary>
  258. /// 设置API入参中没有的其他可选业务请求参数(biz_content下的字段)
  259. /// </summary>
  260. /// <param name="key">业务请求参数名称(biz_content下的字段名,比如timeout_express)</param>
  261. /// <param name="value">
  262. /// 业务请求参数的值,一个可以序列化成JSON的对象
  263. /// 如果该字段是一个字符串类型(String、Price、Date在SDK中都是字符串),请使用string储存
  264. /// 如果该字段是一个数值型类型(比如:Number),请使用long储存
  265. /// 如果该字段是一个复杂类型,请使用嵌套的Dictionary指定各下级字段的值
  266. /// 如果该字段是一个数组,请使用List储存各个值
  267. /// 对于更复杂的情况,也支持Dictionary和List的各种组合嵌套,比如参数是值是个List,List中的每种类型是一个复杂对象
  268. /// </param>
  269. /// <returns>本客户端,便于链式调用</returns>
  270. public Client Optional(string key, object value)
  271. {
  272. _kernel.InjectBizParam(key, value);
  273. return this;
  274. }
  275. /// <summary>
  276. /// 批量设置API入参中没有的其他可选业务请求参数(biz_content下的字段)
  277. /// optional方法的批量版本
  278. /// </summary>
  279. /// <param name="optionalArgs">可选参数集合,每个参数由key和value组成,key和value的格式请参见optional方法的注释</param>
  280. /// <returns>本客户端,便于链式调用</returns>
  281. public Client BatchOptional(Dictionary<string, object> optionalArgs)
  282. {
  283. foreach (var pair in optionalArgs)
  284. {
  285. _kernel.InjectBizParam(pair.Key, pair.Value);
  286. }
  287. return this;
  288. }
  289. }
  290. }