Skip to content

Commit

Permalink
Allow the user to customise the streaming client
Browse files Browse the repository at this point in the history
  • Loading branch information
000xE committed Feb 3, 2025
1 parent 3cb2080 commit 8eb5b80
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 17 deletions.
19 changes: 8 additions & 11 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,14 @@ func (jsonClient *BetfairClient) Logout() (*http.Response, error) {
return resp, nil
}

func (client *BetfairClient) LoginStream(sc *streaming.StreamingClient) error {
if client.Client == nil {
return errors.New("client not initialised: please resume or create a new session")
}

return sc.Authenticate(client.Tls, client.ApplicationKey, client.SessionToken)
}

const (
api_account = "account"
api_betting = "betting"
Expand Down Expand Up @@ -332,17 +340,6 @@ func (client *BetfairClient) getRest(api string, method string, params any, resp
return nil
}

func (client *BetfairClient) GetStreamingClient() (*streaming.StreamingClient, error) {
if client.Client == nil {
return nil, errors.New("client not initialised: please resume or create a new session")
}

sc := streaming.NewStreamingClient()
err := sc.Authenticate(client.Tls, client.ApplicationKey, client.SessionToken)

return sc, err
}

func readJson(res *http.Response, response any) error {
defer res.Body.Close()

Expand Down
6 changes: 4 additions & 2 deletions streaming/tests/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const sessionToken, appKey = "", ""
const certificate_crt_path = ""
const certificate_key_path = ""

func NewStreamingClient(t *testing.T) *streaming.StreamingClient {
func NewTestStreamingClient(t *testing.T) *streaming.StreamingClient {
if sessionToken == "" || appKey == "" {
t.Skip("Invalid credentials")
}
Expand All @@ -26,7 +26,9 @@ func NewStreamingClient(t *testing.T) *streaming.StreamingClient {
t.Fatal(err)
}

if sc, err := c.GetStreamingClient(); err != nil {
sc := streaming.NewStreamingClient()

if err := c.LoginStream(sc); err != nil {
t.Fatal(err)
return nil
} else {
Expand Down
6 changes: 4 additions & 2 deletions streaming/tests/market_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

func TestSubscribeToMarkets(t *testing.T) {
c := NewStreamingClient(t)
c := NewTestStreamingClient(t)
c.HeartbeatMs = 524

marketFilter := streaming.MarketFilter{MarketIDs: []string{"1.238426694"}}
Expand All @@ -23,7 +23,9 @@ func TestSubscribeToMarkets(t *testing.T) {
for x := range marketChanges {
if x.HeartbeatMs != c.HeartbeatMs {
t.Fatal("Heartbeat does not match")
} else {
c.Connection.Close()
return
}
close(marketChanges)
}
}
6 changes: 4 additions & 2 deletions streaming/tests/order_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

func TestSubscribeToOrders(t *testing.T) {
c := NewStreamingClient(t)
c := NewTestStreamingClient(t)
c.HeartbeatMs = 524

orderChanges, err := c.SubscribeToOrders(streaming.OrderFilter{})
Expand All @@ -18,7 +18,9 @@ func TestSubscribeToOrders(t *testing.T) {
for x := range orderChanges {
if x.HeartbeatMs != c.HeartbeatMs {
t.Fatal("Heartbeat does not match")
} else {
c.Connection.Close()
return
}
close(orderChanges)
}
}

0 comments on commit 8eb5b80

Please sign in to comment.