sync: 同步 v1.0.4 开源代码并脱敏
This commit is contained in:
@@ -76,7 +76,46 @@ func (c *Client) DoGetRequest(ctx context.Context, endpoint string, params url.V
|
||||
return c.doRequest(req)
|
||||
}
|
||||
|
||||
// DoGetWriteRequest 用独立无复用连接发送非幂等GET,禁止运输层重放与跳转。
|
||||
func (c *Client) DoGetWriteRequest(ctx context.Context, endpoint string, params url.Values) ([]byte, error) {
|
||||
req, err := c.doGetRequest(ctx, endpoint, params)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return c.doWriteRequest(req)
|
||||
}
|
||||
|
||||
func (c *Client) doWriteRequest(req *http.Request) ([]byte, error) {
|
||||
transport := http.DefaultTransport.(*http.Transport).Clone()
|
||||
if original, ok := c.http.Transport.(*http.Transport); ok {
|
||||
transport = original.Clone()
|
||||
}
|
||||
transport.DisableKeepAlives = true
|
||||
defer transport.CloseIdleConnections()
|
||||
client := *c.http
|
||||
client.Transport = transport
|
||||
client.CheckRedirect = func(*http.Request, []*http.Request) error { return http.ErrUseLastResponse }
|
||||
return c.doRequestWithClient(&client, req)
|
||||
}
|
||||
|
||||
func (c *Client) DoPostJSONRequest(ctx context.Context, endpoint string, body []byte) ([]byte, error) {
|
||||
req, err := c.doPostJSONRequest(ctx, endpoint, body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return c.doRequest(req)
|
||||
}
|
||||
|
||||
// DoPostJSONWriteRequest 仅用于办理写请求,禁止连接复用及自动跳转重放。
|
||||
func (c *Client) DoPostJSONWriteRequest(ctx context.Context, endpoint string, body []byte) ([]byte, error) {
|
||||
req, err := c.doPostJSONRequest(ctx, endpoint, body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return c.doWriteRequest(req)
|
||||
}
|
||||
|
||||
func (c *Client) doPostJSONRequest(ctx context.Context, endpoint string, body []byte) (*http.Request, error) {
|
||||
timestamp := GetTimestampGMT()
|
||||
signStr, err := BuildSign("", string(body), c.appSecret, timestamp)
|
||||
if c.uppercaseSign {
|
||||
@@ -93,7 +132,7 @@ func (c *Client) DoPostJSONRequest(ctx context.Context, endpoint string, body []
|
||||
req.Header.Set("Sign", c.formatSign(signStr))
|
||||
req.Header.Set("Timestamp", timestamp)
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
return c.doRequest(req)
|
||||
return req, nil
|
||||
}
|
||||
|
||||
// doGetRequest 封装通用的 GET 请求构建逻辑
|
||||
@@ -127,7 +166,11 @@ func (c *Client) formatSign(sign string) string {
|
||||
|
||||
// doRequest 执行HTTP请求
|
||||
func (c *Client) doRequest(req *http.Request) ([]byte, error) {
|
||||
resp, err := c.http.Do(req)
|
||||
return c.doRequestWithClient(c.http, req)
|
||||
}
|
||||
|
||||
func (c *Client) doRequestWithClient(client *http.Client, req *http.Request) ([]byte, error) {
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user