-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathgoanda.go
166 lines (137 loc) · 3.73 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package goanda
import (
"bytes"
"encoding/json"
"io"
"net/http"
"time"
)
const (
apiUserAgent = "v20-golang/0.0.1"
httpTimeout = time.Second * 5
)
// ConnectionConfig is used to configure new connections
// Defaults;
//
// UserAgent = v20-golang/0.0.1
// Timeout = 5 seconds
// Live = False
type ConnectionConfig struct {
UserAgent string
Timeout time.Duration
Live bool
}
// Connection describes a connection to the Oanda v20 API
// It is thread safe
type Connection struct {
hostname string
accountID string
authHeader string
userAgent string
client http.Client
}
// NewConnection creates a new connection
// This function calls Connection.CheckConnection(), returning any errors
// Supplying a config is optional, with sane defaults (paper trading) being used otherwise.
func NewConnection(accountID string, token string, config *ConnectionConfig) (*Connection, error) {
// Make new connection with defaults
nc := &Connection{
hostname: "https://api-fxpractice.oanda.com/v3",
accountID: accountID,
authHeader: "Bearer " + token,
userAgent: apiUserAgent,
client: http.Client{
Timeout: httpTimeout,
},
}
// Overwrite things if we've been given configuration for them
if config != nil {
if config.Live {
nc.hostname = "https://api-fxtrade.oanda.com/v3"
}
if config.Timeout != 0 {
nc.client = http.Client{
Timeout: config.Timeout,
}
}
if config.UserAgent != "" {
nc.userAgent = config.UserAgent
}
}
return nc, nc.CheckConnection()
}
// CheckConnection performs a request, returning any errors encountered
func (c *Connection) CheckConnection() error {
_, err := c.Get("/accounts/" + c.accountID)
return err
}
// Get performs a generic http get on the api
func (c *Connection) Get(endpoint string) ([]byte, error) {
req, err := http.NewRequest(http.MethodGet, c.hostname+endpoint, nil)
if err != nil {
return nil, err
}
return c.makeRequest(endpoint, c.client, req)
}
// Post performs a generic http post on the api
func (c *Connection) Post(endpoint string, data []byte) ([]byte, error) {
req, err := http.NewRequest(http.MethodPost, c.hostname+endpoint, bytes.NewBuffer(data))
if err != nil {
return nil, err
}
return c.makeRequest(endpoint, c.client, req)
}
// Put performs a generic http put on the api
func (c *Connection) Put(endpoint string, data []byte) ([]byte, error) {
req, err := http.NewRequest(http.MethodPut, c.hostname+endpoint, bytes.NewBuffer(data))
if err != nil {
return nil, err
}
return c.makeRequest(endpoint, c.client, req)
}
func (c *Connection) getAndUnmarshal(endpoint string, receive interface{}) error {
response, err := c.Get(endpoint)
if err != nil {
return err
}
return json.Unmarshal(response, receive)
}
func (c *Connection) postAndUnmarshal(endpoint string, send interface{}, receive interface{}) error {
data, err := json.Marshal(send)
if err != nil {
return err
}
response, err := c.Post(endpoint, data)
if err != nil {
return err
}
return json.Unmarshal(response, receive)
}
func (c *Connection) putAndUnmarshal(endpoint string, send interface{}, receive interface{}) error {
data, err := json.Marshal(send)
if err != nil {
return err
}
response, err := c.Put(endpoint, data)
if err != nil {
return err
}
return json.Unmarshal(response, receive)
}
func (c *Connection) makeRequest(endpoint string, client http.Client, req *http.Request) ([]byte, error) {
req.Header.Set("User-Agent", c.userAgent)
req.Header.Set("Authorization", c.authHeader)
req.Header.Set("Content-Type", "application/json")
res, err := client.Do(req)
if err != nil {
return nil, err
}
if res.StatusCode >= 400 {
return nil, newAPIError(req, res)
}
body, err := io.ReadAll(res.Body)
if err != nil {
return nil, err
}
return body, nil
}