-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparams.go
More file actions
88 lines (76 loc) · 2.32 KB
/
params.go
File metadata and controls
88 lines (76 loc) · 2.32 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
87
88
package hyperdrive
import (
"fmt"
"net/http"
"net/url"
"github.com/gorilla/mux"
)
// QueryParams extracts the values from the request QueryString. It returns
// a url.Values object (essentially map[string][]string). If the
// request method is not GET, an empty url.Values is returned.
func QueryParams(r *http.Request) url.Values {
if r.Method == "GET" {
r.ParseForm()
return r.Form
}
return url.Values{}
}
// BodyParams deserializes the input, and extracts the values from the request
// body. It returns a url.Values object (essentially map[string][]string). If
// the request method is GET, an empty url.Values is returned.
func BodyParams(r *http.Request) url.Values {
if r.Method != "GET" {
return url.Values{}
}
return url.Values{}
}
// PathParams extracts the values from the request path which match named
// params in the route. They are returned as url.Values for consistincey
// with http.Request.Form's behaviour.
func PathParams(r *http.Request) url.Values {
var params = url.Values{}
for k, v := range mux.Vars(r) {
params.Add(k, v)
}
return params
}
// Params extracts the param values from all sources: query, body, and path -- in
// that order. Each subsequent source will overwrite values with the same key, to
// ensure API client intent is maintained in a consistent way.
func Params(r *http.Request) url.Values {
var params = QueryParams(r)
for k, values := range BodyParams(r) {
for _, v := range values {
params.Set(k, v)
}
}
for k, v := range mux.Vars(r) {
params.Set(k, v)
}
return params
}
// GetParams returns all allowed request params. It returns an error on
// the first required param is not present. GetParams is intended to be used
// in your method handlers in a given endpoint.
func GetParams(e Endpointer, r *http.Request) (url.Values, error) {
pp := parseEndpoint(e)
p := Params(r)
for k := range p {
if contains(pp.Allowed(r.Method), k) != true {
p.Del(k)
}
}
for _, required := range pp.Required(r.Method) {
if k, ok := p[required]; !ok {
return p, fmt.Errorf("Missing required parameter: %s", k)
}
}
return p, nil
}
// Parameter is an interface to allow users to create self-describing custom types
// to be used as endpoint params. The name and description are reusable
// across multiple endpoints.
type Parameter interface {
GetName() string
GetDesc() string
}