package sdk import ( "context" "crypto/tls" "fmt" "golib.gaore.com/GaoreGo/gaore-common-sdk-go/sdk/auth" "golib.gaore.com/GaoreGo/gaore-common-sdk-go/sdk/auth/credentials" "golib.gaore.com/GaoreGo/gaore-common-sdk-go/sdk/requests" "golib.gaore.com/GaoreGo/gaore-common-sdk-go/sdk/responses" "net" "net/http" "runtime" "strings" "time" ) var Version = "0.0.1" var defaultConnectTimeout = 5 * time.Second var defaultReadTimeout = 10 * time.Second var DefaultUserAgent = fmt.Sprintf("GaoreGoSdk (%s;%s) Golang/%s Core/%s", runtime.GOOS, runtime.GOARCH, strings.Trim(runtime.Version(), "go"), Version) type Client struct { Host string httpClient *http.Client isInsecure bool signer auth.Signer readTimeout time.Duration connectTimeout time.Duration config *Config } func (client *Client) GetHTTPSInsecure() bool { return client.isInsecure } func (client *Client) InitClientConfig() (config *Config) { if client.config != nil { return client.config } else { return NewConfig() } } func (client *Client) InitWithAccessKey(accessKeyId, accessKeySecret, source string) (err error) { config := client.InitWithConfig() credential := &credentials.BaseCredential{ AccessKeyId: accessKeyId, AccessKeySecret: accessKeySecret, } return client.InitWithOptions("", config, credential) } func (client *Client) InitWithOptions(host string, config *Config, credential auth.Credential) (err error) { client.httpClient = &http.Client{} if config.Transport != nil { client.httpClient.Transport = config.Transport } else if config.HttpTransport != nil { client.httpClient.Transport = config.HttpTransport } if config.Timeout > 0 { client.httpClient.Timeout = config.Timeout } client.signer, err = auth.NewSignerWithCredential(credential, client.ProcessCommonRequestWithSigner) return nil } func (client *Client) InitWithConfig() (config *Config) { if client.config != nil { return client.config } else { return NewConfig() } } func (client *Client) ProcessCommonRequestWithSigner(request *requests.CommonRequest, signerInterface interface{}) (response *responses.CommonResponse, err error) { if signer, isSigner := signerInterface.(auth.Signer); isSigner { response = responses.NewCommonResponse() err = client.DoActionWithSigner(request, response, signer) return } panic("should not be here") } func Timeout(connectTimeout time.Duration) func(ctx context.Context, net, addr string) (c net.Conn, err error) { return func(ctx context.Context, network, address string) (c net.Conn, err error) { return (&net.Dialer{ Timeout: connectTimeout, DualStack: true, }).DialContext(ctx, network, address) } } func (client *Client) setTimeOut(request requests.AcsRequest) { readTimeout, connectTimeout := client.getTimeOut(request) client.httpClient.Timeout = readTimeout if trans, ok := client.httpClient.Transport.(*http.Transport); ok && trans != nil { trans.DialContext = Timeout(connectTimeout) client.httpClient.Transport = trans } else if client.httpClient.Transport == nil { client.httpClient.Transport = &http.Transport{ DialContext: Timeout(connectTimeout), } } } func (client *Client) getTimeOut(request requests.AcsRequest) (time.Duration, time.Duration) { readTimeOut := defaultReadTimeout connectTimeOut := defaultConnectTimeout reqReadTimeout := request.GetReadTimeout() reqConnectTimeout := request.GetConnectTimeout() if reqReadTimeout != 0*time.Millisecond { readTimeOut = reqReadTimeout } else if client.readTimeout != 0*time.Microsecond { readTimeOut = client.readTimeout } else if client.httpClient.Timeout != 0 { readTimeOut = client.httpClient.Timeout } if reqConnectTimeout != 0*time.Microsecond { connectTimeOut = reqConnectTimeout } else if client.connectTimeout != 0*time.Millisecond { connectTimeOut = client.connectTimeout } return readTimeOut, connectTimeOut } func (client *Client) getHTTPSInsecure(request requests.AcsRequest) (insecure bool) { if request.GetHTTPSInsecure() != nil { insecure = *request.GetHTTPSInsecure() } else { insecure = client.GetHTTPSInsecure() } return } func (client *Client) DoAction(request requests.AcsRequest, response responses.AcsResponse) (err error) { return client.DoActionWithSigner(request, response, nil) } func (client *Client) DoActionWithSigner(request requests.AcsRequest, response responses.AcsResponse, signer auth.Signer) (err error) { client.setTimeOut(request) if trans, ok := client.httpClient.Transport.(*http.Transport); ok && trans != nil { if trans.TLSClientConfig != nil { trans.TLSClientConfig.InsecureSkipVerify = client.getHTTPSInsecure(request) } else { trans.TLSClientConfig = &tls.Config{ InsecureSkipVerify: client.getHTTPSInsecure(request), } } client.httpClient.Transport = trans } var httpResponse *http.Response } func (client *Client) buildRequestWithSigner(request requests.AcsRequest, signer auth.Signer) (httpRequest *requests.HttpRequest, err error) { }