config.go raw
1 /*
2 * Copyright 2017 Baidu, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5 * except in compliance with the License. You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software distributed under the
10 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
11 * either express or implied. See the License for the specific language governing permissions
12 * and limitations under the License.
13 */
14
15 // config.go - define the client configuration for BCE
16
17 package bce
18
19 import (
20 "fmt"
21 "net/http"
22 "reflect"
23 "runtime"
24 "time"
25
26 "github.com/baidubce/bce-sdk-go/auth"
27 )
28
29 // Constants and default values for the package bce
30 const (
31 SDK_VERSION = "0.9.256"
32 URI_PREFIX = "/" // now support uri without prefix "v1" so just set root path
33 DEFAULT_DOMAIN = "baidubce.com"
34 DEFAULT_PROTOCOL = "http"
35 HTTPS_PROTOCAL = "https"
36 DEFAULT_REGION = "bj"
37 DEFAULT_CONTENT_TYPE = "application/json;charset=utf-8"
38 DEFAULT_CONNECTION_TIMEOUT_IN_MILLIS = 1200 * 1000
39 DEFAULT_WARN_LOG_TIMEOUT_IN_MILLS = 5 * 1000
40 )
41
42 var (
43 DEFAULT_USER_AGENT string
44 DEFAULT_RETRY_POLICY = NewBackOffRetryPolicy(3, 20000, 300)
45 )
46
47 func init() {
48 DEFAULT_USER_AGENT = "bce-sdk-go"
49 DEFAULT_USER_AGENT += "/" + SDK_VERSION
50 DEFAULT_USER_AGENT += "/" + runtime.Version()
51 DEFAULT_USER_AGENT += "/" + runtime.GOOS
52 DEFAULT_USER_AGENT += "/" + runtime.GOARCH
53 }
54
55 // BceClientConfiguration defines the config components structure.
56 type BceClientConfiguration struct {
57 Endpoint string
58 ProxyUrl string
59 Region string
60 UserAgent string
61 Credentials *auth.BceCredentials
62 SignOption *auth.SignOptions
63 Retry RetryPolicy
64 ConnectionTimeoutInMillis int
65 // CnameEnabled should be true when use custom domain as endpoint to visit bos resource
66 CnameEnabled bool
67 BackupEndpoint string
68 RedirectDisabled bool
69 DisableKeepAlives bool
70 NoVerifySSL bool
71 DialTimeout *time.Duration // timeout of building a connection
72 KeepAlive *time.Duration // the interval between keep-alive probes for an active connection
73 ReadTimeout *time.Duration // read timeout of net.Conn
74 WriteTimeOut *time.Duration // write timeout of net.Conn
75 TLSHandshakeTimeout *time.Duration // http.Transport.TLSHandshakeTimeout
76 IdleConnectionTimeout *time.Duration // http.Transport.IdleConnTimeout
77 ResponseHeaderTimeout *time.Duration // http.Transport.ResponseHeaderTimeout
78 HTTPClientTimeout *time.Duration // http.Client.Timeout
79 HTTPClient *http.Client // customized http client
80 UploadRatelimit *int64 // the limit of upload rate, unit:KB/s
81 DownloadRatelimit *int64 // the limit of download rate, unit:KB/s
82 }
83
84 func (c *BceClientConfiguration) String() string {
85 return fmt.Sprintf(`BceClientConfiguration [
86 Endpoint=%s;
87 ProxyUrl=%s;
88 Region=%s;
89 UserAgent=%s;
90 Credentials=%v;
91 SignOption=%v;
92 RetryPolicy=%v;
93 ConnectionTimeoutInMillis=%v;
94 RedirectDisabled=%v
95 ]`, c.Endpoint, c.ProxyUrl, c.Region, c.UserAgent, c.Credentials,
96 c.SignOption, reflect.TypeOf(c.Retry).Name(), c.ConnectionTimeoutInMillis, c.RedirectDisabled)
97 }
98