diff --git a/examples/http/account/main.go b/examples/http/account/main.go index 2707418..59c493a 100644 --- a/examples/http/account/main.go +++ b/examples/http/account/main.go @@ -16,7 +16,7 @@ func main() { var ( key = os.Getenv("API_KEY") secret = os.Getenv("API_SECRET") - client = bitvavo.NewHttpClient(httpc.WithDebug(true)) + client = bitvavo.NewHttpClient(httpc.WithDebug(false)) authClient = client.ToAuthClient(key, secret, 0) ) diff --git a/httpc/client.go b/httpc/client.go index eb09ab3..4aebbdd 100644 --- a/httpc/client.go +++ b/httpc/client.go @@ -10,7 +10,6 @@ import ( "github.com/goccy/go-json" "github.com/larscom/go-bitvavo/v2/crypto" - "github.com/larscom/go-bitvavo/v2/log" "github.com/larscom/go-bitvavo/v2/util" ) @@ -20,19 +19,21 @@ var ( func httpGet[T any]( url string, - updateRateLimit func(int64), - updateRateLimitResetAt func(time.Time), + updateRateLimit func(ratelimit int64), + updateRateLimitResetAt func(resetAt time.Time), + logDebug func(message string, args ...any), config *authConfig, ) (T, error) { req, _ := http.NewRequest("GET", url, nil) - return httpDo[T](req, updateRateLimit, updateRateLimitResetAt, config) + return httpDo[T](req, updateRateLimit, updateRateLimitResetAt, logDebug, config) } func httpPost[T any]( url string, body T, - updateRateLimit func(int64), - updateRateLimitResetAt func(time.Time), + updateRateLimit func(ratelimit int64), + updateRateLimitResetAt func(resetAt time.Time), + logDebug func(message string, args ...any), config *authConfig, ) (T, error) { payload, err := json.Marshal(body) @@ -40,16 +41,17 @@ func httpPost[T any]( return body, err } req, _ := http.NewRequest("POST", url, bytes.NewBuffer(payload)) - return httpDo[T](req, updateRateLimit, updateRateLimitResetAt, config) + return httpDo[T](req, updateRateLimit, updateRateLimitResetAt, logDebug, config) } func httpDo[T any]( request *http.Request, - updateRatelimit func(int64), - updateRateLimitResetAt func(time.Time), + updateRateLimit func(ratelimit int64), + updateRateLimitResetAt func(resetAt time.Time), + logDebug func(message string, args ...any), config *authConfig, ) (T, error) { - log.Logger().Debug("executing request", "method", request.Method, "url", request.URL.String()) + logDebug("executing request", "method", request.Method, "url", request.URL.String()) var data T if err := applyHeaders(request, config); err != nil { @@ -66,7 +68,7 @@ func httpDo[T any]( if len(value) == 0 { return data, fmt.Errorf("header: %s didn't contain a value", headerRatelimit) } - updateRatelimit(util.MustInt64(value[0])) + updateRateLimit(util.MustInt64(value[0])) } if key == headerRatelimitResetAt { if len(value) == 0 { diff --git a/httpc/httpclient.go b/httpc/httpclient.go index 35bb04c..162ebca 100644 --- a/httpc/httpclient.go +++ b/httpc/httpclient.go @@ -4,6 +4,8 @@ import ( "fmt" "sync" "time" + + "github.com/larscom/go-bitvavo/v2/log" ) const ( @@ -86,12 +88,14 @@ func (c *httpClient) ToAuthClient(apiKey string, apiSecret string, windowTimeMs if windowTimeMs > maxWindowTimeMs { windowTimeMs = maxWindowTimeMs } + config := &authConfig{ windowTimeMs: windowTimeMs, apiKey: apiKey, apiSecret: apiSecret, } - c.authClient = newHttpClientAuth(c.updateRateLimit, c.updateRateLimitResetAt, config) + + c.authClient = newHttpClientAuth(c.updateRateLimit, c.updateRateLimitResetAt, c.logDebug, config) return c.authClient } @@ -104,7 +108,7 @@ func (c *httpClient) GetRateLimitResetAt() time.Time { } func (c *httpClient) GetTime() (int64, error) { - resp, err := httpGet[map[string]float64](fmt.Sprintf("%s/time", httpUrl), c.updateRateLimit, c.updateRateLimitResetAt, nil) + resp, err := httpGet[map[string]float64](fmt.Sprintf("%s/time", httpUrl), c.updateRateLimit, c.updateRateLimitResetAt, c.logDebug, nil) if err != nil { return 0, err } @@ -127,3 +131,9 @@ func (c *httpClient) updateRateLimitResetAt(resetAt time.Time) { func (c *httpClient) hasAuthClient() bool { return c.authClient != nil } + +func (c *httpClient) logDebug(message string, args ...any) { + if c.debug { + log.Logger().Debug(message, args...) + } +} diff --git a/httpc/httpclientauth.go b/httpc/httpclientauth.go index db71528..b203b03 100644 --- a/httpc/httpclientauth.go +++ b/httpc/httpclientauth.go @@ -17,8 +17,9 @@ type HttpClientAuth interface { type httpClientAuth struct { config *authConfig - updateRateLimit func(int64) - updateRateLimitResetAt func(time.Time) + updateRateLimit func(ratelimit int64) + updateRateLimitResetAt func(resetAt time.Time) + logDebug func(message string, args ...any) } type authConfig struct { @@ -28,21 +29,23 @@ type authConfig struct { } func newHttpClientAuth( - updateRateLimit func(int64), - updateRateLimitResetAt func(time.Time), + updateRateLimit func(ratelimit int64), + updateRateLimitResetAt func(resetAt time.Time), + logDebug func(message string, args ...any), config *authConfig, ) *httpClientAuth { return &httpClientAuth{ updateRateLimit: updateRateLimit, updateRateLimitResetAt: updateRateLimitResetAt, + logDebug: logDebug, config: config, } } func (c *httpClientAuth) GetBalance() ([]jsond.Balance, error) { - return httpGet[[]jsond.Balance](fmt.Sprintf("%s/balance", httpUrl), c.updateRateLimit, c.updateRateLimitResetAt, c.config) + return httpGet[[]jsond.Balance](fmt.Sprintf("%s/balance", httpUrl), c.updateRateLimit, c.updateRateLimitResetAt, c.logDebug, c.config) } func (c *httpClientAuth) GetAccount() (jsond.Account, error) { - return httpGet[jsond.Account](fmt.Sprintf("%s/account", httpUrl), c.updateRateLimit, c.updateRateLimitResetAt, c.config) + return httpGet[jsond.Account](fmt.Sprintf("%s/account", httpUrl), c.updateRateLimit, c.updateRateLimitResetAt, c.logDebug, c.config) }