Skip to content

Commit

Permalink
Merge branch 'release/v1.21.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
bububa committed Oct 30, 2023
2 parents 07aa598 + 999db8d commit f7041cd
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 21 deletions.
12 changes: 5 additions & 7 deletions marketing-api/core/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"crypto/sha256"
"encoding/hex"
"errors"
"io"
"mime/multipart"
"net/http"
Expand Down Expand Up @@ -345,13 +346,10 @@ func (c *SDKClient) fetch(httpReq *http.Request, resp model.Response) error {
}
if body, err := debug.DecodeJSONHttpResponse(httpResp.Body, resp, c.debug); err != nil {
debug.PrintError(err, c.debug)
if body != nil {
return model.BaseResponse{
Code: httpResp.StatusCode,
Message: string(body),
}
}
return err
return errors.Join(model.BaseResponse{
Code: httpResp.StatusCode,
Message: string(body),
}, err)
}
if resp.IsError() {
return resp
Expand Down
30 changes: 16 additions & 14 deletions marketing-api/core/internal/debug/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io"
"log"
"net/http"
"strings"

"github.com/bububa/oceanengine/marketing-api/util"
)
Expand Down Expand Up @@ -60,10 +61,12 @@ func PrintJSONRequest(method string, url string, header http.Header, body []byte
buf := util.GetBufferPool()
defer util.PutBufferPool(buf)
json.Indent(buf, body, "", "\t")
headers := make([]string, 0, len(header))
for k := range header {
headers = append(headers, util.StringsJoin(k, ": ", header.Get(k)))
}

headerJson, _ := json.MarshalIndent(header, "", "\t")

log.Printf(format, method, url, headerJson, buf.String())
log.Printf(format, method, url, strings.Join(headers, "\n"), buf.String())
}

// PrintPostMultipartRequest print multipart/form-data post request with debug
Expand All @@ -82,20 +85,19 @@ func PrintPostMultipartRequest(url string, mp map[string]string, debug bool) {
func DecodeJSONHttpResponse(r io.Reader, v interface{}, debug bool) ([]byte, error) {
buf := util.GetBufferPool()
defer util.PutBufferPool(buf)
if !debug {
tee := io.TeeReader(r, buf)
err := json.NewDecoder(tee).Decode(v)
tee := io.TeeReader(r, buf)
if err := json.NewDecoder(tee).Decode(v); err != nil {
return buf.Bytes(), err
}
body, err := io.ReadAll(r)
if err != nil {
return nil, err
if !debug {
return nil, nil
}
if err := json.Indent(buf, body, "", "\t"); err != nil {
return body, err
bs := buf.Bytes()
buf.Reset()
if err := json.Indent(buf, bs, "", "\t"); err != nil {
return bs, err
}

log.Println(util.StringsJoin("[DEBUG] [API] http response body:\n", string(buf.Bytes())))

return body, json.Unmarshal(body, v)
log.Println(util.StringsJoin("[DEBUG] [API] http response body:\n", string(bs)))
return nil, nil
}

0 comments on commit f7041cd

Please sign in to comment.