CosXmlConfig.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using COSXML.Network;
  5. namespace COSXML
  6. {
  7. /// <summary>
  8. /// COSXML 服务配置类
  9. /// </summary>
  10. public sealed class CosXmlConfig
  11. {
  12. private HttpClientConfig httpConfig;
  13. private string appid;
  14. private string region;
  15. private bool isHttps = true;
  16. private bool isDebug;
  17. /// <summary>
  18. /// 读取 Endpoint 后缀
  19. /// </summary>
  20. /// <value></value>
  21. public string endpointSuffix { get; private set; }
  22. /// <summary>
  23. /// 获取完整请求域名
  24. /// </summary>
  25. /// <value></value>
  26. public string host { get; private set; }
  27. private CosXmlConfig(Builder builder)
  28. {
  29. this.appid = builder.appid;
  30. this.region = builder.region;
  31. this.isHttps = builder.isHttps;
  32. this.httpConfig = builder.httpClientConfigBuilder.Build();
  33. this.isDebug = builder.isDebug;
  34. this.endpointSuffix = builder.endpointSuffix;
  35. this.host = builder.host;
  36. }
  37. /// <summary>
  38. /// 获取 AppID
  39. /// </summary>
  40. /// <value></value>
  41. public string Appid
  42. {
  43. get
  44. {
  45. return appid;
  46. }
  47. }
  48. /// <summary>
  49. /// 获取 Region
  50. /// </summary>
  51. /// <value></value>
  52. public string Region
  53. {
  54. get
  55. {
  56. return region;
  57. }
  58. }
  59. /// <summary>
  60. /// 获取是否开启 Https
  61. /// </summary>
  62. /// <value></value>
  63. public bool IsHttps
  64. {
  65. get
  66. {
  67. return isHttps;
  68. }
  69. }
  70. /// <summary>
  71. /// 获取 HttpClient 配置
  72. /// </summary>
  73. /// <value></value>
  74. public HttpClientConfig HttpConfig
  75. {
  76. get
  77. {
  78. return httpConfig;
  79. }
  80. }
  81. /// <summary>
  82. /// 获取是否开启 DEBUG 日志
  83. /// </summary>
  84. /// <value></value>
  85. public bool IsDebugLog
  86. {
  87. get
  88. {
  89. return isDebug;
  90. }
  91. }
  92. /// <summary>
  93. /// Config 构造器
  94. /// </summary>
  95. public sealed class Builder
  96. {
  97. internal string appid;
  98. internal string region;
  99. internal bool isHttps = true;
  100. internal HttpClientConfig.Builder httpClientConfigBuilder;
  101. internal bool isDebug = false;
  102. internal string endpointSuffix;
  103. internal string host;
  104. /// <summary>
  105. /// 初始化一个构造器
  106. /// </summary>
  107. public Builder()
  108. {
  109. httpClientConfigBuilder = new HttpClientConfig.Builder();
  110. }
  111. /// <summary>
  112. /// cos 服务的Appid
  113. /// </summary>
  114. /// <param name="appid"></param>
  115. /// <returns></returns>
  116. public Builder SetAppid(string appid)
  117. {
  118. this.appid = appid;
  119. return this;
  120. }
  121. /// <summary>
  122. /// 存储桶所属地域
  123. /// </summary>
  124. /// <param name="region"></param>
  125. /// <returns></returns>
  126. public Builder SetRegion(string region)
  127. {
  128. //region cannot be empty
  129. if(region == null || region == "") {
  130. throw new CosException.CosClientException(
  131. (int)COSXML.Common.CosClientError.InvalidArgument,
  132. "region cannot be empty"
  133. );
  134. }
  135. this.region = region;
  136. return this;
  137. }
  138. /// <summary>
  139. /// true:https请求
  140. /// </summary>
  141. /// <param name="isHttps"></param>
  142. /// <returns></returns>
  143. public Builder IsHttps(bool isHttps)
  144. {
  145. this.isHttps = isHttps;
  146. return this;
  147. }
  148. /// <summary>
  149. /// 设置最大连接数,默认值 512
  150. /// </summary>
  151. /// <param name="connectionLimit"></param>
  152. /// <returns></returns>
  153. public Builder SetConnectionLimit(int connectionLimit)
  154. {
  155. this.httpClientConfigBuilder.SetConnectionLimit(connectionLimit);
  156. return this;
  157. }
  158. /// <summary>
  159. /// 设置 TCP 连接超时时间,单位是毫秒,默认 45 秒
  160. /// </summary>
  161. /// <param name="connectionTimeoutMs"></param>
  162. /// <returns></returns>
  163. public Builder SetConnectionTimeoutMs(int connectionTimeoutMs)
  164. {
  165. this.httpClientConfigBuilder.SetConnectionTimeoutMs(connectionTimeoutMs);
  166. return this;
  167. }
  168. /// <summary>
  169. /// 设置 TCP 连接读写时间,单位是毫秒,默认 45 秒
  170. /// </summary>
  171. /// <param name="readWriteTimeoutMs"></param>
  172. /// <returns></returns>
  173. public Builder SetReadWriteTimeoutMs(int readWriteTimeoutMs)
  174. {
  175. this.httpClientConfigBuilder.SetReadWriteTimeoutMs(readWriteTimeoutMs);
  176. return this;
  177. }
  178. /// <summary>
  179. /// 设置是否使用 Keep-Alive 长连接
  180. /// </summary>
  181. /// <param name="keepAlive"></param>
  182. /// <returns></returns>
  183. public Builder SetHttpKeepAlive(bool keepAlive)
  184. {
  185. this.httpClientConfigBuilder.SetHttpKeepAlive(keepAlive);
  186. return this;
  187. }
  188. /// <summary>
  189. /// 设置 HTTP 代理主机
  190. /// </summary>
  191. /// <param name="host"></param>
  192. /// <returns></returns>
  193. public Builder SetProxyHost(string host)
  194. {
  195. this.httpClientConfigBuilder.SetProxyHost(host);
  196. return this;
  197. }
  198. /// <summary>
  199. /// 设置 HTTP 代理端口
  200. /// </summary>
  201. /// <param name="port"></param>
  202. /// <returns></returns>
  203. public Builder SetProxyPort(int port)
  204. {
  205. this.httpClientConfigBuilder.SetProxyPort(port);
  206. return this;
  207. }
  208. /// <summary>
  209. /// 设置 HTTP 代理用户名
  210. /// </summary>
  211. /// <param name="userName"></param>
  212. /// <returns></returns>
  213. public Builder SetProxyUserName(string userName)
  214. {
  215. this.httpClientConfigBuilder.SetProxyUserName(userName);
  216. return this;
  217. }
  218. /// <summary>
  219. /// 设置 HTTP 代理用户密码
  220. /// </summary>
  221. /// <param name="password"></param>
  222. /// <returns></returns>
  223. public Builder SetProxyUserPassword(string password)
  224. {
  225. this.httpClientConfigBuilder.SetProxyUserPassword(password);
  226. return this;
  227. }
  228. /// <summary>
  229. /// 设置 HTTP 代理 Domain
  230. /// </summary>
  231. /// <param name="domain"></param>
  232. /// <returns></returns>
  233. public Builder SetProxyDomain(string domain)
  234. {
  235. this.httpClientConfigBuilder.SetProxyDomain(domain);
  236. return this;
  237. }
  238. /// <summary>
  239. /// 设置是否允许请求重定向
  240. /// </summary>
  241. /// <param name="isAllow"></param>
  242. /// <returns></returns>
  243. public Builder SetAllowAutoRedirect(bool isAllow)
  244. {
  245. this.httpClientConfigBuilder.AllowAutoRedirect(isAllow);
  246. return this;
  247. }
  248. /// <summary>
  249. /// 设置是否开启 DEBUG 日志
  250. /// </summary>
  251. /// <param name="isDebug"></param>
  252. /// <returns></returns>
  253. public Builder SetDebugLog(bool isDebug)
  254. {
  255. this.isDebug = isDebug;
  256. return this;
  257. }
  258. /// <summary>
  259. /// 设置 Endpoint 后缀,最终请求域名为 $Bucket.$EndpointSuffix
  260. /// </summary>
  261. /// <param name="suffix"></param>
  262. /// <returns></returns>
  263. public Builder SetEndpointSuffix(string suffix)
  264. {
  265. this.endpointSuffix = suffix;
  266. return this;
  267. }
  268. /// <summary>
  269. /// 设置完整请求域名
  270. /// </summary>
  271. /// <param name="host"></param>
  272. /// <returns></returns>
  273. public Builder SetHost(string host)
  274. {
  275. this.host = host;
  276. return this;
  277. }
  278. /// <summary>
  279. /// 构建 Config
  280. /// </summary>
  281. /// <returns></returns>
  282. public CosXmlConfig Build()
  283. {
  284. return new CosXmlConfig(this);
  285. }
  286. }
  287. }
  288. }