-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathgobitlaunch.go
More file actions
123 lines (100 loc) · 2.72 KB
/
gobitlaunch.go
File metadata and controls
123 lines (100 loc) · 2.72 KB
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
package gobitlaunch
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"strings"
"time"
"github.com/hashicorp/go-retryablehttp"
)
const (
version = "1.1.0"
userAgent = "gobitlaunch/" + version
maxRateLimit = 900 * time.Millisecond
retryLimit = 3
baseURI = "https://app.bitlaunch.io/api"
)
// Client manages interaction with the API
type Client struct {
token string
hclient *retryablehttp.Client
Account *AccountService
Server *ServerService
Transaction *TransactionService
CreateOptions *CreateOptionsService
SSHKey *SSHKeyService
}
// NewClient takes an API token and returns a new BitLaunch API client
func NewClient(token string) *Client {
c := Client{
token: token,
hclient: retryablehttp.NewClient(),
}
// set up http client
c.hclient.Logger = nil
c.hclient.ErrorHandler = c.errorHandler
c.hclient.RetryMax = retryLimit
c.hclient.RetryWaitMin = maxRateLimit / 3
c.hclient.RetryWaitMax = maxRateLimit
// add services
c.Account = &AccountService{&c}
c.Server = &ServerService{&c}
c.Transaction = &TransactionService{&c}
c.CreateOptions = &CreateOptionsService{&c}
c.SSHKey = &SSHKeyService{&c}
return &c
}
// NewRequest creates an API Request
func (c *Client) NewRequest(method, path string, body []byte) (*http.Request, error) {
fullURI := baseURI + path
var reqBody io.Reader
if body != nil {
reqBody = bytes.NewReader(body)
}
req, err := http.NewRequest(method, fullURI, reqBody)
if err != nil {
return nil, err
}
req.Header.Add("Authorization", "Bearer: "+c.token)
req.Header.Add("User-Agent", userAgent)
return req, nil
}
// DoRequest performs a http request
func (c *Client) DoRequest(r *http.Request, data interface{}) error {
rreq, err := retryablehttp.FromRequest(r)
if err != nil {
return err
}
res, err := c.hclient.Do(rreq)
if err != nil {
return err
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return err
}
if res.StatusCode == http.StatusOK {
if data != nil {
if err := json.Unmarshal(body, data); err != nil {
return err
}
}
return nil
}
return fmt.Errorf("error %d %s", res.StatusCode, string(body))
}
func (c *Client) errorHandler(resp *http.Response, err error, numTries int) (*http.Response, error) {
if resp == nil {
return nil, fmt.Errorf("gave up after %d attempts, last error unavailable (resp == nil)", retryLimit+1)
}
defer resp.Body.Close()
buf, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("gave up after %d attempts, last error unavailable (error reading response body: %v)", retryLimit+1, err)
}
return nil, fmt.Errorf("gave up after %d attempts, last error: %#v", retryLimit+1, strings.TrimSpace(string(buf)))
}