refactor: clean code

This commit is contained in:
Fu Diwei
2026-07-31 09:26:45 +08:00
committed by RHQYZ
parent cb658a3b8a
commit 8f19a61f37
6 changed files with 20 additions and 12 deletions
+2 -2
View File
@@ -79,7 +79,7 @@ func (c *Client) SetTimeout(timeout time.Duration) *Client {
return c
}
func (c *Client) newRequest(method string, path string, params any, ossResource string) (*resty.Request, error) {
func (c *Client) newRequest(method string, path string, params any, xOssResource string) (*resty.Request, error) {
if method == "" {
return nil, fmt.Errorf("sdkerr: unset method")
}
@@ -115,7 +115,7 @@ func (c *Client) newRequest(method string, path string, params any, ossResource
req.QueryParam = requestUrl.Query()
req.SetHeader("Content-MD5", payloadMd5Encoded)
req.SetHeader("Content-Type", contentType)
req.SetHeader("X-API-Resource", ossResource)
req.SetHeader("X-API-Resource", xOssResource)
req.SetBody(payloadStr)
// WARN:
+1 -5
View File
@@ -40,11 +40,7 @@ func NewClient(baseUrl string, service string, optFns ...OptionsFunc) (*Client,
return nil, fmt.Errorf("sdkerr: unset secretAccessKey")
}
signer := &signer{
accessKeyId: options.AccessKeyId,
secretAccessKey: options.SecretAccessKey,
service: service,
}
signer := NewSigner(options.AccessKeyId, options.SecretAccessKey, service)
httper := resty.New().
SetBaseURL(baseUrl).
SetHeader("Accept", "application/json").
@@ -22,6 +22,14 @@ type signer struct {
service string
}
func NewSigner(ak, sk, service string) *signer {
return &signer{
accessKeyId: ak,
secretAccessKey: sk,
service: service,
}
}
func (s *signer) Sign(req *http.Request) error {
// API 签名机制:
// https://docs.ksyun.com/documents/40298
+1 -1
View File
@@ -36,7 +36,7 @@ func NewClient(serverUrl string, optFns ...OptionsFunc) (*Client, error) {
}
httper := resty.New().
SetBaseURL(strings.TrimRight(serverUrl, "/")+"/api/v1").
SetBaseURL(strings.TrimSuffix(serverUrl, "/")+"/api/v1").
SetHeader("Accept", "application/json").
SetHeader("Content-Type", "application/json").
SetHeader("User-Agent", app.AppUserAgent).
+1 -4
View File
@@ -28,10 +28,7 @@ func NewClient(optFns ...OptionsFunc) (*Client, error) {
return nil, fmt.Errorf("sdkerr: unset secretKey")
}
signer := &signer{
accessKey: options.AccessKey,
secretKey: options.SecretKey,
}
signer := NewSigner(options.AccessKey, options.SecretKey)
httper := resty.New().
SetBaseURL("https://open.chinanetcenter.com").
SetHeader("Accept", "application/json").
@@ -19,6 +19,13 @@ type signer struct {
secretKey string
}
func NewSigner(ak, sk string) *signer {
return &signer{
accessKey: ak,
secretKey: sk,
}
}
func (s *signer) Sign(req *http.Request) error {
// API 签名机制:
// https://www.wangsu.com/document/openapi/api-authentication