Skip to content

Commit

Permalink
feat: export json marshall
Browse files Browse the repository at this point in the history
  • Loading branch information
NgoKimPhu committed Dec 11, 2023
1 parent d05aa58 commit 487c097
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion http.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/hashicorp/go-retryablehttp"
)

// HttpCfg is the resty http client configs
type HttpCfg struct {
HttpClient *http.Client `json:"-"`
BaseUrl string // client's base url for all methods
Expand All @@ -23,6 +24,7 @@ type HttpCfg struct {
Debug bool // whether to log requests and responses
}

// NewRestyClient creates a new resty client with the given configs
func (h *HttpCfg) NewRestyClient() (client *resty.Client) {
if h == nil {
return resty.New()
Expand All @@ -47,7 +49,7 @@ func (h *HttpCfg) NewRestyClient() (client *resty.Client) {
if maxWaitTime := h.RetryMaxWaitTime; maxWaitTime != 0 {
client.SetRetryMaxWaitTime(maxWaitTime)
}
client.JSONMarshal = json.Marshal
client.JSONMarshal = JSONMarshal
client.JSONUnmarshal = JSONUnmarshal
return client
}
Expand All @@ -65,6 +67,12 @@ func retryableHttpError(r *resty.Response, err error) bool {
}
}

// JSONMarshal allows choosing the JSON marshalling implementation with build tag with the same logic as used by gin
func JSONMarshal(v any) ([]byte, error) {
return json.Marshal(v)
}

// JSONUnmarshal allows choosing the JSON unmarshalling implementation with build tag with the same logic as used by gin
func JSONUnmarshal(data []byte, v any) error {
decoder := json.NewDecoder(bytes.NewReader(data))
decoder.UseNumber()
Expand Down

0 comments on commit 487c097

Please sign in to comment.