forked from AwolDes/goanda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
52 lines (44 loc) · 1.12 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
package goanda
import (
"bytes"
"encoding/json"
"io/ioutil"
"log"
"net/http"
"strings"
)
func checkErr(err error) {
if err != nil {
log.Fatal(err)
}
}
func checkApiErr(body []byte, route string) {
bodyString := string(body[:])
if strings.Contains(bodyString, "errorMessage") {
log.SetFlags(log.LstdFlags | log.Llongfile)
log.Fatal("\nOANDA API Error: " + bodyString + "\nOn route: " + route)
}
}
func unmarshalJson(body []byte, data interface{}) {
jsonErr := json.Unmarshal(body, &data)
checkErr(jsonErr)
}
func createUrl(host string, endpoint string) string {
var buffer bytes.Buffer
// Generate the auth header
buffer.WriteString(host)
buffer.WriteString(endpoint)
url := buffer.String()
return url
}
func makeRequest(c *OandaConnection, endpoint string, client http.Client, req *http.Request) []byte {
req.Header.Set("User-Agent", c.headers.agent)
req.Header.Set("Authorization", c.headers.auth)
req.Header.Set("Content-Type", c.headers.contentType)
res, getErr := client.Do(req)
checkErr(getErr)
body, readErr := ioutil.ReadAll(res.Body)
checkErr(readErr)
checkApiErr(body, endpoint)
return body
}