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

Commit

Permalink
add client
Browse files Browse the repository at this point in the history
  • Loading branch information
larscom committed Nov 28, 2023
1 parent 693e417 commit 891741e
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,16 @@ trades:
go run ./examples/trades/main.go
account:
go run ./examples/account/main.go

time:
go run ./examples/time/main.go
build:
go build -o ./bin/candles ./examples/candles/main.go
go build -o ./bin/book ./examples/book/main.go
go build -o ./bin/ticker ./examples/ticker/main.go
go build -o ./bin/ticker24h ./examples/ticker24h/main.go
go build -o ./bin/trades ./examples/trades/main.go
go build -o ./bin/account ./examples/account/main.go
go build -o ./bin/time ./examples/time/main.go
test:
go test -v ./.../ --race
13 changes: 13 additions & 0 deletions examples/time/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package main

import (
"log"

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

func main() {
client := bitvavo.NewHttpClient()
v, _ := client.GetTime()
log.Println("Time", v)
}
60 changes: 60 additions & 0 deletions httpclient.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package bitvavo

import (
"fmt"
"io"
"net/http"

"github.com/goccy/go-json"
)

const (
httpUrl = "https://api.bitvavo.com/v2"
maxRateLimit = 1000
)

type HttpClient interface {
GetTime() (int64, error)
}

type httpClient struct {
rateLimit int64
}

func NewHttpClient() HttpClient {
return &httpClient{}
}

func (h *httpClient) GetTime() (int64, error) {
r, err := httpGet[map[string]float64](fmt.Sprintf("%s/time", httpUrl))
if err != nil {
return 0, err
}

return int64(r["time"]), nil
}

func httpGet[T any](url string) (T, error) {
var data T

response, err := http.Get(url)
if err != nil {
return data, err
}

if response.StatusCode != http.StatusOK {
return data, fmt.Errorf("did not get successfull response, code=%d", response.StatusCode)
}

defer response.Body.Close()
bytes, err := io.ReadAll(response.Body)
if err != nil {
return data, err
}

if err := json.Unmarshal(bytes, &data); err != nil {
return data, err
}

return data, nil
}
1 change: 1 addition & 0 deletions websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ func (ws *webSocket) readLoop() {
_, bytes, err := ws.conn.ReadMessage()
if err != nil {
defer ws.reconnect()
log.Logger().Error("Read failed", "error", err.Error())
return
}
ws.handleMessage(bytes)
Expand Down

0 comments on commit 891741e

Please sign in to comment.