7
0

init project

This commit is contained in:
liangzy 2020-08-04 21:14:06 +08:00
parent b688b909e8
commit e7cb80d0b2
5 changed files with 115 additions and 6 deletions

View File

@ -2,6 +2,7 @@ package sdk
import ( import (
"context" "context"
"crypto/tls"
"fmt" "fmt"
"golib.gaore.com/GaoreGo/gaore-common-sdk-go/sdk/auth" "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/auth/credentials"
@ -29,7 +30,16 @@ type Client struct {
config *Config config *Config
} }
func (client *Client) GetHTTPSInsecure() bool {
return client.isInsecure
}
func (client *Client) InitClientConfig() (config *Config) { func (client *Client) InitClientConfig() (config *Config) {
if client.config != nil {
return client.config
} else {
return NewConfig()
}
} }
@ -121,10 +131,39 @@ func (client *Client) getTimeOut(request requests.AcsRequest) (time.Duration, ti
return readTimeOut, 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) { func (client *Client) DoAction(request requests.AcsRequest, response responses.AcsResponse) (err error) {
return client.DoActionWithSigner(request, response, nil) return client.DoActionWithSigner(request, response, nil)
} }
func (client *Client) DoActionWithSigner(request requests.AcsRequest, response responses.AcsResponse, signer auth.Signer) (err error) { 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) {
} }

View File

@ -1,16 +1,23 @@
package sdk package sdk
import ( import (
"golib.gaore.com/GaoreGo/gaore-common-sdk-go/sdk/utils"
"net/http" "net/http"
"time" "time"
) )
type Config struct { type Config struct {
Timeout time.Duration Debug bool `default:"false"`
HttpTransport *http.Transport `default:""` HttpTransport *http.Transport `default:""`
Transport http.RoundTripper `default:""` Transport http.RoundTripper `default:""`
GoRoutinePoolSize int `default:"0"`
UserAgent string `default:""`
Scheme string `default:"HTTP"`
Timeout time.Duration `default:"5"`
} }
func NewConfig() *Config { func NewConfig() *Config {
return &Config{} config := &Config{}
utils.InitStructWithDefaultTag(config)
return config
} }

View File

@ -1,6 +1,13 @@
package utils package utils
import "net/url" import (
"fmt"
"net/url"
"reflect"
"strconv"
"strings"
"time"
)
func GetUrlFormedMap(source map[string]string) (urlEncoded string) { func GetUrlFormedMap(source map[string]string) (urlEncoded string) {
urlEncoder := url.Values{} urlEncoder := url.Values{}
@ -10,3 +17,34 @@ func GetUrlFormedMap(source map[string]string) (urlEncoded string) {
urlEncoded = urlEncoder.Encode() urlEncoded = urlEncoder.Encode()
return return
} }
func InitStructWithDefaultTag(bean interface{}) {
beantype := reflect.TypeOf(bean)
for i := 0; i < beantype.Elem().NumField(); i++ {
field := beantype.Elem().Field(i)
defaultValue := strings.TrimSpace(field.Tag.Get("default"))
if defaultValue == "" || defaultValue == "-" {
continue
}
setter := reflect.ValueOf(bean).Elem().Field(i)
fieldTypeName := field.Type.String()
switch fieldTypeName {
case "int", "int64", "int32", "int8", "int16":
intval, _ := strconv.ParseInt(defaultValue, 10, 64)
setter.SetInt(intval)
case "uint", "uint8", "uint16", "uint32", "uint64", "uintptr":
uintval, _ := strconv.ParseUint(defaultValue, 10, 64)
setter.SetUint(uintval)
case "string":
setter.SetString(defaultValue)
case "time.Duration":
intval, _ := strconv.ParseInt(defaultValue, 10, 64)
setter.SetInt(intval * int64(time.Second))
case "bool":
boolval, _ := strconv.ParseBool(defaultValue)
setter.SetBool(boolval)
default:
fmt.Println(field.Type.String(), field.Name)
}
}
}

25
sdk/utils/utils_test.go Normal file
View File

@ -0,0 +1,25 @@
package utils
import (
"fmt"
"net/http"
"testing"
"time"
)
type TestCase struct {
Debug bool `default:"false"`
HttpTransport *http.Transport `default:""`
Transport http.RoundTripper `default:""`
GoRoutinePoolSize int `default:"5"`
UserAgent string `default:""`
Scheme string `default:"HTTP"`
Haha uintptr `default:"232"`
Timeout time.Duration `default:"5"`
}
func TestInitStructWithDefaultTag(t *testing.T) {
testcase := &TestCase{}
InitStructWithDefaultTag(testcase)
fmt.Printf("%+v", testcase)
}

View File

@ -19,6 +19,6 @@ func (c *Client) SendSms(req *SendSmsRequest) (response *SendSmsResponse, err er
func NewClientWithAccessKey(accesskey, secrect, source string) (client *Client, err error) { func NewClientWithAccessKey(accesskey, secrect, source string) (client *Client, err error) {
client = &Client{} client = &Client{}
err = client.InitWithOptions("", nil, "") err = client.InitWithAccessKey(accesskey, secrect, source)
return return
} }