-
Notifications
You must be signed in to change notification settings - Fork 5
/
petstore.go
253 lines (217 loc) · 6.06 KB
/
petstore.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
package petstore
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"log"
"net/http"
"net/url"
"os"
"strings"
"github.com/google/go-querystring/query"
cleanhttp "github.com/hashicorp/go-cleanhttp"
"github.com/svanharmelen/jsonapi"
"golang.org/x/time/rate"
)
const (
userAgent = "go-petstore"
// DefaultAddress of Petstore server
DefaultAddress = "http://localhost:8080"
// DefaultBasePath on which the API is served.
DefaultBasePath = "/api"
)
var (
// ErrBadRequest is returned when a receiving a 400.
ErrBadRequest = errors.New("bad request")
// ErrResourceNotFound is returned when a receiving a 404.
ErrResourceNotFound = errors.New("resource not found")
// ErrGatewayTimeout is returned when a receiving a 504.
ErrGatewayTimeout = errors.New("gateway timeout")
)
// Config provides configuration details to the API client.
type Config struct {
// The address of the Petstore API.
Address string
// The base path on which the API is served.
BasePath string
// Headers that will be added to every request.
Headers http.Header
// A custom HTTP client to use.
HTTPClient *http.Client
}
// DefaultConfig returns a default config structure.
func DefaultConfig() *Config {
config := &Config{
Address: os.Getenv("PETSTORE_ADDRESS"),
BasePath: DefaultBasePath,
Headers: make(http.Header),
HTTPClient: cleanhttp.DefaultPooledClient(),
}
// Set the default address if none is given.
if config.Address == "" {
config.Address = DefaultAddress
}
// Set the default user agent.
config.Headers.Set("Content-Type", "application/json")
return config
}
// Client is the Petstore API client. It provides the basic
// connectivity and configuration for accessing the Petstore API.
type Client struct {
baseURL *url.URL
headers http.Header
http *http.Client
limiter *rate.Limiter
Pets Pets
}
// NewClient creates a new Petstore API client.
func NewClient(cfg *Config) (*Client, error) {
config := DefaultConfig()
// Layer in the provided config for any non-blank values.
if cfg != nil {
if cfg.Address != "" {
config.Address = cfg.Address
}
if cfg.BasePath != "" {
config.BasePath = cfg.BasePath
}
for k, v := range cfg.Headers {
config.Headers[k] = v
}
if cfg.HTTPClient != nil {
config.HTTPClient = cfg.HTTPClient
}
}
// Parse the address to make sure its a valid URL.
baseURL, err := url.Parse(config.Address + config.BasePath)
if err != nil {
return nil, fmt.Errorf("invalid address: %v", err)
}
// Create the client.
client := &Client{
baseURL: baseURL,
headers: config.Headers,
http: config.HTTPClient,
}
// Create the services.
client.Pets = &pets{client: client}
return client, nil
}
// newRequest creates an API request. A relative URL path can be provided in
// path, in which case it is resolved relative to the apiVersionPath of the
// Client. Relative URL paths should always be specified without a preceding
// slash.
// If v is supplied, the value will be JSONAPI encoded and included as the
// request body. If the method is GET, the value will be parsed and added as
// query parameters.
func (c *Client) newRequest(method, path string, v interface{}) (*http.Request, error) {
u, err := url.Parse(c.baseURL.String() + "/" + path)
if err != nil {
return nil, err
}
var body io.Reader
switch method {
case "GET":
if v != nil {
q, err := query.Values(v)
if err != nil {
return nil, err
}
u.RawQuery = q.Encode()
}
case "PATCH", "POST":
if v != nil {
dat, _ := json.Marshal(v)
log.Printf("[DEBUG] go-petstore body: " + string(dat))
body = bytes.NewReader(dat)
}
}
req, err := http.NewRequest(method, u.String(), body)
if err != nil {
return nil, err
}
// Set the default headers.
for k, v := range c.headers {
req.Header[k] = v
}
return req, nil
}
// do sends an API request and returns the API response. The API response
// is JSONAPI decoded and the document's primary data is stored in the value
// pointed to by v, or returned as an error if an API error has occurred.
// If v implements the io.Writer interface, the raw response body will be
// written to v, without attempting to first decode it.
//
// The provided ctx must be non-nil. If it is canceled or times out, ctx.Err()
// will be returned.
func (c *Client) do(ctx context.Context, req *http.Request, v interface{}) error {
// Add the context to the request.
req = req.WithContext(ctx)
log.Printf("[DEBUG] go-petstore request: %v", req)
// wake up the function?
tempReq, _ := c.newRequest("GET","pets",nil)
c.http.Do(tempReq)
// Execute the request and check the response.
resp, err := c.http.Do(req)
if err != nil {
// If we got an error, and the context has been canceled,
// the context's error is probably more useful.
select {
case <-ctx.Done():
return ctx.Err()
default:
return err
}
}
defer resp.Body.Close()
log.Printf("[DEBUG] go-petstore response: %v", resp)
// Basic response checking.
if err := checkResponseCode(resp); err != nil {
//retryable error
if err == ErrGatewayTimeout {
resp, err = c.http.Do(req)
}
return err
}
// Return here if decoding the response isn't needed.
if v == nil {
return nil
}
buf := new(bytes.Buffer)
buf.ReadFrom(resp.Body)
return json.Unmarshal(buf.Bytes(), v)
}
// checkResponseCode can be used to check the status code of an HTTP request.
func checkResponseCode(r *http.Response) error {
if r.StatusCode >= 200 && r.StatusCode <= 299 {
return nil
}
switch r.StatusCode {
case 400:
return ErrBadRequest
case 404:
return ErrResourceNotFound
case 504:
return ErrGatewayTimeout
}
// Decode the error payload.
errPayload := &jsonapi.ErrorsPayload{}
err := json.NewDecoder(r.Body).Decode(errPayload)
if err != nil || len(errPayload.Errors) == 0 {
log.Printf("[DEBUG] resp status: %v", r.Status)
return fmt.Errorf(r.Status)
}
// Parse and format the errors.
var errs []string
for _, e := range errPayload.Errors {
if e.Detail == "" {
errs = append(errs, e.Title)
} else {
errs = append(errs, fmt.Sprintf("%s\n\n%s", e.Title, e.Detail))
}
}
return fmt.Errorf(strings.Join(errs, "\n"))
}