Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 3618315947 | |||
| 28a4a7b309 | |||
| 4d8a89852b | |||
| 60574a9378 | |||
| 2a388c0108 | |||
| 268f040e48 |
+56
-31
@@ -48,11 +48,11 @@ type CastError struct {
|
||||
|
||||
// Request is used wrap http request
|
||||
type Request struct {
|
||||
Protocol string
|
||||
Port int
|
||||
Method string
|
||||
Pathname string
|
||||
Domain string
|
||||
Protocol *string
|
||||
Port *int
|
||||
Method *string
|
||||
Pathname *string
|
||||
Domain *string
|
||||
Headers map[string]string
|
||||
Query map[string]string
|
||||
Body io.Reader
|
||||
@@ -61,8 +61,8 @@ type Request struct {
|
||||
// Response is use d wrap http response
|
||||
type Response struct {
|
||||
Body io.ReadCloser
|
||||
StatusCode int
|
||||
StatusMessage string
|
||||
StatusCode *int
|
||||
StatusMessage *string
|
||||
Headers map[string]string
|
||||
}
|
||||
|
||||
@@ -154,8 +154,8 @@ func NewResponse(httpResponse *http.Response) (res *Response) {
|
||||
res = &Response{}
|
||||
res.Body = httpResponse.Body
|
||||
res.Headers = make(map[string]string)
|
||||
res.StatusCode = httpResponse.StatusCode
|
||||
res.StatusMessage = httpResponse.Status
|
||||
res.StatusCode = Int(httpResponse.StatusCode)
|
||||
res.StatusMessage = String(httpResponse.Status)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -230,29 +230,29 @@ func DoRequest(request *Request, requestRuntime map[string]interface{}) (respons
|
||||
runtimeObject.Logger.PrintLog(fieldMap, err)
|
||||
}
|
||||
}()
|
||||
if request.Method == "" {
|
||||
request.Method = "GET"
|
||||
if request.Method == nil {
|
||||
request.Method = String("GET")
|
||||
}
|
||||
|
||||
if request.Protocol == "" {
|
||||
request.Protocol = "http"
|
||||
if request.Protocol == nil {
|
||||
request.Protocol = String("http")
|
||||
} else {
|
||||
request.Protocol = strings.ToLower(request.Protocol)
|
||||
request.Protocol = String(strings.ToLower(StringValue(request.Protocol)))
|
||||
}
|
||||
|
||||
if request.Protocol == "http" {
|
||||
request.Port = 80
|
||||
} else if request.Protocol == "https" {
|
||||
request.Port = 443
|
||||
if StringValue(request.Protocol) == "http" {
|
||||
request.Port = Int(80)
|
||||
} else if StringValue(request.Protocol) == "https" {
|
||||
request.Port = Int(443)
|
||||
}
|
||||
|
||||
requestURL := ""
|
||||
request.Domain = request.Headers["host"]
|
||||
matched, _ := regexp.MatchString(":", request.Domain)
|
||||
request.Domain = String(request.Headers["host"])
|
||||
matched, _ := regexp.MatchString(":", StringValue(request.Domain))
|
||||
if matched {
|
||||
requestURL = fmt.Sprintf("%s://%s%s", request.Protocol, request.Domain, request.Pathname)
|
||||
requestURL = fmt.Sprintf("%s://%s%s", StringValue(request.Protocol), StringValue(request.Domain), StringValue(request.Pathname))
|
||||
} else {
|
||||
requestURL = fmt.Sprintf("%s://%s:%d%s", request.Protocol, request.Domain, request.Port, request.Pathname)
|
||||
requestURL = fmt.Sprintf("%s://%s:%d%s", StringValue(request.Protocol), StringValue(request.Domain), IntValue(request.Port), StringValue(request.Pathname))
|
||||
}
|
||||
queryParams := request.Query
|
||||
// sort QueryParams by key
|
||||
@@ -268,15 +268,15 @@ func DoRequest(request *Request, requestRuntime map[string]interface{}) (respons
|
||||
requestURL = fmt.Sprintf("%s?%s", requestURL, querystring)
|
||||
}
|
||||
}
|
||||
debugLog("> %s %s", request.Method, requestURL)
|
||||
debugLog("> %s %s", StringValue(request.Method), requestURL)
|
||||
|
||||
httpRequest, err := http.NewRequest(request.Method, requestURL, request.Body)
|
||||
httpRequest, err := http.NewRequest(StringValue(request.Method), requestURL, request.Body)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
httpRequest.Host = request.Domain
|
||||
httpRequest.Host = StringValue(request.Domain)
|
||||
|
||||
client := getTeaClient(runtimeObject.getClientTag(request.Domain))
|
||||
client := getTeaClient(runtimeObject.getClientTag(StringValue(request.Domain)))
|
||||
client.Lock()
|
||||
if !client.ifInit {
|
||||
trans, err := getHttpTransport(request, runtimeObject)
|
||||
@@ -335,7 +335,7 @@ func DoRequest(request *Request, requestRuntime map[string]interface{}) (respons
|
||||
|
||||
func getHttpTransport(req *Request, runtime *RuntimeObject) (*http.Transport, error) {
|
||||
trans := new(http.Transport)
|
||||
httpProxy, err := getHttpProxy(req.Protocol, req.Domain, runtime)
|
||||
httpProxy, err := getHttpProxy(StringValue(req.Protocol), StringValue(req.Domain), runtime)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -369,7 +369,7 @@ func getHttpTransport(req *Request, runtime *RuntimeObject) (*http.Transport, er
|
||||
&net.Dialer{
|
||||
Timeout: time.Duration(runtime.ConnectTimeout) * time.Second,
|
||||
DualStack: true,
|
||||
LocalAddr: getLocalAddr(runtime.LocalAddr, req.Port),
|
||||
LocalAddr: getLocalAddr(runtime.LocalAddr, IntValue(req.Port)),
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -377,7 +377,7 @@ func getHttpTransport(req *Request, runtime *RuntimeObject) (*http.Transport, er
|
||||
trans.Dial = dialer.Dial
|
||||
}
|
||||
} else {
|
||||
trans.DialContext = setDialContext(runtime, req.Port)
|
||||
trans.DialContext = setDialContext(runtime, IntValue(req.Port))
|
||||
}
|
||||
return trans, nil
|
||||
}
|
||||
@@ -495,7 +495,7 @@ func setDialContext(runtime *RuntimeObject, port int) func(cxt context.Context,
|
||||
}
|
||||
|
||||
func (err *SDKError) Error() string {
|
||||
return fmt.Sprintf("SDKError: %s %s %s", err.Code, err.Message, err.Data)
|
||||
return fmt.Sprintf("SDKError:\n Code: %s\n Message: %s\n Data: %s\n", err.Code, err.Message, err.Data)
|
||||
}
|
||||
|
||||
func ToObject(obj interface{}) map[string]interface{} {
|
||||
@@ -557,9 +557,27 @@ func Merge(args ...interface{}) map[string]string {
|
||||
return finalArg
|
||||
}
|
||||
|
||||
func isNil(a interface{}) bool {
|
||||
defer func() {
|
||||
recover()
|
||||
}()
|
||||
vi := reflect.ValueOf(a)
|
||||
return vi.IsNil()
|
||||
}
|
||||
|
||||
func ToMap(args ...interface{}) map[string]interface{} {
|
||||
isNotNil := false
|
||||
finalArg := make(map[string]interface{})
|
||||
for _, obj := range args {
|
||||
if obj == nil {
|
||||
continue
|
||||
}
|
||||
|
||||
if isNil(obj) {
|
||||
continue
|
||||
}
|
||||
isNotNil = true
|
||||
|
||||
switch obj.(type) {
|
||||
case map[string]string:
|
||||
arg := obj.(map[string]string)
|
||||
@@ -618,6 +636,9 @@ func ToMap(args ...interface{}) map[string]interface{} {
|
||||
}
|
||||
}
|
||||
|
||||
if !isNotNil {
|
||||
return nil
|
||||
}
|
||||
return finalArg
|
||||
}
|
||||
|
||||
@@ -848,7 +869,11 @@ func checkMaxLength(valueField reflect.Value, tag string) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if maxLength < valueField.Len() {
|
||||
length := valueField.Len()
|
||||
if valueField.Kind().String() == "string" {
|
||||
length = strings.Count(valueField.String(), "") - 1
|
||||
}
|
||||
if maxLength < length {
|
||||
errMsg := fmt.Sprintf("Length of %s is more than %d", valueField.String(), maxLength)
|
||||
return errors.New(errMsg)
|
||||
}
|
||||
|
||||
+16
-11
@@ -47,6 +47,7 @@ var runtimeObj = map[string]interface{}{
|
||||
|
||||
type validateTest struct {
|
||||
Num *int `json:"num" require:"true"`
|
||||
Name *string `json:"name" maxLength:"4"`
|
||||
Str *string `json:"str" pattern:"^[a-d]*$" maxLength:"4"`
|
||||
Test *errLength `json:"test"`
|
||||
List []*string `json:"list" pattern:"^[a-d]*$" maxLength:"4"`
|
||||
@@ -139,7 +140,7 @@ func TestSDKError(t *testing.T) {
|
||||
},
|
||||
})
|
||||
utils.AssertNotNil(t, err)
|
||||
utils.AssertEqual(t, "SDKError: code message {\"hostId\":\"github.com/alibabacloud/tea\",\"httpCode\":\"404\",\"requestId\":\"dfadfa32cgfdcasd4313\"}", err.Error())
|
||||
utils.AssertEqual(t, "SDKError:\n Code: code\n Message: message\n Data: {\"hostId\":\"github.com/alibabacloud/tea\",\"httpCode\":\"404\",\"requestId\":\"dfadfa32cgfdcasd4313\"}\n", err.Error())
|
||||
}
|
||||
|
||||
func TestSDKErrorCode404(t *testing.T) {
|
||||
@@ -148,7 +149,7 @@ func TestSDKErrorCode404(t *testing.T) {
|
||||
"message": "message",
|
||||
})
|
||||
utils.AssertNotNil(t, err)
|
||||
utils.AssertEqual(t, "SDKError: 404 message ", err.Error())
|
||||
utils.AssertEqual(t, "SDKError:\n Code: 404\n Message: message\n Data: \n", err.Error())
|
||||
}
|
||||
|
||||
func TestToObject(t *testing.T) {
|
||||
@@ -273,13 +274,13 @@ func TestToMap(t *testing.T) {
|
||||
|
||||
invalidStr := "sdfdg"
|
||||
result = ToMap(invalidStr)
|
||||
utils.AssertEqual(t, result, map[string]interface{}{})
|
||||
utils.AssertEqual(t, map[string]interface{}{}, result)
|
||||
|
||||
result = ToMap(10)
|
||||
utils.AssertEqual(t, result, map[string]interface{}{})
|
||||
utils.AssertEqual(t, map[string]interface{}{}, result)
|
||||
|
||||
result = ToMap(nil)
|
||||
utils.AssertEqual(t, map[string]interface{}{}, result)
|
||||
utils.AssertNil(t, result)
|
||||
}
|
||||
|
||||
func Test_Retryable(t *testing.T) {
|
||||
@@ -334,14 +335,14 @@ func Test_DoRequest(t *testing.T) {
|
||||
}
|
||||
}
|
||||
request := NewRequest()
|
||||
request.Port = 80
|
||||
request.Method = "TEA TEST"
|
||||
request.Port = Int(80)
|
||||
request.Method = String("TEA TEST")
|
||||
resp, err := DoRequest(request, nil)
|
||||
utils.AssertNil(t, resp)
|
||||
utils.AssertEqual(t, `net/http: invalid method "TEA TEST"`, err.Error())
|
||||
|
||||
request.Method = ""
|
||||
request.Protocol = "https"
|
||||
request.Method = String("")
|
||||
request.Protocol = String("https")
|
||||
request.Query = map[string]string{
|
||||
"tea": "test",
|
||||
}
|
||||
@@ -350,7 +351,7 @@ func Test_DoRequest(t *testing.T) {
|
||||
utils.AssertNil(t, resp)
|
||||
utils.AssertEqual(t, `parse # #%gfdf: invalid URL escape "%gf"`, err.Error())
|
||||
|
||||
request.Pathname = "?log"
|
||||
request.Pathname = String("?log")
|
||||
request.Headers["tea"] = ""
|
||||
runtimeObj["httpsProxy"] = "http://someuser:somepassword@ecs.aliyun.com"
|
||||
resp, err = DoRequest(request, runtimeObj)
|
||||
@@ -541,7 +542,7 @@ func Test_validate(t *testing.T) {
|
||||
utils.AssertNil(t, err)
|
||||
|
||||
num := 1
|
||||
str0, str1 := "acc", "abcddd"
|
||||
str0, str1 := "abc", "abcddd"
|
||||
val := &validateTest{
|
||||
Num: &num,
|
||||
Str: &str0,
|
||||
@@ -559,6 +560,10 @@ func Test_validate(t *testing.T) {
|
||||
err = validate(reflect.ValueOf(val))
|
||||
utils.AssertEqual(t, "num should be setted", err.Error())
|
||||
|
||||
val.Name = String("最大长度")
|
||||
err = validate(reflect.ValueOf(val))
|
||||
utils.AssertEqual(t, "num should be setted", err.Error())
|
||||
|
||||
val.Num = &num
|
||||
val.Str = &str0
|
||||
val.List = []*string{&str1}
|
||||
|
||||
Reference in New Issue
Block a user