Skip to content

Commit

Permalink
Bitstamp: FetchWSAuth mock and live test
Browse files Browse the repository at this point in the history
  • Loading branch information
gbjk committed Sep 19, 2023
1 parent 2fefe84 commit 88f6511
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 11 deletions.
11 changes: 11 additions & 0 deletions exchanges/bitstamp/bitstamp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -956,3 +956,14 @@ func TestGetOrderInfo(t *testing.T) {
t.Error(err)
}
}

func TestFetchWSAuth(t *testing.T) {
t.Parallel()
resp, err := b.FetchWSAuth(context.TODO())
if assert.NoError(t, err, "FetchWSAuth should not error") {
assert.NotNil(t, resp, "resp should not be nil")
assert.Positive(t, resp.UserID, "UserID should be positive")
assert.Len(t, resp.Token, 32, "Token should be 32 chars")
assert.Positive(t, resp.ValidSecs, "ValidSecs should be positive")
}
}
9 changes: 4 additions & 5 deletions exchanges/bitstamp/bitstamp_types.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package bitstamp

import (
"encoding/json"
"errors"
"time"

Expand Down Expand Up @@ -224,10 +223,10 @@ type websocketTradeData struct {
ID int64 `json:"id"`
}

type websocketAuthResponse struct {
Token string `json:"token"`
UserID json.Number `json:"user_id"`
ValidSecs int64 `json:"valid_sec"`
type WebsocketAuthResponse struct {

Check warning on line 226 in exchanges/bitstamp/bitstamp_types.go

View workflow job for this annotation

GitHub Actions / lint

exported: exported type WebsocketAuthResponse should have comment or be unexported (revive)
Token string `json:"token"`
UserID int64 `json:"user_id"`
ValidSecs int64 `json:"valid_sec"`
}

type websocketOrderBookResponse struct {
Expand Down
12 changes: 6 additions & 6 deletions exchanges/bitstamp/bitstamp_websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,12 +267,12 @@ func (b *Bitstamp) generateDefaultSubscriptions() ([]stream.ChannelSubscription,
// Subscribe sends a websocket message to receive data from the channel
func (b *Bitstamp) Subscribe(channelsToSubscribe []stream.ChannelSubscription) error {
var errs error
var auth *websocketAuthResponse
var auth *WebsocketAuthResponse

for i := range channelsToSubscribe {
if _, ok := channelsToSubscribe[i].Params["auth"]; ok {
var err error
auth, err = b.fetchWSAuth(context.TODO())
auth, err = b.FetchWSAuth(context.TODO())
if err != nil {
errs = common.AppendError(errs, err)
}
Expand All @@ -288,7 +288,7 @@ func (b *Bitstamp) Subscribe(channelsToSubscribe []stream.ChannelSubscription) e
},
}
if _, ok := channelsToSubscribe[i].Params["auth"]; ok && auth != nil {
req.Data.Channel = "private-" + req.Data.Channel + "-" + string(auth.UserID)
req.Data.Channel = "private-" + req.Data.Channel + "-" + strconv.Itoa(int(auth.UserID))
req.Data.Auth = auth.Token
}
err := b.Websocket.Conn.SendJSONMessage(req)
Expand Down Expand Up @@ -412,10 +412,10 @@ func (b *Bitstamp) seedOrderBook(ctx context.Context) error {
return nil
}

// fetchWSAuth Retrieves a userID and auth-token from REST for subscribing to a websocket channel
// FetchWSAuth Retrieves a userID and auth-token from REST for subscribing to a websocket channel
// The token life-expectancy is only about 60s; use it immediately and do not store it
func (b *Bitstamp) fetchWSAuth(ctx context.Context) (*websocketAuthResponse, error) {
resp := &websocketAuthResponse{}
func (b *Bitstamp) FetchWSAuth(ctx context.Context) (*WebsocketAuthResponse, error) {
resp := &WebsocketAuthResponse{}
err := b.SendAuthenticatedHTTPRequest(ctx, exchange.RestSpot, bitstampAPIWSAuthToken, true, nil, resp)
if err != nil {
return nil, fmt.Errorf("error fetching auth token: %w", err)
Expand Down
17 changes: 17 additions & 0 deletions testdata/http_mock/bitstamp/bitstamp.json
Original file line number Diff line number Diff line change
Expand Up @@ -68230,6 +68230,23 @@
}
}
]
},
"/api/v2/websockets_token/": {
"POST": [
{
"data": {
"token": "HorseWalksIntoBarBtenderLongFace",
"valid_sec": 60,
"user_id": 1337210
},
"bodyParams": "key=1\u0026nonce=2\u0026signature=SIGGYMSIGGY",
"headers": {
"Content-Type": [
"application/x-www-form-urlencoded"
]
}
}
]
}
}
}

0 comments on commit 88f6511

Please sign in to comment.