-
Notifications
You must be signed in to change notification settings - Fork 0
/
request.go
159 lines (143 loc) · 3.13 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
package rest
import (
"context"
"encoding"
"encoding/json"
"io"
"net/http"
"reflect"
"strconv"
"strings"
"github.com/julienschmidt/httprouter"
"github.com/sokool/errors"
"github.com/sokool/rest/docs"
)
type Request[S Session] struct {
Session S
Request *http.Request
Response http.ResponseWriter
docs *docs.Path
}
func NewRequest[S Session](s Sessions[S], w http.ResponseWriter, r *http.Request) (*Request[S], error) {
var err error
var req = &Request[S]{Request: r, Response: w}
if req.Session, err = s.Read(r); err != nil {
return nil, err
}
return req, nil
}
func (r *Request[S]) Decode(to any) error {
defer r.Request.Body.Close()
switch err := json.NewDecoder(r.Request.Body).Decode(to); {
case err == io.EOF:
if j, ok := to.(json.Unmarshaler); ok {
return j.UnmarshalJSON([]byte{})
}
case err != nil:
return err
}
return nil
}
func (r *Request[S]) Param(name string, to ...any) (s string, err error) {
defer func() {
if r.docs == nil {
return
}
var v any = s
if len(to) > 0 {
v = to[0]
}
r.docs.Parameters().Path(name, v)
}()
if s = httprouter.ParamsFromContext(r.Request.Context()).ByName(name); s == "" {
return s, ErrValueNotFound
}
if len(to) != 0 {
if err = r.decode(s, to[0]); err != nil {
return s, Err("decode failed %w", err)
}
}
return
}
func (r *Request[S]) Query(name string, to ...any) (s string, err error) {
defer func() {
if r.docs == nil {
return
}
var v any = s
if len(to) > 0 {
v = to[0]
}
r.docs.Parameters().Query(name, "", v)
}()
if s = r.Request.URL.Query().Get(name); s == "" {
return s, ErrValueNotFound
}
if len(to) != 0 {
if err = r.decode(s, to[0]); err != nil {
return s, Err("decode failed %w", err)
}
}
return
}
func (r *Request[S]) Header(name string, to ...any) (s string, err error) {
if s = r.Request.Header.Get(name); s == "" {
return s, ErrValueNotFound
}
if len(to) != 0 {
if err = r.decode(s, to[0]); err != nil {
return s, Err("decode failed %w", err)
}
}
return s, nil
}
func (r *Request[S]) Method(names ...string) bool {
for i := range names {
if names[i] == r.Request.Method {
return true
}
}
return false
}
func (r *Request[S]) decode(from string, to any) error {
var err error
switch v := to.(type) {
case *string:
*v = from
case *[]string:
*v = strings.Split(from, "|")
case *int:
var n int64
n, err = strconv.ParseInt(from, 2, 64)
*v = int(n)
case *float64:
var n float64
n, err = strconv.ParseFloat(from, 64)
*v = n
case encoding.TextUnmarshaler:
err = v.UnmarshalText([]byte(from))
case json.Unmarshaler:
err = v.UnmarshalJSON([]byte(from))
default:
err = json.Unmarshal([]byte(from), to)
}
return err
}
var Err = errors.Errorf
var ErrValueNotFound = Err("rest: value not found")
func Read[T any](from *http.Request, key string) (T, bool) {
var ok bool
var t T
var v = from.Context().Value(key)
if v == nil || reflect.ValueOf(v).IsZero() {
return t, false
}
if t, ok = v.(T); ok {
return t, true
}
return t, false
}
func Write[T any](key string, value T, to *http.Request) {
t := to.WithContext(context.WithValue(to.Context(), key, value))
*to = *t
}