forked from Nyarum/betting
-
Notifications
You must be signed in to change notification settings - Fork 0
/
betting.go
63 lines (49 loc) · 1.34 KB
/
betting.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
package betting
import (
"bytes"
"fmt"
"github.com/pquerna/ffjson/ffjson"
"github.com/valyala/fasthttp"
)
type Betting struct {
*Client
}
// Request function for send requests to betfair via REST JSON
func (b *Betting) Request(reqStruct interface{}, url BetfairRestURL, method string, filter *Filter) error {
req, resp := fasthttp.AcquireRequest(), fasthttp.AcquireResponse()
urlBuild := bytes.NewBuffer([]byte{})
urlBuild.WriteString(string(url))
urlBuild.WriteString("/")
urlBuild.WriteString(method)
urlBuild.WriteString("/")
req.SetRequestURI(urlBuild.String())
req.Header.Set("Accept", "application/json")
req.Header.Set("Content-Type", "application/json")
req.Header.Set("X-Application", b.ApiKey)
req.Header.Set("X-Authentication", b.SessionKey)
req.Header.SetMethod("POST")
if filter != nil {
filterBody, err := ffjson.Marshal(&filter)
if err != nil {
return err
}
fmt.Println(string(filterBody))
req.SetBody(filterBody)
}
err := fasthttp.Do(req, resp)
if err != nil {
return err
}
if resp.StatusCode() == 400 {
err = ffjson.Unmarshal(resp.Body(), &bettingError)
if err != nil {
return err
}
return fmt.Errorf("Error with code - %s and string - %s", bettingError.Faultcode, bettingError.Faultstring)
}
err = ffjson.Unmarshal(resp.Body(), reqStruct)
if err != nil {
return err
}
return nil
}