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

Commit

Permalink
add WithContext
Browse files Browse the repository at this point in the history
  • Loading branch information
larscom committed Mar 16, 2024
1 parent b9662f6 commit 4c89005
Show file tree
Hide file tree
Showing 6 changed files with 204 additions and 18 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ import "github.com/larscom/go-bitvavo/v2"

## 🌐 HTTP client

Every function on the HTTP client has a `WithContext` variant so you can pass your own context as argument.

```go
client := bitvavo.NewHttpClient()
```
Expand All @@ -52,6 +54,14 @@ func main() {
log.Fatal(err)
}
log.Println(time)

// provide your own context
time, err := client.GetTimeWithContext(context.TODO())
if err != nil {
log.Fatal(err)
}
log.Println(time)

}

```
Expand Down
4 changes: 2 additions & 2 deletions examples/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ require (
github.com/orsinium-labs/enum v1.3.0 // indirect
github.com/rs/zerolog v1.32.0 // indirect
github.com/smallnest/safemap v0.0.0-20221221063619-2e3a9fa0ff20 // indirect
golang.org/x/net v0.21.0 // indirect
golang.org/x/sys v0.17.0 // indirect
golang.org/x/net v0.22.0 // indirect
golang.org/x/sys v0.18.0 // indirect
)
8 changes: 4 additions & 4 deletions examples/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ github.com/smallnest/safemap v0.0.0-20221221063619-2e3a9fa0ff20 h1:TSS9XXTYReYgY
github.com/smallnest/safemap v0.0.0-20221221063619-2e3a9fa0ff20/go.mod h1:utWAg2jafYmELXFuUaJsT+8tClaCEumLPN0exPk9pzI=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
golang.org/x/net v0.21.0 h1:AQyQV4dYCvJ7vGmJyKki9+PBdyvhkSd8EIx/qb0AYv4=
golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44=
golang.org/x/net v0.22.0 h1:9sGLhx7iRIHEiX0oAJ3MRZMUCElJgy7Br1nO+AMN3Tc=
golang.org/x/net v0.22.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y=
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
13 changes: 9 additions & 4 deletions http/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package http

import (
"bytes"
"context"
"fmt"
"io"
"net/http"
Expand All @@ -27,28 +28,31 @@ var (
)

func httpDelete[T any](
ctx context.Context,
url string,
params url.Values,
updateRateLimit func(ratelimit int64),
updateRateLimitResetAt func(resetAt time.Time),
config *authConfig,
) (T, error) {
req, _ := http.NewRequest("DELETE", createRequestUrl(url, params), nil)
req, _ := http.NewRequestWithContext(ctx, "DELETE", createRequestUrl(url, params), nil)
return httpDo[T](req, emptyBody, updateRateLimit, updateRateLimitResetAt, config)
}

func httpGet[T any](
ctx context.Context,
url string,
params url.Values,
updateRateLimit func(ratelimit int64),
updateRateLimitResetAt func(resetAt time.Time),
config *authConfig,
) (T, error) {
req, _ := http.NewRequest("GET", createRequestUrl(url, params), nil)
req, _ := http.NewRequestWithContext(ctx, "GET", createRequestUrl(url, params), nil)
return httpDo[T](req, emptyBody, updateRateLimit, updateRateLimitResetAt, config)
}

func httpPost[T any](
ctx context.Context,
url string,
body any,
params url.Values,
Expand All @@ -63,11 +67,12 @@ func httpPost[T any](
}
log.Debug().Str("body", string(payload)).Msg("created request body")

req, _ := http.NewRequest("POST", createRequestUrl(url, params), bytes.NewBuffer(payload))
req, _ := http.NewRequestWithContext(ctx, "POST", createRequestUrl(url, params), bytes.NewBuffer(payload))
return httpDo[T](req, payload, updateRateLimit, updateRateLimitResetAt, config)
}

func httpPut[T any](
ctx context.Context,
url string,
body any,
params url.Values,
Expand All @@ -82,7 +87,7 @@ func httpPut[T any](
}
log.Debug().Str("body", string(payload)).Msg("created request body")

req, _ := http.NewRequest("PUT", createRequestUrl(url, params), bytes.NewBuffer(payload))
req, _ := http.NewRequestWithContext(ctx, "PUT", createRequestUrl(url, params), bytes.NewBuffer(payload))
return httpDo[T](req, payload, updateRateLimit, updateRateLimitResetAt, config)
}

Expand Down
Loading

0 comments on commit 4c89005

Please sign in to comment.