forked from AwolDes/goanda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
goanda.go
131 lines (104 loc) · 2.82 KB
/
goanda.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
123
124
125
126
127
128
129
130
131
package goanda
import (
"bytes"
"net/http"
"time"
)
type Headers struct {
contentType string
agent string
DatetimeFormat string
auth string
}
type Connection interface {
Request(endpoint string) []byte
Send(endpoint string, data []byte) []byte
Update(endpoint string, data []byte) []byte
GetOrderDetails(instrument string, units string) OrderDetails
GetAccountSummary() AccountSummary
CreateOrder(body OrderPayload) OrderResponse
}
type OandaConnection struct {
Hostname string
Port int
Ssl bool
Token string
AccountID string
DatetimeFormat string
Headers *Headers
}
const OANDA_AGENT string = "v20-golang/0.0.1"
func NewConnection(accountID string, token string, live bool) *OandaConnection {
hostname := ""
// should we use the live API?
if live {
hostname = "https://api-fxtrade.oanda.com/"
} else {
hostname = "https://api-fxpractice.oanda.com/"
}
var buffer bytes.Buffer
// Generate the auth header
buffer.WriteString("Bearer ")
buffer.WriteString(token)
authHeader := buffer.String()
// Create headers for oanda to be used in requests
headers := &Headers{
contentType: "application/json",
agent: OANDA_AGENT,
DatetimeFormat: "RFC3339",
auth: authHeader,
}
// Create the connection object
connection := &OandaConnection{
Hostname: hostname,
Port: 443,
Ssl: true,
Token: token,
Headers: headers,
AccountID: accountID,
}
return connection
}
// TODO: include params as a second option
func (c *OandaConnection) Request(endpoint string) ([]byte, error, error) {
var body []byte
var err2, err3 error
client := http.Client {
Timeout: time.Second * 5, // 5 sec timeout
}
url := createUrl(c.Hostname, endpoint)
req, err1 := http.NewRequest(http.MethodGet, url, nil)
if err1 == nil {
body, err2, err3 = makeRequest(c, endpoint, client, req)
}
return body, err2, err3
}
func (c *OandaConnection) Send(endpoint string, data []byte) ([]byte, error, error) {
var body []byte
var err2, err3 error
client := http.Client{
Timeout: time.Second * 5, // 5 sec timeout
}
url := createUrl(c.Hostname, endpoint)
// New request object
req, err1 := http.NewRequest(http.MethodPost, url, bytes.NewBuffer(data))
//checkErr(err)
if err1 == nil {
body, err2, err3 = makeRequest(c, endpoint, client, req)
}
return body, err2, err3
}
func (c *OandaConnection) Update(endpoint string, data []byte) ([]byte, error, error) {
var body []byte
var err2, err3 error
client := http.Client{
Timeout: time.Second * 5,
}
url := createUrl(c.Hostname, endpoint)
req, err1 := http.NewRequest(http.MethodPut, url, bytes.NewBuffer(data))
//checkErr(err)
if err1 == nil {
body, err2, err3 = makeRequest(c, endpoint, client, req)
}
return body, err2, err3
}