-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoperation.go
348 lines (313 loc) · 9.01 KB
/
operation.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
// Copyright (c) 2021 Bojan Zivanovic and contributors
// SPDX-License-Identifier: Apache-2.0
package broom
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"net/url"
"runtime"
"sort"
"strconv"
"strings"
"github.com/iancoleman/strcase"
)
// Operations represents a list of operations.
type Operations []Operation
// ByID returns an operation with the given ID.
func (ops Operations) ByID(id string) (Operation, bool) {
// Having to iterate over all operations is not super performant,
// but it's something we only do once per command, it allows us
// to avoid the problem of maps not being ordered.
for _, op := range ops {
if op.ID == id {
return op, true
}
}
return Operation{}, false
}
// ByTag returns a list of operations for the given tag.
func (ops Operations) ByTag(tag string) Operations {
filteredOps := make(Operations, 0, len(ops))
for _, op := range ops {
if op.Tag == tag {
filteredOps = append(filteredOps, op)
}
}
return filteredOps
}
// Tags returns a list of all available operation tags.
func (ops Operations) Tags() []string {
tags := make(map[string]struct{})
for _, op := range ops {
tags[op.Tag] = struct{}{}
}
tagNames := make([]string, 0, len(tags))
for tagName := range tags {
tagNames = append(tagNames, tagName)
}
sort.Strings(tagNames)
return tagNames
}
// Operation represents an available operation.
type Operation struct {
ID string
Summary string
Description string
Tag string
Method string
Path string
Parameters Parameters
BodyFormat string
Deprecated bool
}
// SummaryWithFlags returns the operation summary with flags.
func (op Operation) SummaryWithFlags() string {
summary := op.Summary
if op.Deprecated {
summary = fmt.Sprintf("%v (deprecated)", summary)
}
return summary
}
// HasBody returns whether the operation has a body.
func (op Operation) HasBody() bool {
// Body params are keyed by format in the spec, so there's no need to check both.
return op.BodyFormat != ""
}
// Validate validates the given values against the operation's parameters.
func (op Operation) Validate(values RequestValues) error {
nParams := len(op.Parameters.Path)
nValues := len(values.Path)
if nParams > nValues {
return fmt.Errorf("too few path parameters: got %v, want %v", nValues, nParams)
}
return nil
}
// Request creates a new request with the given values.
func (op Operation) Request(serverURL string, values RequestValues) (*http.Request, error) {
if err := op.Validate(values); err != nil {
return nil, err
}
url := op.requestURL(serverURL, values)
body, err := op.requestBody(values.Body)
if err != nil {
return nil, err
}
req, err := http.NewRequest(op.Method, url, bytes.NewReader(body))
if err != nil {
return nil, err
}
if len(values.Header) > 0 {
req.Header = values.Header
}
if op.HasBody() {
req.Header.Set("Content-Type", op.BodyFormat)
}
req.Header.Set("User-Agent", fmt.Sprintf("broom/%s (%s %s)", Version, runtime.GOOS, runtime.GOARCH))
return req, nil
}
// requestURL builds an absolute request url using the given body/query values.
func (op Operation) requestURL(serverURL string, values RequestValues) string {
oldnew := make([]string, 0, len(op.Parameters.Path)*2)
for i, v := range values.Path {
if i+1 > len(op.Parameters.Path) {
break
}
paramName := fmt.Sprintf("{%v}", op.Parameters.Path[i].Name)
oldnew = append(oldnew, paramName, v)
}
r := strings.NewReplacer(oldnew...)
path := r.Replace(op.Path)
if len(values.Query) > 0 {
path = path + "?" + values.Query.Encode()
}
// Paths always start with a slash, so make
// sure the server URL doesn't end with one.
serverURL = strings.TrimSuffix(serverURL, "/")
return serverURL + path
}
// requestBody converts the given body values into a byte array suitable for sending.
func (op Operation) requestBody(bodyValues url.Values) ([]byte, error) {
if !op.HasBody() {
// Operation does not support specifying a body (e.g. GET/DELETE).
return nil, nil
}
if IsJSON(op.BodyFormat) {
jsonValues := make(map[string]any, len(bodyValues))
for name := range bodyValues {
value := bodyValues.Get(name)
// Allow defined parameters to cast the string.
if bodyParam, ok := op.Parameters.Body.ByName(name); ok {
var err error
jsonValues[name], err = bodyParam.CastString(value)
if err != nil {
return nil, fmt.Errorf("could not process %v: %v", name, err)
}
}
// Pass through non-defined parameters as strings.
if _, ok := jsonValues[name]; !ok {
jsonValues[name] = value
}
}
// Process nested parameters.
for name, value := range jsonValues {
if !strings.Contains(name, ".") {
continue
}
nameParts := strings.Split(name, ".")
temp := jsonValues
for i, subname := range nameParts {
if i == len(nameParts)-1 {
temp[subname] = value
break
}
if _, ok := temp[subname].(map[string]any); !ok {
temp[subname] = make(map[string]any)
}
temp = temp[subname].(map[string]any)
}
delete(jsonValues, name)
}
return json.Marshal(jsonValues)
} else if op.BodyFormat == "application/x-www-form-urlencoded" {
return []byte(bodyValues.Encode()), nil
} else {
return nil, fmt.Errorf("unsupported body format %v", op.BodyFormat)
}
}
// Parameters represents the operation's parameters.
type Parameters struct {
Header ParameterList
Path ParameterList
Query ParameterList
Body ParameterList
}
// Add adds one or more parameters to the appropriate parameter list.
func (ps *Parameters) Add(params ...Parameter) {
for _, p := range params {
switch p.In {
case "header":
ps.Header = append(ps.Header, p)
case "path":
ps.Path = append(ps.Path, p)
case "query":
ps.Query = append(ps.Query, p)
case "body":
ps.Body = append(ps.Body, p)
}
}
}
// ParameterList represents a list of parameters.
type ParameterList []Parameter
// ByName returns a parameter with the given name.
func (pl ParameterList) ByName(name string) (Parameter, bool) {
for _, p := range pl {
if p.Name == name {
return p, true
}
}
return Parameter{}, false
}
// Parameter represents an operation parameter.
type Parameter struct {
In string
Name string
Description string
Style string
Type string
Enum []string
Example string
Default string
Deprecated bool
Required bool
}
// Label returns a human-readable parameter label.
func (p Parameter) Label() string {
//lint:ignore SA1019 The param name is ASCII, so strings.Title() still works fine.
return strings.Title(strcase.ToDelimited(p.Name, ' '))
}
// Flags returns the formatted parameter flags (deprecated, required).
func (p Parameter) FormattedFlags() string {
flags := make([]string, 0, 2)
if p.Deprecated {
flags = append(flags, "deprecated")
}
if p.Required {
flags = append(flags, "required")
}
formatted := ""
if len(flags) > 0 {
formatted = fmt.Sprintf("(%v)", strings.Join(flags, ", "))
}
return formatted
}
// CastString casts the given string to the parameter type.
func (p Parameter) CastString(str string) (any, error) {
if strings.HasPrefix(p.Type, "[]") {
strs := strings.Split(str, ",")
vs := make([]any, 0, len(strs))
for _, s := range strs {
v, err := parseStr(s, p.Type[2:])
if err != nil {
return nil, fmt.Errorf("%q is not a valid %v", s, p.Type[2:])
}
vs = append(vs, v)
}
return vs, nil
} else {
v, err := parseStr(str, p.Type)
if err != nil {
return nil, fmt.Errorf("%q is not a valid %v", str, p.Type)
}
return v, nil
}
}
// parseStr invokes the strconv parse function for the given type.
func parseStr(str string, newType string) (any, error) {
if newType == "boolean" {
return strconv.ParseBool(str)
} else if newType == "integer" {
return strconv.ParseInt(str, 10, 64)
} else if newType == "number" {
return strconv.ParseFloat(str, 64)
}
return str, nil
}
// RequestValues represent the values used to populate an operation request.
//
// Header, query, and body values are added to the request even if they don't
// have matching parameters, unlike path values, where the parameter is used
// to determine the name of the placeholder to replace.
type RequestValues struct {
Header http.Header
Path []string
Query url.Values
Body url.Values
}
// ParseRequestValues parses parameter values from the given strings.
func ParseRequestValues(headers []string, pathValues []string, query string, body string) (RequestValues, error) {
headerValues := make(http.Header, len(headers))
for _, header := range headers {
kv := strings.SplitN(header, ":", 2)
if len(kv) < 2 {
return RequestValues{}, fmt.Errorf("parse header: could not parse %q", header)
}
headerValues.Set(strings.TrimSpace(kv[0]), strings.TrimSpace(kv[1]))
}
queryValues, err := url.ParseQuery(query)
if err != nil {
return RequestValues{}, fmt.Errorf("parse query: %w", err)
}
bodyValues, err := url.ParseQuery(body)
if err != nil {
return RequestValues{}, fmt.Errorf("parse body: %w", err)
}
values := RequestValues{
Header: headerValues,
Path: pathValues,
Query: queryValues,
Body: bodyValues,
}
return values, nil
}