-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrequest.go
70 lines (53 loc) · 1.49 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
package golamb
import (
"encoding/json"
"net/http"
"github.com/aws/aws-lambda-go/events"
)
// Request holds the http request data.
type Request interface {
// Query returns the query parameter with the given key.
Query(key string) string
// Path returns the path parameter with the given key.
Path(key string) string
// Body parses the JSON-encoded data and stores the result in the
// value pointed to by v.
Body(v any) error
// Cookie returns the cookie with the given name.
Cookie(name string) *http.Cookie
// Header returns the header value with the given key.
Header(key string) string
// Headers returns a map of all the headers.
Headers() map[string]string
// RawPath returns the raw path.
RawPath() string
}
type request struct {
request *events.APIGatewayV2HTTPRequest
cookies map[string]*http.Cookie
}
func (r *request) Query(key string) string {
return r.request.QueryStringParameters[key]
}
func (r *request) Path(key string) string {
return r.request.PathParameters[key]
}
func (r *request) Body(v any) error {
return json.Unmarshal([]byte(r.request.Body), v)
}
func (r *request) Header(key string) string {
return r.request.Headers[key]
}
func (r *request) Headers() map[string]string {
return r.request.Headers
}
func (r *request) Cookie(name string) *http.Cookie {
if r.cookies == nil {
r.cookies = make(map[string]*http.Cookie)
parseCookies(r.cookies, r.request.Cookies)
}
return r.cookies[name]
}
func (r *request) RawPath() string {
return r.request.RawPath
}