-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathrequest.go
141 lines (120 loc) · 3.04 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
package there
import (
"context"
"encoding/json"
"encoding/xml"
"errors"
"io"
"net/http"
"net/textproto"
"net/url"
)
var (
ErrorParameterNotPresent = errors.New("parameter not present")
)
type Request struct {
Request *http.Request
ResponseWriter http.ResponseWriter
Method string
Body *BodyReader
Params DefaultUrlValues
Headers DefaultHttpHeader
RouteParams *RouteParamReader
RemoteAddress string
Host string
URI string
}
func NewHttpRequest(responseWriter http.ResponseWriter, request *http.Request) Request {
return Request{
Request: request,
ResponseWriter: responseWriter,
Method: request.Method,
Body: &BodyReader{request: request},
Params: DefaultUrlValues{request.URL.Query()},
Headers: DefaultHttpHeader{request.Header},
RouteParams: &RouteParamReader{request},
RemoteAddress: request.RemoteAddr,
URI: request.RequestURI,
}
}
func (r *Request) Context() context.Context {
return r.Request.Context()
}
func (r *Request) WithContext(ctx context.Context) {
*r.Request = *r.Request.WithContext(ctx)
}
// BodyReader reads the body and unmarshal it to the specified destination
type BodyReader struct {
request *http.Request
}
func (read BodyReader) BindJson(dest any) error {
return read.bind(dest, json.Unmarshal)
}
func (read BodyReader) BindXml(dest any) error {
return read.bind(dest, xml.Unmarshal)
}
func (read BodyReader) bind(dest any, formatter func(data []byte, v any) error) error {
body, err := read.ToBytes()
if err != nil {
return err
}
err = formatter(body, dest)
return err
}
func (read BodyReader) ToString() (string, error) {
data, err := read.ToBytes()
if err != nil {
return "", err
}
return string(data), err
}
func (read BodyReader) ToBytes() ([]byte, error) {
data, err := io.ReadAll(read.request.Body)
defer read.request.Body.Close()
if err != nil {
return nil, err
}
return data, nil
}
// DefaultHttpHeader wraps http.Header and provides a method to get a header value with a default.
type DefaultHttpHeader struct {
http.Header
}
func (v DefaultHttpHeader) Get(key string) (string, bool) {
value, ok := v.Header[textproto.CanonicalMIMEHeaderKey(key)]
if !ok || len(value) == 0 {
return "", false
}
return value[0], true
}
func (v DefaultHttpHeader) GetDefault(key, defaultValue string) string {
value, ok := v.Get(key)
if !ok {
return defaultValue
}
return value
}
// DefaultUrlValues wraps url.Values and provides a method to get a header value with a default.
type DefaultUrlValues struct {
url.Values
}
func (v DefaultUrlValues) Get(key string) (string, bool) {
value, ok := v.Values[key]
if !ok || len(value) == 0 {
return "", false
}
return value[0], true
}
func (v DefaultUrlValues) GetDefault(key, defaultValue string) string {
value, ok := v.Get(key)
if !ok {
return defaultValue
}
return value
}
type RouteParamReader struct {
request *http.Request
}
func (reader RouteParamReader) Get(key string) string {
return reader.request.PathValue(key)
}