/
request.go
164 lines (147 loc) · 3.79 KB
/
request.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
// Copyright 2022 The kubegems.io Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package request
import (
"compress/gzip"
"compress/zlib"
"encoding/json"
"encoding/xml"
"fmt"
"mime"
"net/http"
"strconv"
"strings"
"time"
)
type PathVar struct {
Key string `json:"key,omitempty"`
Value string `json:"value,omitempty"`
}
type PathVarList []PathVar
func (p PathVarList) Get(key string) string {
for _, v := range p {
if v.Key == key {
return v.Value
}
}
return ""
}
func (p PathVarList) Map() map[string]string {
m := make(map[string]string, len(p))
for _, v := range p {
m[v.Key] = v.Value
}
return m
}
var PathVarsFunc = func(r *http.Request) PathVarList {
return nil
}
type ListOptions struct {
Page int `json:"page,omitempty"`
Size int `json:"size,omitempty"`
Search string `json:"search,omitempty"`
Sort string `json:"sort,omitempty"`
}
// nolint: gomnd
func GetListOptions(r *http.Request) ListOptions {
return ListOptions{
Page: Query(r, "page", 1),
Size: Query(r, "size", 10),
Search: Query(r, "search", ""),
Sort: Query(r, "sort", ""),
}
}
func HeaderOrQuery[T any](r *http.Request, key string, defaultValue T) T {
if val := r.Header.Get(key); val == "" {
return ValueOrDefault(r.URL.Query().Get(key), defaultValue)
} else {
return ValueOrDefault(val, defaultValue)
}
}
func PathVars(r *http.Request) PathVarList {
return PathVarsFunc(r)
}
func Path[T any](r *http.Request, key string, defaultValue T) T {
return ValueOrDefault(PathVars(r).Get(key), defaultValue)
}
func Header[T any](r *http.Request, key string, defaultValue T) T {
val := r.Header.Get(key)
return ValueOrDefault(val, defaultValue)
}
func Query[T any](r *http.Request, key string, defaultValue T) T {
val := r.URL.Query().Get(key)
return ValueOrDefault(val, defaultValue)
}
// nolint: forcetypeassert,gomnd,ifshort
// ValueOrDefault return default value if empty string
func ValueOrDefault[T any](val string, defaultValue T) T {
if val == "" {
return defaultValue
}
switch any(defaultValue).(type) {
case string:
return any(val).(T)
case []string:
if val == "" {
return defaultValue
}
return any(strings.Split(val, ",")).(T)
case int:
intval, _ := strconv.Atoi(val)
return any(intval).(T)
case bool:
b, _ := strconv.ParseBool(val)
return any(b).(T)
case int64:
intval, _ := strconv.ParseInt(val, 10, 64)
return any(intval).(T)
case time.Time:
t, _ := time.Parse(time.RFC3339, val)
return any(t).(T)
default:
return defaultValue
}
}
func Body(r *http.Request, into any) error {
body := r.Body
// check if the request body needs decompression
switch contentEncoding := r.Header.Get("Content-Encoding"); contentEncoding {
case "gzip":
reader, err := gzip.NewReader(r.Body)
if err != nil {
return err
}
body = reader
case "deflate":
zlibReader, err := zlib.NewReader(r.Body)
if err != nil {
return err
}
body = zlibReader
}
mediatype, _, _ := mime.ParseMediaType(r.Header.Get("Content-Type"))
switch mediatype {
case "application/json", "":
if err := json.NewDecoder(body).Decode(into); err != nil {
return err
}
case "application/xml":
if err := xml.NewDecoder(body).Decode(into); err != nil {
return err
}
default:
return fmt.Errorf("unsupported media type: %s", mediatype)
}
return ValidateBody(r, into)
}