-
Notifications
You must be signed in to change notification settings - Fork 1
/
http.go
198 lines (181 loc) · 5.01 KB
/
http.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package iland
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"strings"
"time"
)
func (c *client) Get(endpoint string) (io.ReadCloser, error) {
return c.request(endpoint, http.MethodGet, []byte{})
}
func (c *client) Post(endpoint string, body []byte) (io.ReadCloser, error) {
return c.request(endpoint, http.MethodPost, body)
}
func (c *client) Put(endpoint string, body []byte) (io.ReadCloser, error) {
return c.request(endpoint, http.MethodPut, body)
}
func (c *client) Delete(endpoint string) (io.ReadCloser, error) {
return c.request(endpoint, http.MethodDelete, []byte{})
}
func (c *client) getObject(endpoint string, object interface{}) error {
resp, err := c.Get(endpoint)
if err != nil {
return err
}
return unmarshalBody(resp, object)
}
type Token struct {
AccessToken string `json:"access_token"`
ExpiresIn int64 `json:"expires_in"`
RefreshToken string `json:"refresh_token"`
}
type TokenRequest struct {
ClientID string `json:"client_id"`
ClientSecret string `json:"client_secret"`
Username string `json:"username"`
Password string `json:"password"`
GrantType string `json:"grant_type"`
}
type RefreshTokenRequest struct {
ClientID string `json:"client_id"`
ClientSecret string `json:"client_secret"`
RefreshToken string `json:"refresh_token"`
GrantType string `json:"grant_type"`
}
type APIError struct {
Error string `json:"error"`
Message string `json:"message"`
DetailMessage string `json:"detail_message"`
}
func (c *client) getToken() error {
tokenRequest := TokenRequest{c.clientID, c.clientSecret, c.username, c.password, "password"}
form := url.Values{}
form.Add("client_id", tokenRequest.ClientID)
form.Add("client_secret", tokenRequest.ClientSecret)
form.Add("username", tokenRequest.Username)
form.Add("password", tokenRequest.Password)
form.Add("grant_type", tokenRequest.GrantType)
resp, err := http.Post(accessURL, "application/x-www-form-urlencoded", strings.NewReader(form.Encode()))
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode >= 300 {
return fmt.Errorf("Could not retrieve a token.")
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
var t Token
err = json.Unmarshal(body, &t)
if err != nil {
return err
}
c.Token = t
c.setTokenExpiration()
return nil
}
func (c *client) refreshToken() error {
tokenRequest := RefreshTokenRequest{c.clientID, c.clientSecret, c.Token.RefreshToken, "refresh_token"}
form := url.Values{}
form.Add("client_id", tokenRequest.ClientID)
form.Add("client_secret", tokenRequest.ClientSecret)
form.Add("refresh_token", tokenRequest.RefreshToken)
form.Add("grant_type", tokenRequest.GrantType)
resp, err := http.Post(refreshURL, "application/x-www-form-urlencoded", strings.NewReader(form.Encode()))
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode >= 300 {
return fmt.Errorf("Could not refresh current token.")
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
var t Token
err = json.Unmarshal(body, &t)
if err != nil {
return err
}
c.Token = t
c.setTokenExpiration()
return nil
}
func (c *client) setTokenExpiration() {
c.tokenExpiration = time.Now().Add(time.Duration(c.Token.ExpiresIn-60) * time.Second)
}
func (c *client) request(relPath, verb string, payload []byte) (io.ReadCloser, error) {
if c.Token.AccessToken == "" {
err := c.getToken()
if err != nil {
return nil, err
}
}
err := c.RefreshTokenIfNecessary()
if err != nil {
return nil, err
}
bytesJSON := bytes.NewBuffer(payload)
req, err := http.NewRequest(verb, fmt.Sprintf("https://%s%s", apiHostname, relPath), bytesJSON)
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", c.Token.AccessToken))
req.Header.Add("Accept", "application/vnd.ilandcloud.api.v1.0+json")
if verb == http.MethodPut || verb == http.MethodPost {
req.Header.Add("Content-Type", "application/json")
}
resp, err := c.http.Do(req)
if err != nil {
return nil, err
}
if resp.StatusCode > 204 {
defer resp.Body.Close()
data, err := ioutil.ReadAll(resp.Body)
if err == nil {
return nil, errors.New(string(data))
}
return nil, errors.New(http.StatusText(resp.StatusCode))
}
return resp.Body, nil
}
func (c *client) RefreshTokenIfNecessary() error {
emptyToken := Token{}
if c == nil || c.Token == emptyToken {
err := c.getToken()
if err != nil {
return fmt.Errorf("Error retrieving iland cloud API token. %s", err.Error())
}
}
if c.isTokenExpired() {
err := c.refreshToken()
if err != nil {
err := c.getToken()
if err != nil {
return fmt.Errorf("Error refreshing iland cloud API token. %s", err.Error())
}
}
}
return nil
}
func (c *client) isTokenExpired() bool {
if time.Now().After(c.tokenExpiration) {
return true
}
return false
}
func unmarshalBody(body io.ReadCloser, object interface{}) error {
defer body.Close()
data, err := ioutil.ReadAll(body)
if err != nil {
return err
}
data = bytes.TrimPrefix(data, []byte(")]}'"))
return json.Unmarshal(data, object)
}