Skip to content

Commit

Permalink
support pointer
Browse files Browse the repository at this point in the history
  • Loading branch information
wenzuochao authored and JacksonTian committed Apr 20, 2020
1 parent 28a4a7b commit 3618315
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 37 deletions.
60 changes: 30 additions & 30 deletions tea/tea.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}

Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -369,15 +369,15 @@ 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
}
trans.Dial = dialer.Dial
}
} else {
trans.DialContext = setDialContext(runtime, req.Port)
trans.DialContext = setDialContext(runtime, IntValue(req.Port))
}
return trans, nil
}
Expand Down Expand Up @@ -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{} {
Expand Down
14 changes: 7 additions & 7 deletions tea/tea_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,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) {
Expand All @@ -149,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) {
Expand Down Expand Up @@ -335,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",
}
Expand All @@ -351,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:[email protected]"
resp, err = DoRequest(request, runtimeObj)
Expand Down

0 comments on commit 3618315

Please sign in to comment.