-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
117 lines (96 loc) · 2.37 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
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
package oauth
import (
"encoding/json"
"io/ioutil"
"net/http"
"net/url"
"strings"
)
const (
defaultUserAgent = "oauth/0.1.0 (github.com/jamiemansfield/oauth)"
)
type Client struct {
httpClient *http.Client
UserAgent string
ClientID string
ClientSecret string
}
func NewClient(client *http.Client, id string, secret string) *Client {
if client == nil {
client = http.DefaultClient
}
return &Client{
httpClient: client,
UserAgent: defaultUserAgent,
ClientID: id,
ClientSecret: secret,
}
}
func (c *Client) NewRequest(method string, urlStr string, data url.Values) (*http.Request, error) {
// Create the request
req, err := http.NewRequest(method, urlStr, strings.NewReader(data.Encode()))
if err != nil {
return nil, err
}
// Set request headers
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Header.Set("Accept", "application/json")
if c.UserAgent != "" {
req.Header.Set("User-Agent", c.UserAgent)
}
return req, nil
}
func (c *Client) Do(req *http.Request, v interface{}) (*http.Response, error) {
resp, err := c.httpClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
err = checkResponse(resp)
if err != nil {
return resp, err
}
if v != nil {
err = json.NewDecoder(resp.Body).Decode(v)
}
return resp, err
}
func checkResponse(r *http.Response) error {
// 200 is good
if r.StatusCode == 200 {
return nil
}
var errResp ErrorResponse
data, err := ioutil.ReadAll(r.Body)
if err == nil && data != nil {
err := json.Unmarshal(data, &errResp)
if err != nil {
return err
}
}
return errResp
}
func (c *Client) postToken(urlStr string, data url.Values) (*AccessTokenResponse, error) {
req, err := c.NewRequest(http.MethodPost, urlStr, data)
if err != nil {
return nil, err
}
var authorisation AccessTokenResponse
_, err = c.Do(req, &authorisation)
if err != nil {
return nil, err
}
return &authorisation, nil
}
// See https://tools.ietf.org/html/rfc6749#section-6
func (c *Client) RefreshAccessToken(urlStr string, refreshToken string, scope []string) (*AccessTokenResponse, error) {
data := make(url.Values)
data.Set("client_id", c.ClientID)
data.Set("client_secret", c.ClientSecret)
data.Set("grant_type", "refresh_token")
data.Set("refresh_token", refreshToken)
if scope != nil && len(scope) > 0 {
data["scope"] = scope
}
return c.postToken(urlStr, data)
}