-
Notifications
You must be signed in to change notification settings - Fork 10
/
utils.go
79 lines (64 loc) · 1.55 KB
/
utils.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
package gofair
import (
"encoding/json"
"errors"
"io/ioutil"
"log"
"net/http"
"strings"
)
func createUrl(endpoint string, method string) string {
return endpoint + method
}
type Detail struct{}
type ErrorResponse struct {
//b'{"faultcode":"Client","faultstring":"DSC-0018","detail":{}}'
FaultCode string `json:"faultcode"`
FaultString string `json:"faultstring"`
Detail *Detail `json:"detail"`
}
func logError(data []byte) error {
var errorResp ErrorResponse
if err := json.Unmarshal(data, &errorResp); err != nil {
return err
}
log.Println("Error:", errorResp, errorResp.Detail)
return nil
}
func (b *Betting) Request(url string, params *Params, v interface{}) error {
//params.Locale = b.Client.config.Locale
bytes, err := json.Marshal(params)
if err != nil {
return err
}
body := strings.NewReader(string(bytes))
req, err := http.NewRequest("POST", url, body)
if err != nil {
return err
}
// set headers
req.Header.Set("X-Application", b.Client.config.AppKey)
req.Header.Set("X-Authentication", b.Client.session.SessionToken)
req.Header.Set("Accept", "application/json")
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Connection", "keep-alive")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
if resp.StatusCode != 200 {
logError(data)
return errors.New(resp.Status)
} else {
if err := json.Unmarshal(data, v); err != nil {
return err
}
}
return nil
}