Skip to content

Commit

Permalink
Make NewRequestWithContext more robust
Browse files Browse the repository at this point in the history
  • Loading branch information
hiepnv90 committed Jul 11, 2024
1 parent 49149e5 commit 7560068
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions pkg/httpclient/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ import (
"fmt"
"io"
"net/http"

"github.com/KyberNetwork/tradinglib/pkg/sb"
"net/url"
)

func DoHTTPRequest(client *http.Client, req *http.Request, out interface{}, options ...Option) (*http.Response, error) {
Expand Down Expand Up @@ -47,11 +46,19 @@ func NewRequest(method, baseURL, path string, query Query, body io.Reader) (*htt
func NewRequestWithContext(
ctx context.Context, method, baseURL, path string, query Query, body io.Reader,
) (*http.Request, error) {
url := baseURL + path
u, err := url.Parse(baseURL)
if err != nil {
return nil, fmt.Errorf("parse url: %w", err)
}

if path != "" {
u = u.JoinPath(path)
}
if query != nil {
url = sb.Concat(url, "?", query.String())
u.RawQuery = query.String()
}
return http.NewRequestWithContext(ctx, method, url, body)

return http.NewRequestWithContext(ctx, method, u.String(), body)
}

func NewGet(baseURL, path string, query Query) (*http.Request, error) {
Expand Down

0 comments on commit 7560068

Please sign in to comment.