123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345 |
- using System;
- using System.Collections.Generic;
- using System.Text;
- using COSXML.Network;
- namespace COSXML
- {
- /// <summary>
- /// COSXML 服务配置类
- /// </summary>
- public sealed class CosXmlConfig
- {
- private HttpClientConfig httpConfig;
- private string appid;
- private string region;
- private bool isHttps = true;
- private bool isDebug;
- /// <summary>
- /// 读取 Endpoint 后缀
- /// </summary>
- /// <value></value>
- public string endpointSuffix { get; private set; }
- /// <summary>
- /// 获取完整请求域名
- /// </summary>
- /// <value></value>
- public string host { get; private set; }
- private CosXmlConfig(Builder builder)
- {
- this.appid = builder.appid;
- this.region = builder.region;
- this.isHttps = builder.isHttps;
- this.httpConfig = builder.httpClientConfigBuilder.Build();
- this.isDebug = builder.isDebug;
- this.endpointSuffix = builder.endpointSuffix;
- this.host = builder.host;
- }
- /// <summary>
- /// 获取 AppID
- /// </summary>
- /// <value></value>
- public string Appid
- {
- get
- {
- return appid;
- }
- }
- /// <summary>
- /// 获取 Region
- /// </summary>
- /// <value></value>
- public string Region
- {
- get
- {
- return region;
- }
- }
- /// <summary>
- /// 获取是否开启 Https
- /// </summary>
- /// <value></value>
- public bool IsHttps
- {
- get
- {
- return isHttps;
- }
- }
- /// <summary>
- /// 获取 HttpClient 配置
- /// </summary>
- /// <value></value>
- public HttpClientConfig HttpConfig
- {
- get
- {
- return httpConfig;
- }
- }
- /// <summary>
- /// 获取是否开启 DEBUG 日志
- /// </summary>
- /// <value></value>
- public bool IsDebugLog
- {
- get
- {
- return isDebug;
- }
- }
- /// <summary>
- /// Config 构造器
- /// </summary>
- public sealed class Builder
- {
- internal string appid;
- internal string region;
- internal bool isHttps = true;
- internal HttpClientConfig.Builder httpClientConfigBuilder;
- internal bool isDebug = false;
- internal string endpointSuffix;
- internal string host;
- /// <summary>
- /// 初始化一个构造器
- /// </summary>
- public Builder()
- {
- httpClientConfigBuilder = new HttpClientConfig.Builder();
- }
- /// <summary>
- /// cos 服务的Appid
- /// </summary>
- /// <param name="appid"></param>
- /// <returns></returns>
- public Builder SetAppid(string appid)
- {
- this.appid = appid;
- return this;
- }
- /// <summary>
- /// 存储桶所属地域
- /// </summary>
- /// <param name="region"></param>
- /// <returns></returns>
- public Builder SetRegion(string region)
- {
- //region cannot be empty
- if(region == null || region == "") {
- throw new CosException.CosClientException(
- (int)COSXML.Common.CosClientError.InvalidArgument,
- "region cannot be empty"
- );
- }
- this.region = region;
- return this;
- }
- /// <summary>
- /// true:https请求
- /// </summary>
- /// <param name="isHttps"></param>
- /// <returns></returns>
- public Builder IsHttps(bool isHttps)
- {
- this.isHttps = isHttps;
- return this;
- }
- /// <summary>
- /// 设置最大连接数,默认值 512
- /// </summary>
- /// <param name="connectionLimit"></param>
- /// <returns></returns>
- public Builder SetConnectionLimit(int connectionLimit)
- {
- this.httpClientConfigBuilder.SetConnectionLimit(connectionLimit);
- return this;
- }
- /// <summary>
- /// 设置 TCP 连接超时时间,单位是毫秒,默认 45 秒
- /// </summary>
- /// <param name="connectionTimeoutMs"></param>
- /// <returns></returns>
- public Builder SetConnectionTimeoutMs(int connectionTimeoutMs)
- {
- this.httpClientConfigBuilder.SetConnectionTimeoutMs(connectionTimeoutMs);
- return this;
- }
- /// <summary>
- /// 设置 TCP 连接读写时间,单位是毫秒,默认 45 秒
- /// </summary>
- /// <param name="readWriteTimeoutMs"></param>
- /// <returns></returns>
- public Builder SetReadWriteTimeoutMs(int readWriteTimeoutMs)
- {
- this.httpClientConfigBuilder.SetReadWriteTimeoutMs(readWriteTimeoutMs);
- return this;
- }
- /// <summary>
- /// 设置是否使用 Keep-Alive 长连接
- /// </summary>
- /// <param name="keepAlive"></param>
- /// <returns></returns>
- public Builder SetHttpKeepAlive(bool keepAlive)
- {
- this.httpClientConfigBuilder.SetHttpKeepAlive(keepAlive);
- return this;
- }
- /// <summary>
- /// 设置 HTTP 代理主机
- /// </summary>
- /// <param name="host"></param>
- /// <returns></returns>
- public Builder SetProxyHost(string host)
- {
- this.httpClientConfigBuilder.SetProxyHost(host);
- return this;
- }
- /// <summary>
- /// 设置 HTTP 代理端口
- /// </summary>
- /// <param name="port"></param>
- /// <returns></returns>
- public Builder SetProxyPort(int port)
- {
- this.httpClientConfigBuilder.SetProxyPort(port);
- return this;
- }
- /// <summary>
- /// 设置 HTTP 代理用户名
- /// </summary>
- /// <param name="userName"></param>
- /// <returns></returns>
- public Builder SetProxyUserName(string userName)
- {
- this.httpClientConfigBuilder.SetProxyUserName(userName);
- return this;
- }
- /// <summary>
- /// 设置 HTTP 代理用户密码
- /// </summary>
- /// <param name="password"></param>
- /// <returns></returns>
- public Builder SetProxyUserPassword(string password)
- {
- this.httpClientConfigBuilder.SetProxyUserPassword(password);
- return this;
- }
- /// <summary>
- /// 设置 HTTP 代理 Domain
- /// </summary>
- /// <param name="domain"></param>
- /// <returns></returns>
- public Builder SetProxyDomain(string domain)
- {
- this.httpClientConfigBuilder.SetProxyDomain(domain);
- return this;
- }
- /// <summary>
- /// 设置是否允许请求重定向
- /// </summary>
- /// <param name="isAllow"></param>
- /// <returns></returns>
- public Builder SetAllowAutoRedirect(bool isAllow)
- {
- this.httpClientConfigBuilder.AllowAutoRedirect(isAllow);
- return this;
- }
- /// <summary>
- /// 设置是否开启 DEBUG 日志
- /// </summary>
- /// <param name="isDebug"></param>
- /// <returns></returns>
- public Builder SetDebugLog(bool isDebug)
- {
- this.isDebug = isDebug;
- return this;
- }
- /// <summary>
- /// 设置 Endpoint 后缀,最终请求域名为 $Bucket.$EndpointSuffix
- /// </summary>
- /// <param name="suffix"></param>
- /// <returns></returns>
- public Builder SetEndpointSuffix(string suffix)
- {
- this.endpointSuffix = suffix;
- return this;
- }
- /// <summary>
- /// 设置完整请求域名
- /// </summary>
- /// <param name="host"></param>
- /// <returns></returns>
- public Builder SetHost(string host)
- {
- this.host = host;
- return this;
- }
- /// <summary>
- /// 构建 Config
- /// </summary>
- /// <returns></returns>
- public CosXmlConfig Build()
- {
- return new CosXmlConfig(this);
- }
- }
- }
- }
|