-
Notifications
You must be signed in to change notification settings - Fork 1
/
request.go
101 lines (85 loc) · 2.16 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
package lit
import (
"context"
"io"
"net/http"
"net/url"
)
// Request is the input of a [Handler].
type Request struct {
base *http.Request
parameters map[string]string
}
// NewEmptyRequest creates a new [Request] instance.
func NewEmptyRequest() *Request {
return &Request{
parameters: make(map[string]string),
}
}
// NewRequest creates a new [Request] instance from a [*http.Request].
//
// If request is nil, NewRequest panics.
func NewRequest(request *http.Request) *Request {
if request == nil {
panic("request must not be nil")
}
return &Request{
request,
make(map[string]string),
}
}
// WithRequest sets the base request of this request.
//
// If req is nil, WithRequest panics.
func (r *Request) WithRequest(req *http.Request) *Request {
if req == nil {
panic("request should not be nil")
}
r.base = req
return r
}
// WithContext sets the context of this request.
//
// If ctx is nil, WithContext panics.
func (r *Request) WithContext(ctx context.Context) *Request {
r.base = r.base.WithContext(ctx)
return r
}
// WithURIParameters sets the URI parameters (associated with their values) of this request.
func (r *Request) WithURIParameters(parameters map[string]string) *Request {
r.parameters = parameters
return r
}
// URIParameters returns this request's URL path parameters and their values. It can be nil, meaning the
// handler expects no parameters.
//
// Use [bind.URIParameters] for standard model binding and validation features.
//
// The keys from this map don't start with the ":" prefix.
func (r *Request) URIParameters() map[string]string {
return r.parameters
}
// Context of this request.
func (r *Request) Context() context.Context {
return r.base.Context()
}
// URL of this request.
func (r *Request) URL() *url.URL {
return r.base.URL
}
// Method of this request.
func (r *Request) Method() string {
return r.base.Method
}
// Body of this request.
func (r *Request) Body() io.ReadCloser {
return r.base.Body
}
// Header fields of this request.
func (r *Request) Header() http.Header {
return r.base.Header
}
// Base returns the equivalent [*http.Request] of this request.
func (r *Request) Base() *http.Request {
return r.base
}