Repository navigation
Expand file tree
/
Copy pathrequest.go
More file actions
86 lines (79 loc) · 2.04 KB
/
Copy pathrequest.go
File metadata and controls
86 lines (79 loc) · 2.04 KB
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
package axioapi
import (
"fmt"
"net/url"
"slices"
"strconv"
"strings"
)
// Params are the parameters of an operation: path, query and body values in one map.
type Params map[string]any
// preparedRequest is an operation with its parameters placed in path, query and body.
type preparedRequest struct {
method string
path string
query url.Values
body map[string]any
}
// buildRequest splits flat params into path, query and body according to the operation.
func buildRequest(op Operation, params Params) (preparedRequest, error) {
remaining := make(Params, len(params))
for name, value := range params {
remaining[name] = value
}
path, err := fillPath(op, remaining)
if err != nil {
return preparedRequest{}, err
}
query := url.Values{}
body := map[string]any{}
for name, value := range remaining {
if value == nil {
continue
}
if belongsInBody(op, name) {
body[name] = value
} else {
addQuery(query, name, value)
}
}
if !op.hasBody() || len(body) == 0 {
body = nil
}
return preparedRequest{method: op.Method, path: path, query: query, body: body}, nil
}
func fillPath(op Operation, params Params) (string, error) {
path := op.Path
for _, name := range op.PathParams {
value, present := params[name]
if !present || value == nil {
return "", fmt.Errorf("axioapi: missing path parameter %q for %s", name, op.Key)
}
path = strings.ReplaceAll(path, "{"+name+"}", url.PathEscape(fmt.Sprint(value)))
delete(params, name)
}
return path, nil
}
func belongsInBody(op Operation, name string) bool {
return op.hasBody() && (slices.Contains(op.Body, name) || !slices.Contains(op.Query, name))
}
func addQuery(query url.Values, key string, value any) {
switch v := value.(type) {
case []string:
for _, item := range v {
query.Add(key+"[]", item)
}
case []int:
for _, item := range v {
query.Add(key+"[]", strconv.Itoa(item))
}
case []any:
for _, item := range v {
query.Add(key+"[]", fmt.Sprint(item))
}
case bool:
query.Set(key, strconv.FormatBool(v))
default:
query.Set(key, fmt.Sprint(v))
}
}