-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathclient.go
166 lines (148 loc) · 3.98 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
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 goketo
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"strings"
"sync"
"github.com/sirupsen/logrus"
)
// Requester is the interface for all client calls - This allows easy test 'mocks'
type Requester interface {
do(*http.Request) ([]byte, error)
Get(string) ([]byte, error)
Post(string, []byte) ([]byte, error)
}
// Client http client tracker
type Client struct {
client *http.Client
endpoint string
identity string
version string
authLock sync.Mutex
auth *AuthToken
}
// bearerRoundTripper wrapper for query params
type bearerRoundTripper struct {
Delegate http.RoundTripper
clientID string
clientSecret string
}
// AuthToken holds data from Auth request
type AuthToken struct {
Token string `json:"access_token"`
Type string `json:"token_type"`
Expires int `json:"expires_in"` // in seconds
Scope string `json:"scope"`
}
func (b *bearerRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
if b.Delegate == nil {
b.Delegate = http.DefaultTransport
}
values := req.URL.Query()
values.Add("client_id", b.clientID)
values.Add("client_secret", b.clientSecret)
req.URL.RawQuery = values.Encode()
return b.Delegate.RoundTrip(req)
}
func errHandler(err error) {
if err != nil {
log.Print(err)
}
}
// NewAuthClient request application/json
func NewAuthClient(clientID string, ClientSecret string, ClientEndpoint string) (*Client, error) {
// Endpoint: /identity/oauth/token?grant_type=client_credentials
version := "v1"
var endpoint string
var identity string
// Check if endpoint has proper protocol
if strings.HasPrefix(ClientEndpoint, "http") {
endpoint = ClientEndpoint + "/rest/" + version + "/"
identity = ClientEndpoint + "/identity/"
} else {
endpoint = "https://" + ClientEndpoint + "/rest/" + version + "/"
identity = "https://" + ClientEndpoint + "/identity/"
}
// Add credentials to the request
client := &Client{
client: &http.Client{
Transport: &bearerRoundTripper{
clientID: clientID,
clientSecret: ClientSecret,
},
},
endpoint: endpoint,
identity: identity,
version: version,
}
if err := client.RefreshToken(); err != nil {
return nil, err
}
return client, nil
}
// RefreshToken refreshes the auth token provided by the Marketo API.
func (c *Client) RefreshToken() error {
// Make request for token
resp, err := c.client.Get(c.identity + "oauth/token?grant_type=client_credentials")
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return fmt.Errorf("an error occured while fetching data: %v", resp)
}
var auth AuthToken
if err := json.NewDecoder(resp.Body).Decode(&auth); err != nil {
logrus.Errorf("Could not convert response: %v", err)
return err
}
c.authLock.Lock()
defer c.authLock.Unlock()
c.auth = &auth
return nil
}
func (c *Client) do(req *http.Request) ([]byte, error) {
resp, err := c.client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if resp.StatusCode != 200 {
err = fmt.Errorf("Received unexpected status %d while trying to retrieve the server data with \"%s\"", resp.StatusCode, string(body))
return nil, err
}
return body, nil
}
// Get resource string
func (c *Client) Get(resource string) ([]byte, error) {
req, err := http.NewRequest("GET", c.endpoint+resource, nil)
if err != nil {
return nil, err
}
c.authLock.Lock()
logrus.Debug("Token: ", c.auth.Token)
req.Header.Add("Authorization", "Bearer "+c.auth.Token)
c.authLock.Unlock()
return c.do(req)
}
// Post to resource string the data provided
func (c *Client) Post(resource string, data []byte) ([]byte, error) {
req, err := http.NewRequest("POST", c.endpoint+resource, bytes.NewBuffer(data))
if err != nil {
return nil, err
}
c.authLock.Lock()
logrus.Debug("Token: ", c.auth.Token)
req.Header.Add("Authorization", "Bearer "+c.auth.Token)
c.authLock.Unlock()
req.Header.Set("Content-Type", "application/json")
return c.do(req)
}