-
Notifications
You must be signed in to change notification settings - Fork 2
/
binary.go
222 lines (196 loc) · 5.88 KB
/
binary.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package chimera
import (
"context"
"io"
"net/http"
"reflect"
"github.com/invopop/jsonschema"
)
var (
_ RequestReader = new(BinaryRequest[Nil])
_ ResponseWriter = new(BinaryResponse[Nil])
_ RequestReader = new(Binary[Nil])
_ ResponseWriter = new(Binary[Nil])
)
// BinaryRequest[Params any] is a request type that uses a
// []byte as the Body and Params as an user-provided struct
type BinaryRequest[Params any] struct {
request *http.Request
Body []byte
Params Params
}
// Context returns the context that was part of the original http.Request
func (r *BinaryRequest[Params]) Context() context.Context {
if r.request != nil {
return r.request.Context()
}
return nil
}
func readBinaryRequest[Params any](req *http.Request, body *[]byte, params *Params) error {
defer req.Body.Close()
b, err := io.ReadAll(req.Body)
if err != nil {
return err
}
*body = b
if _, ok := any(params).(*Nil); !ok {
err = UnmarshalParams(req, params)
if err != nil {
return err
}
}
return nil
}
// ReadRequest reads the body of an http request and assigns it to the Body field using io.ReadAll.
// This function also reads the parameters using UnmarshalParams and assigns it to the Params field.
// NOTE: the body of the request is closed after this function is run.
func (r *BinaryRequest[Params]) ReadRequest(req *http.Request) error {
r.request = req
return readBinaryRequest(req, &r.Body, &r.Params)
}
func binaryRequestSpec[Params any](schema *RequestSpec) {
schema.RequestBody = &RequestBody{
Content: map[string]MediaType{
"application/octet-stream": {
Schema: &jsonschema.Schema{
Type: "string",
Format: "binary",
},
},
},
}
pType := reflect.TypeOf(new(Params))
for ; pType.Kind() == reflect.Pointer; pType = pType.Elem() {
}
if pType != reflect.TypeOf(Nil{}) {
schema.Parameters = CacheRequestParamsType(pType)
}
}
// OpenAPIRequestSpec returns the Request definition of a BinaryRequest
func (r *BinaryRequest[Params]) OpenAPIRequestSpec() RequestSpec {
schema := RequestSpec{}
binaryRequestSpec[Params](&schema)
return schema
}
// BinaryResponse[Params any] is a response type that uses a
// []byte as the Body and Params as an user-provided struct
type BinaryResponse[Params any] struct {
Body []byte
Params Params
}
// WriteBody writes the response body
func (r *BinaryResponse[Params]) WriteBody(write BodyWriteFunc) error {
_, err := write(r.Body)
return err
}
func binaryResponsesSpec[Params any](schema Responses) {
response := ResponseSpec{}
response.Content = map[string]MediaType{
"application/octet-stream": {
Schema: &jsonschema.Schema{
Type: "string",
Format: "binary",
},
},
}
pType := reflect.TypeOf(*new(Params))
for ; pType.Kind() == reflect.Pointer; pType = pType.Elem() {
}
if pType != reflect.TypeOf(Nil{}) {
response.Headers = make(map[string]Parameter)
for _, param := range CacheResponseParamsType(pType) {
response.Headers[param.Name] = Parameter{
Schema: param.Schema,
Description: param.Description,
Deprecated: param.Deprecated,
AllowReserved: param.AllowReserved,
AllowEmptyValue: param.AllowEmptyValue,
Required: param.Required,
Explode: param.Explode,
Example: param.Example,
Examples: param.Examples,
}
}
}
schema[""] = response
}
// OpenAPIResponsesSpec returns the Responses definition of a BinaryResponse
func (r *BinaryResponse[Params]) OpenAPIResponsesSpec() Responses {
schema := make(Responses)
binaryResponsesSpec[Params](schema)
return schema
}
// WriteHead writes adds the header for this response object
func (r *BinaryResponse[Params]) WriteHead(head *ResponseHead) error {
head.Headers.Set("Content-Type", "application/octet-stream")
h, err := MarshalParams(&r.Params)
if err != nil {
return err
}
for k, v := range h {
for _, x := range v {
head.Headers.Add(k, x)
}
}
return nil
}
// NewBinaryResponse creates a BinaryResponse from body and params
func NewBinaryResponse[Params any](body []byte, params Params) *BinaryResponse[Params] {
return &BinaryResponse[Params]{
Body: body,
Params: params,
}
}
// Binary[Params] is a helper type that effectively works as both a BinaryRequest[Params] and BinaryResponse[Params]
// This is mostly here for convenience
type Binary[Params any] struct {
request *http.Request
Body []byte
Params Params
}
// Context returns the context that was part of the original http.Request
// if this was used in a non-request context it will return nil
func (r *Binary[Params]) Context() context.Context {
if r.request != nil {
return r.request.Context()
}
return nil
}
// ReadRequest reads the body of an http request and assigns it to the Body field using io.ReadAll.
// This function also reads the parameters using UnmarshalParams and assigns it to the Params field.
// NOTE: the body of the request is closed after this function is run.
func (r *Binary[Params]) ReadRequest(req *http.Request) error {
r.request = req
return readBinaryRequest(req, &r.Body, &r.Params)
}
// OpenAPIRequestSpec returns the Request definition of a BinaryRequest
func (r *Binary[Params]) OpenAPIRequestSpec() RequestSpec {
schema := RequestSpec{}
binaryRequestSpec[Params](&schema)
return schema
}
// WriteBody writes the response body
func (r *Binary[Params]) WriteBody(write BodyWriteFunc) error {
_, err := write(r.Body)
return err
}
// OpenAPIResponsesSpec returns the Responses definition of a BinaryResponse
func (r *Binary[Params]) OpenAPIResponsesSpec() Responses {
schema := make(Responses)
binaryResponsesSpec[Params](schema)
return schema
}
// WriteHead writes the header for this response object
func (r *Binary[Params]) WriteHead(head *ResponseHead) error {
head.Headers.Set("Content-Type", "application/octet-stream")
h, err := MarshalParams(&r.Params)
if err != nil {
return err
}
for k, v := range h {
for _, x := range v {
head.Headers.Add(k, x)
}
}
return nil
}