Skip to content
This repository has been archived by the owner on Jul 4, 2024. It is now read-only.

Commit

Permalink
add debug
Browse files Browse the repository at this point in the history
  • Loading branch information
larscom committed Dec 2, 2023
1 parent c34e1cb commit b946b5a
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 20 deletions.
2 changes: 1 addition & 1 deletion examples/http/account/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
)

Expand Down
24 changes: 13 additions & 11 deletions httpc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -20,36 +19,39 @@ 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)
if err != nil {
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 {
Expand All @@ -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 {
Expand Down
14 changes: 12 additions & 2 deletions httpc/httpclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"fmt"
"sync"
"time"

"github.com/larscom/go-bitvavo/v2/log"
)

const (
Expand Down Expand Up @@ -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
}

Expand All @@ -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
}
Expand All @@ -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...)
}
}
15 changes: 9 additions & 6 deletions httpc/httpclientauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
}

0 comments on commit b946b5a

Please sign in to comment.