From 3fcea4ba2f904af272923bb6e611e2782a2e7ad4 Mon Sep 17 00:00:00 2001 From: Fu Diwei Date: Fri, 20 Dec 2024 23:07:40 +0800 Subject: [PATCH] refactor: clean code --- internal/pkg/vendors/qiniu-sdk/client.go | 31 +++++++++++++----------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/internal/pkg/vendors/qiniu-sdk/client.go b/internal/pkg/vendors/qiniu-sdk/client.go index 76c07ba9..1f036afd 100644 --- a/internal/pkg/vendors/qiniu-sdk/client.go +++ b/internal/pkg/vendors/qiniu-sdk/client.go @@ -6,10 +6,9 @@ import ( "fmt" "io" "net/http" + "strings" "github.com/qiniu/go-sdk/v7/auth" - - xhttp "github.com/usual2970/certimate/internal/utils/http" ) const qiniuHost = "https://api.qiniu.com" @@ -136,25 +135,29 @@ func (c *Client) UploadSslCert(name, commonName, certificate, privateKey string) } func (c *Client) sendReq(method string, path string, body io.Reader) ([]byte, error) { - req := xhttp.BuildReq(fmt.Sprintf("%s/%s", qiniuHost, path), method, body, map[string]string{ - "Content-Type": "application/json", - }) + path = strings.TrimPrefix(path, "/") + + req, err := http.NewRequest(method, fmt.Sprintf("%s/%s", qiniuHost, path), body) + if err != nil { + return nil, err + } + req.Header.Add("Content-Type", "application/json") if err := c.mac.AddToken(auth.TokenQBox, req); err != nil { return nil, err } - respBody, err := xhttp.ToRequest(req) + client := http.Client{} + resp, err := client.Do(req) + if err != nil { + return nil, err + } + defer resp.Body.Close() + + r, err := io.ReadAll(resp.Body) if err != nil { return nil, err } - defer respBody.Close() - - res, err := io.ReadAll(respBody) - if err != nil { - return nil, err - } - - return res, nil + return r, nil }