Skip to content

Commit

Permalink
Prevent requests from being executed if there is no client created, w…
Browse files Browse the repository at this point in the history
…hich is now only done from new or existing sessions
  • Loading branch information
000xE committed Feb 2, 2025
1 parent 2f59e86 commit d2a1202
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 15 deletions.
42 changes: 27 additions & 15 deletions common/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ import (
type SessionStatus string

const (
KAS_SUCCESS = "SUCCESS"
KAS_FAIL = "FAIL"
SS_SUCCESS = "SUCCESS"
SS_FAIL = "FAIL"
)

type SessionError string
type SessionStatusError string

const (
KAE_INPUT_VALIDATION_ERROR = "INPUT_VALIDATION_ERROR"
KAE_INTERNAL_ERROR = "INTERNAL_ERROR"
KAE_NO_SESSION = "NO_SESSION"
SS_ERR_INPUT_VALIDATION_ERROR = "INPUT_VALIDATION_ERROR"
SS_ERR_INTERNAL_ERROR = "INTERNAL_ERROR"
SS_ERR_NO_SESSION = "NO_SESSION"
)

type (
Expand All @@ -37,18 +37,22 @@ type (
}

SessionStatusResponse struct {
Token string `json:"token"`
Product string `json:"product"`
Status SessionStatus `json:"status"`
Error SessionError `json:"error"`
Token string `json:"token"`
Product string `json:"product"`
Status SessionStatus `json:"status"`
Error SessionStatusError `json:"error"`
}
)

func NewJsonClient(app_key string) *JsonClient {
return &JsonClient{Client: &http.Client{}, ApplicationKey: app_key}
return &JsonClient{ApplicationKey: app_key}
}

func (client *JsonClient) Do(req *http.Request) (*http.Response, error) {
if client.Client == nil {
return nil, errors.New("client not initialised: please resume or create a new session")
}

req.Header.Add("X-Authentication", client.SessionToken)
req.Header.Add("X-Application", client.ApplicationKey)
req.Header.Add("Accept", "application/json")
Expand All @@ -58,32 +62,36 @@ func (client *JsonClient) Do(req *http.Request) (*http.Response, error) {
}

func (jsonClient *JsonClient) ResumeSession(sessionToken string) (*http.Response, error) {
jsonClient.Client = &http.Client{}
jsonClient.SessionToken = sessionToken

keepAliveUrl := url.URL{Path: "https://identitysso.betfair.com/api/keepAlive"}
req, _ := http.NewRequest("POST", keepAliveUrl.RequestURI(), nil)

resp, err := jsonClient.Do(req)
if err != nil {
jsonClient.Client = nil
jsonClient.SessionToken = ""
return resp, err
}

json := SessionStatusResponse{}
if err := helpers.ReadJson(resp, &json); err != nil {
jsonClient.Client = nil
jsonClient.SessionToken = ""
return resp, err
}

if json.Error != "" {
jsonClient.Client = nil
jsonClient.SessionToken = ""
return resp, errors.New(string(json.Error))
}

return resp, nil
}

func (jsonClient *JsonClient) NewSession(tls *tls.Config, apiKey string, applicationName string, username string, password string) (*http.Response, error) {
func (jsonClient *JsonClient) NewSession(tls *tls.Config, applicationName string, username string, password string) (*http.Response, error) {
postUrl := url.URL{Path: "https://identitysso-cert.betfair.com/api/certlogin"}
q := postUrl.Query()
q.Set("username", username)
Expand All @@ -97,19 +105,21 @@ func (jsonClient *JsonClient) NewSession(tls *tls.Config, apiKey string, applica
req.Header.Add("X-Application", applicationName)
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")

client := &http.Client{
jsonClient.Client = &http.Client{
Transport: &http.Transport{
TLSClientConfig: tls,
},
}

resp, err := client.Do(req)
resp, err := jsonClient.Client.Do(req)
if err != nil {
jsonClient.Client = nil
return resp, err
}

json := SessionResponse{}
if err := helpers.ReadJson(resp, &json); err != nil {
jsonClient.Client = nil
return resp, err
}

Expand All @@ -136,6 +146,8 @@ func (jsonClient *JsonClient) ClearSession() (*http.Response, error) {
return resp, errors.New(string(json.Error))
}

jsonClient.Client = nil
jsonClient.SessionToken = ""
return resp, err

return resp, nil
}
13 changes: 13 additions & 0 deletions stateless/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package stateless

import (
"bytes"
"crypto/tls"
"encoding/json"
"fmt"
"math/rand/v2"
Expand All @@ -26,6 +27,18 @@ func (client *StatelessClient) Do(req *http.Request) (*http.Response, error) {
return client.Client.Do(req)
}

func (client *StatelessClient) ResumeSession(sessionToken string) (*http.Response, error) {
return client.Client.ResumeSession(sessionToken)
}

func (client *StatelessClient) NewSession(tls *tls.Config, applicationName string, username string, password string) (*http.Response, error) {
return client.Client.NewSession(tls, applicationName, username, password)
}

func (client *StatelessClient) ClearSession() (*http.Response, error) {
return client.Client.ClearSession()
}

const (
api_account = "account"
api_betting = "betting"
Expand Down
12 changes: 12 additions & 0 deletions streaming/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,18 @@ func (client *StreamingClient) Do(req *http.Request) (*http.Response, error) {
return client.Client.Do(req)
}

func (client *StreamingClient) ResumeSession(sessionToken string) (*http.Response, error) {
return client.Client.ResumeSession(sessionToken)
}

func (client *StreamingClient) NewSession(tls *tls.Config, applicationName string, username string, password string) (*http.Response, error) {
return client.Client.NewSession(tls, applicationName, username, password)
}

func (client *StreamingClient) ClearSession() (*http.Response, error) {
return client.Client.ClearSession()
}

type (
ConnectionMessage struct {
Op string `json:"op"`
Expand Down

0 comments on commit d2a1202

Please sign in to comment.