-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
58 lines (50 loc) · 1.24 KB
/
client.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
package rhclient
import (
"encoding/json"
"sync"
"github.com/valyala/fasthttp"
)
var once sync.Once
var rh *Robinhood
var baseURL = "https://api.robinhood.com"
type Robinhood struct {
request func(req *fasthttp.Request, resp *fasthttp.Response)
token string
}
func Client() *Robinhood {
once.Do(func() {
rh = &Robinhood{}
rh.request = func(req *fasthttp.Request, resp *fasthttp.Response) {
if len(rh.token) > 0 {
req.Header.Set("Authorization", rh.token)
}
req.Header.SetContentType("application/json")
req.Header.Set("Accept", "application/json")
(&fasthttp.Client{}).Do(req, resp)
}
})
return rh
}
func (rh *Robinhood) post(uri string, payload interface{}) (*fasthttp.Response, error) {
req := fasthttp.AcquireRequest()
req.SetRequestURI(uri)
req.Header.SetMethod("POST")
if payload != nil {
if body, err := json.Marshal(payload); err != nil {
return nil, err
} else {
req.SetBody(body)
}
}
resp := fasthttp.AcquireResponse()
rh.request(req, resp)
return resp, nil
}
func (rh *Robinhood) get(uri string) (*fasthttp.Response, error) {
req := fasthttp.AcquireRequest()
req.SetRequestURI(uri)
req.Header.SetMethod("GET")
resp := fasthttp.AcquireResponse()
rh.request(req, resp)
return resp, nil
}