-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
226 lines (195 loc) · 4.81 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
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package gonube
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"path"
"strconv"
)
const (
clientName = "Gonube SDK (https://github.com/leonelquinteros/gonube)"
apiHost = "https://api.tiendanube.com/v1"
)
// AuthResponse data
type AuthResponse struct {
AccessToken string `json:"access_token"`
TokenType string `json:"token_type"`
Scope string `json:"scope"`
UserID uint `json:"user_id"`
Error string `json:"error"`
ErrorDescription string `json:"error_description"`
}
// ClientConfig object used for client creation
type ClientConfig struct {
AccessToken string
UserID string
ClientID string
ClientSecret string
Debug bool
}
// NewClientConfig constructs a ClientConfig object with the environment variables set as default
func NewClientConfig() ClientConfig {
return ClientConfig{
AccessToken: accessToken,
UserID: userID,
ClientID: clientID,
ClientSecret: clientSecret,
}
}
// Client object
type Client struct {
config ClientConfig
Transport http.RoundTripper
}
// New constructor from configuration
func New(cc ClientConfig) Client {
return Client{
config: cc,
}
}
// GetAccessToken exchanges an authorization code obtained through OAuth2 for a permanent access_token.
func (c Client) GetAccessToken(code string) (AuthResponse, error) {
var r AuthResponse
params := url.Values{}
params.Set("client_id", c.config.ClientID)
params.Set("client_secret", c.config.ClientSecret)
params.Set("grant_type", "authorization_code")
params.Set("code", code)
data := params.Encode()
if c.config.Debug {
log.Printf("Sending auth request to https://www.tiendanube.com/apps/authorize/token?grant_type=authorization_code with payload: %s", data)
}
authRequest, err := http.NewRequest("POST", "https://www.tiendanube.com/apps/authorize/token?grant_type=authorization_code", bytes.NewBufferString(data))
if err != nil {
return r, err
}
authRequest.Header.Add("Content-Type", "application/x-www-form-urlencoded")
// Perform request
client := &http.Client{Transport: c.Transport}
resp, err := client.Do(authRequest)
if err != nil {
return r, err
}
defer resp.Body.Close()
// Read response
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return r, err
}
if c.config.Debug {
log.Printf("Got auth response: %s", body)
}
err = json.Unmarshal(body, &r)
if err != nil {
return r, err
}
// Validate response
if r.Error != "" {
return r, fmt.Errorf("%s: %s", r.Error, r.ErrorDescription)
}
// Set current client credentials
c.config.AccessToken = r.AccessToken
c.config.UserID = strconv.FormatUint(uint64(r.UserID), 10)
return r, nil
}
// Request executes any Tiendanube API method using the current client configuration
func (c Client) Request(method, endpoint string, params url.Values, data, response interface{}) error {
// Parse URL
base, err := url.Parse(apiHost)
if err != nil {
return err
}
base.Path = path.Join(base.Path, c.config.UserID, endpoint)
// Handle root path redirect
if endpoint == "" || endpoint == "/" {
base.Path += "/"
}
uri := base.String()
// Parse params
if params == nil {
params = url.Values{}
}
encodedParams := params.Encode()
if encodedParams != "" {
uri += "?" + params.Encode()
}
// Debug
if c.config.Debug {
log.Printf("NEW REQUEST TO %s with payload: %+v", base.String(), data)
}
// Create request
var req *http.Request
if data != nil {
var eData []byte
eData, err = json.Marshal(data)
if err != nil {
return err
}
req, err = http.NewRequest(method, uri, bytes.NewBuffer(eData))
if err != nil {
return err
}
} else {
req, err = http.NewRequest(method, uri, nil)
if err != nil {
return err
}
}
req.Header.Set("Content-Type", "application/json")
// Set Auth
req.Header.Set("User-Agent", clientName)
req.Header.Set("Authentication", "bearer "+c.config.AccessToken)
// Perform request
client := &http.Client{Transport: c.Transport}
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
// Read response
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
// Debug
if c.config.Debug {
log.Printf("RESPONSE FROM %s: %s", base.String(), body)
}
// Handle API errors
if resp.StatusCode >= 400 {
errResp := ErrorResponse{}
err = json.Unmarshal(body, &errResp)
if err != nil {
return err
}
err = errResp
} else {
// Unmarshal into response
if len(body) > 0 {
err = json.Unmarshal(body, response)
}
}
return err
}
// Stores returns a Stores API client
func (c Client) Stores() Stores {
return Stores{
Client: c,
}
}
// Products returns a Products API client
func (c Client) Products() Products {
return Products{
Client: c,
}
}
// Orders returns a Orders API client
func (c Client) Orders() Orders {
return Orders{
Client: c,
}
}