-
Notifications
You must be signed in to change notification settings - Fork 10
/
client.go
122 lines (104 loc) · 2.36 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package gofair
import (
"crypto/tls"
"time"
"strings"
)
// betfair api endpoints
const (
login_url = "https://identitysso-api.betfair.com/api/"
identity_url = "https://identitysso.betfair.com/api/"
api_betting_url = "https://api.betfair.com/exchange/betting/rest/v1.0/"
api_account_url = "https://api.betfair.com/exchange/account/rest/v1.0/"
navigation_url = "https://api.betfair.com/exchange/betting/rest/v1/en/navigation/menu.json"
)
// holds login data
type Config struct {
Username string
Password string
AppKey string
CertFile string
KeyFile string
Locale string
}
// holds session data
type session struct {
SessionToken string
LoginTime time.Time
}
// main client object
type Client struct {
config *Config
session *session
certificates *tls.Certificate
Betting *Betting
Account *Account
Streaming *Streaming
Historical *Historical
}
// betting object
type Betting struct {
Client *Client
}
// account object
type Account struct {
Client *Client
}
// streaming object
type Streaming struct {
Client *Client
}
// historical object
type Historical struct {
Client *Client
}
// creates new client
func NewClient(config *Config) (*Client, error) {
c := new(Client)
// create session
c.session = new(session)
var cert tls.Certificate
var err error
// create certificates
// ----- is obviously not a path, therefore load direct from the variables
if strings.HasPrefix(config.CertFile, "------") {
cert, err = tls.X509KeyPair([]byte(config.CertFile), []byte(config.KeyFile))
if err != nil {
return nil, err
}
} else {
cert, err = tls.LoadX509KeyPair(config.CertFile, config.KeyFile)
if err != nil {
return nil, err
}
}
c.certificates = &cert
// set config
c.config = config
// create betting
c.Betting = new(Betting)
c.Betting.Client = c
// create account
c.Account = new(Account)
c.Account.Client = c
// create streaming
c.Streaming = new(Streaming)
c.Streaming.Client = c
// create historical
c.Historical = new(Historical)
c.Historical.Client = c
return c, nil
}
func (c *Client) SessionExpired() bool {
// returns True if client not logged in or expired
// betfair requires keep alive every 4hrs (20mins ITA)
if c.session.SessionToken == "" {
return true
}
duration := time.Since(c.session.LoginTime)
if duration.Minutes() > 200 {
return true
} else {
return false
}
}