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