This repository has been archived by the owner on Jun 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathsession.go
108 lines (86 loc) · 2.32 KB
/
session.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
package betting
import (
"bytes"
"crypto/tls"
"encoding/json"
"errors"
"github.com/valyala/fasthttp"
)
type Client struct {
ApiKey string
SessionKey string
CertURL BetfairRestURL
KeepAliveURL BetfairRestURL
}
type Session struct {
SessionToken string
LoginStatus ELoginStatus
}
func (c *Client) GetSessionFromCertificate(cert tls.Certificate, login, password string) error {
var session *Session = &Session{}
client := fasthttp.Client{TLSConfig: &tls.Config{Certificates: []tls.Certificate{cert}, InsecureSkipVerify: true}}
req, resp := fasthttp.AcquireRequest(), fasthttp.AcquireResponse()
req.SetRequestURI(string(c.CertURL))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Header.Set("X-Application", c.ApiKey)
req.Header.SetMethod("POST")
bufferString := bytes.NewBuffer([]byte{})
bufferString.WriteString(`username=`)
bufferString.WriteString(login)
bufferString.WriteString(`&password=`)
bufferString.WriteString(password)
req.SetBody(bufferString.Bytes())
err := client.Do(req, resp)
if err != nil {
return err
}
err = json.Unmarshal(resp.Body(), session)
if err != nil {
return err
}
switch session.LoginStatus {
case LS_SUCCESS:
c.SessionKey = session.SessionToken
default:
err = errors.New(string(session.LoginStatus))
}
return err
}
func (c *Client) GetSession(pemCert, keyCert, login, password string) error {
cert, err := tls.LoadX509KeyPair(pemCert, keyCert)
if err != nil {
return err
}
return c.GetSessionFromCertificate(cert, login, password)
}
type KeepAlive struct {
Token string
Product string
Status string
Error string
}
// KeepAlive for support connect, session key will available for 20 minutes
func (c *Client) KeepAlive() error {
var keepAlive *KeepAlive = &KeepAlive{}
req, resp := fasthttp.AcquireRequest(), fasthttp.AcquireResponse()
req.SetRequestURI(string(c.KeepAliveURL))
req.Header.Set("Accept", "application/json")
req.Header.Set("X-Application", c.ApiKey)
req.Header.Set("X-Authentication", c.SessionKey)
req.Header.SetMethod("POST")
err := fasthttp.Do(req, resp)
if err != nil {
return err
}
err = json.Unmarshal(resp.Body(), keepAlive)
if err != nil {
return err
}
switch keepAlive.Status {
case "SUCCESS":
c.SessionKey = keepAlive.Token
default:
err = errors.New(keepAlive.Error)
}
return err
}