-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgr_req.go
More file actions
626 lines (535 loc) · 18.2 KB
/
gr_req.go
File metadata and controls
626 lines (535 loc) · 18.2 KB
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
package gr
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"os"
u "unsafe"
)
/*
Returns a new request with the given context.
Shortcut for `new(gr.Req).Ctx(ctx)`.
*/
func Ctx(ctx context.Context) *Req { return new(Req).Ctx(ctx) }
/*
Returns a new request with the given URL string.
Shortcut for `new(gr.Req).To(val)`.
*/
func To(val string) *Req { return new(Req).To(val) }
/*
Returns a new request with the given URL.
Shortcut for `new(gr.Req).Url(val)`.
*/
func Url(val *url.URL) *Req { return new(Req).Url(val) }
/*
Returns a new request with the given URL path.
Shortcut for `new(gr.Req).Path(val, vals...)`.
*/
func Path(val string, vals ...interface{}) *Req {
return new(Req).Path(val, vals...)
}
/*
Returns a new request with the given method.
Shortcut for `new(gr.Req).Meth(val)`.
*/
func Meth(val string) *Req { return new(Req).Meth(val) }
// Returns a new "GET" request. Shortcut for `new(gr.Req).Get()`.
func Get() *Req { return new(Req).Get() }
// Returns a new "POST" request. Shortcut for `new(gr.Req).Post()`.
func Post() *Req { return new(Req).Post() }
// Returns a new "PUT" request. Shortcut for `new(gr.Req).Put()`.
func Put() *Req { return new(Req).Put() }
// Returns a new "PATCH" request. Shortcut for `new(gr.Req).Patch()`.
func Patch() *Req { return new(Req).Patch() }
// Returns a new "DELETE" request. Shortcut for `new(gr.Req).Delete()`.
func Delete() *Req { return new(Req).Delete() }
// Returns a new "OPTIONS" request. Shortcut for `new(gr.Req).Options()`.
func Options() *Req { return new(Req).Options() }
// Returns a new "CONNECT" request. Shortcut for `new(gr.Req).Connect()`.
func Connect() *Req { return new(Req).Connect() }
// Returns a new "TRACE" request. Shortcut for `new(gr.Req).Trace()`.
func Trace() *Req { return new(Req).Trace() }
/*
Returns a new request with pre-initialized non-zero context, method, URL, and
header. Shortcut for `new(gr.Req).Init()`.
*/
func Init() *Req { return new(Req).Init() }
/*
Alias of `http.Request` with a fluent builder-style API. Freely castable to and
from `http.Request`. All methods are defined on `*gr.Req` and may mutate the
receiver or its inner references. To store and copy a "partially built"
request, use `(*gr.Req).Clone`.
*/
type Req http.Request
// Free cast to `*http.Request`.
func (self *Req) Req() *http.Request { return (*http.Request)(self) }
/*
Initializes context, `.Method`, `.URL`, `.Header` to non-zero values, similar to
how `http.NewRequest` would have done it. Mutates and returns the receiver.
*/
func (self *Req) Init() *Req {
return self.initCtx().initMeth().initUrl().initHead()
}
/*
Sets the inner context to the exact given value, without nil checks or
fallbacks. Mutates and returns the receiver.
*/
func (self *Req) Ctx(ctx context.Context) *Req {
*self.ctx() = ctx
return self
}
/*
Returns the inner context as-is. Like `(*http.Request).Context`, but without the
hidden fallback on `context.Background`. The function naming is inconsistent
with `.Ctx` which sets the context; consistent name would be `.GetCtx` or
`.CtxGet`; this name is used to match the existing `(*http.Request).Context`.
*/
func (self *Req) Context() context.Context { return *self.ctx() }
func (self *Req) ctx() *context.Context {
return (*context.Context)(u.Pointer(uintptr(u.Pointer(self)) + ctxOffset()))
}
/*
Sets the given HTTP client, unsafely reusing the `.TLS` field which is normally
unused in client requests. Passing nil clears the field. The client is
automatically used by `(*gr.Req).Res` and `(*gr.Req).ResCatch`. The name is
short for "client", not "CLI". Mutates and returns the receiver.
*/
func (self *Req) Cli(val *http.Client) *Req {
*self.cli() = val
return self
}
/*
Returns the HTTP client previously set by `(*gr.Req).Cli`, unsafely reusing the
`.TLS` field which is normally unused in client requests. The default is nil.
*/
func (self *Req) Client() *http.Client { return *self.cli() }
func (self *Req) cli() **http.Client {
return (**http.Client)(u.Pointer(&self.TLS))
}
// True if `.Method` is "", "GET", "HEAD" or "OPTIONS".
func (self *Req) IsReadOnly() bool { return IsReadOnly(self.Method) }
// Sets `.Method` to the given value. Mutates and returns the receiver.
func (self *Req) Meth(val string) *Req {
self.Method = val
return self
}
// Shortcut for `self.Meth(http.MethodGet)`.
func (self *Req) Get() *Req { return self.Meth(http.MethodGet) }
// Shortcut for `self.Meth(http.MethodPost)`.
func (self *Req) Post() *Req { return self.Meth(http.MethodPost) }
// Shortcut for `self.Meth(http.MethodPut)`.
func (self *Req) Put() *Req { return self.Meth(http.MethodPut) }
// Shortcut for `self.Meth(http.MethodPatch)`.
func (self *Req) Patch() *Req { return self.Meth(http.MethodPatch) }
// Shortcut for `self.Meth(http.MethodDelete)`.
func (self *Req) Delete() *Req { return self.Meth(http.MethodDelete) }
// Shortcut for `self.Meth(http.MethodOptions)`.
func (self *Req) Options() *Req { return self.Meth(http.MethodOptions) }
// Shortcut for `self.Meth(http.MethodConnect)`.
func (self *Req) Connect() *Req { return self.Meth(http.MethodConnect) }
// Shortcut for `self.Meth(http.MethodTrace)`.
func (self *Req) Trace() *Req { return self.Meth(http.MethodTrace) }
/*
Parses the input via `url.Parse` and sets `.URL` to the result. Panics on
parsing errors. Mutates and returns the receiver.
*/
func (self *Req) To(src string) *Req {
val, err := url.Parse(src)
if err != nil {
panic(fmt.Errorf(`[gr] failed to parse request destination: %w`, err))
}
return self.Url(val)
}
// Sets the given value as `.URL`. Mutates and returns the receiver.
func (self *Req) Url(val *url.URL) *Req {
self.URL = val
return self
}
/*
Uses `gr.UrlJoin` to make a URL path and sets the result as `.URL.Path`,
creating a new URL reference if the URL was nil. Mutates and returns the
receiver.
*/
func (self *Req) Path(val string, vals ...interface{}) *Req {
self = self.initUrl()
self.URL.Path = val
self.URL = UrlJoin(self.URL, vals...)
return self
}
/*
Uses `gr.UrlAppend` to append the input to the URL path, slash-separated.
Mutates and returns the receiver.
*/
func (self *Req) Append(val interface{}) *Req {
self = self.initUrl()
self.URL = UrlAppend(self.URL, val)
return self
}
/*
Uses `gr.UrlJoin` to append the inputs to the URL path, slash-separated. Mutates
and returns the receiver.
*/
func (self *Req) Join(vals ...interface{}) *Req {
// TODO rename to `.PathJoin` or `.PathAdd`.
self = self.initUrl()
self.URL = UrlJoin(self.URL, vals...)
return self
}
/*
Sets the given value as `.URL.RawQuery`, creating a new URL reference if the URL
was nil. Mutates and returns the receiver.
*/
func (self *Req) RawQuery(val string) *Req {
self = self.initUrl()
self.URL.RawQuery = val
return self
}
/*
Shortcut for `self.RawQuery(url.Values(val).Encode())`. Accepts an "anonymous"
type because all alias types such as `url.Values` are automatically castable
into it.
*/
func (self *Req) Query(val map[string][]string) *Req {
return self.RawQuery(url.Values(val).Encode())
}
// Makes sure `.Header` is non-nil. Mutates and returns the receiver.
func (self *Req) HeadInit() *Req {
self.Header = Head(self.Header).Init().Header()
return self
}
/*
Sets the given value as `.Header`. Mutates and returns the receiver. Accepts
an "anonymous" type because all alias types such as `http.Header` and `gr.Head`
are automatically castable into it.
*/
func (self *Req) Head(val map[string][]string) *Req {
self.Header = val
return self
}
/*
Deletes the given entry in `.Header` by using `gr.Head.Del`. May mutate
`.Header`, but not the slices contained therein. Mutates and returns the
receiver.
*/
func (self *Req) HeadDel(key string) *Req {
self.Header = Head(self.Header).Del(key).Header()
return self
}
/*
Appends the given key-value to `.Header` by using `gr.Head.Add`. Allocates the
header if necessary. May mutate `.Header` and an existing slice corresponding
to the key. Mutates and returns the receiver.
*/
func (self *Req) HeadAdd(key, val string) *Req {
self.Header = Head(self.Header).Add(key, val).Header()
return self
}
/*
Sets the given key-value in `.Header` by using `gr.Head.Set`. Allocates the
header if necessary. May mutate `.Header`, but not the slices contained
therein. Mutates and returns the receiver.
*/
func (self *Req) HeadSet(key, val string) *Req {
self.Header = Head(self.Header).Set(key, val).Header()
return self
}
/*
Replaces the given key-values entry in `.Header` by using `gr.Head.Replace`.
Allocates the header if necessary. May mutate `.Header`, but not the slices
contained therein. Mutates and returns the receiver.
*/
func (self *Req) HeadReplace(key string, vals ...string) *Req {
self.Header = Head(self.Header).Replace(key, vals...).Header()
return self
}
/*
Patches the header by using `gr.Head.Patch`. Allocates the header if necessary.
May mutate `.Header`, but not the slices contained therein. Mutates and returns
the receiver. Accepts an "anonymous" type because all alias types such as
`http.Header` and `gr.Head` are automatically castable into it.
*/
func (self *Req) HeadPatch(head map[string][]string) *Req {
self.Header = Head(self.Header).Patch(head).Header()
return self
}
/*
Shortcut for setting the "Content-Type" header. If the input is "", removes the
header instead. Mutates and returns the receiver.
*/
func (self *Req) Type(typ string) *Req {
if typ == `` {
return self.HeadDel(Type)
}
return self.HeadSet(Type, typ)
}
/*
Shortcut for setting the "Content-Type: application/json" header. Mutates and
returns the receiver.
*/
func (self *Req) TypeJson() *Req { return self.Type(TypeJson) }
/*
Shortcut for setting the "Content-Type: application/x-www-form-urlencoded"
header. Mutates and returns the receiver.
*/
func (self *Req) TypeForm() *Req { return self.Type(TypeForm) }
/*
Shortcut for setting the "Content-Type: multipart/form-data" header. Mutates and
returns the receiver.
*/
func (self *Req) TypeMulti() *Req { return self.Type(TypeMulti) }
/*
Uses the given string as the request body, updating the following fields:
* `.ContentLength` -> input length, in bytes rather than characters.
* `.Body` -> nil or `gr.NewStringReadCloser` from input.
* `.GetBody` -> nil or function returning `gr.NewStringReadCloser` from input.
If the input is empty, the listed fields are set to zero values, otherwise the
fields are set to non-zero values. Mutates and returns the receiver.
*/
func (self *Req) String(val string) *Req {
self.ContentLength = int64(len(val))
if self.ContentLength == 0 {
self.GetBody = nil
self.Body = nil
return self
}
self.GetBody = func() (io.ReadCloser, error) { return NewStringReadCloser(val), nil }
self.Body = NewStringReadCloser(val)
return self
}
/*
Uses the given chunk as the request body, updating the following fields:
* `.ContentLength` -> input length, in bytes rather than characters.
* `.Body` -> nil or `gr.NewBytesReadCloser` from input.
* `.GetBody` -> nil or function returning `gr.NewBytesReadCloser` from input.
If the input is empty, the listed fields are set to zero values, otherwise the
fields are set to non-zero values. Mutates and returns the receiver.
*/
func (self *Req) Bytes(val []byte) *Req {
self.ContentLength = int64(len(val))
if self.ContentLength == 0 {
self.GetBody = nil
self.Body = nil
return self
}
self.GetBody = func() (io.ReadCloser, error) { return NewBytesReadCloser(val), nil }
self.Body = NewBytesReadCloser(val)
return self
}
/*
URL-encodes the given vals as the request body. Shortcut for
`self.String(url.Values(val).Encode())`. Also sets `.ContentLength` and
`.GetBody`. Accepts an "anonymous" type because all alias types such as
`url.Values` are automatically castable into it. Mutates and returns the
receiver.
*/
func (self *Req) Vals(val map[string][]string) *Req {
return self.String(url.Values(val).Encode())
}
/*
URL-encodes the given vals as the request body. Also sets the header
"Content-Type: application/x-www-form-urlencoded", as well as fields
`.ContentLength` and `.GetBody`. Shortcut for `self.TypeForm().Vals(val)`.
Accepts an "anonymous" type because all alias types such as `url.Values` are
automatically castable into it. Mutates and returns the receiver.
*/
func (self *Req) FormVals(val map[string][]string) *Req {
return self.TypeForm().Vals(val)
}
/*
JSON-encodes an arbitrary value, using it as the request body. Also sets the
header "Content-Type: application/json", as well as fields `.ContentLength` and
`.GetBody`. Panics if JSON encoding fails. Use `(*gr.Req).JsonCatch` to catch
those panics. Mutates and returns the receiver.
*/
func (self *Req) Json(val interface{}) *Req {
self = self.TypeJson()
/**
Questionable but convenient special case. Allows the calling code to use this
unconditionally, regardless of the resulting HTTP method, without having
issues with clients that reject GET requests with a non-empty body. Also
avoids wasting performance in this very common case.
*/
if self.IsReadOnly() && val == nil {
return self.ReadCloser(nil)
}
chunk, err := json.Marshal(val)
if err != nil {
panic(fmt.Errorf(`[gr] failed to JSON-encode request body: %w`, err))
}
return self.Bytes(chunk)
}
/*
Same as `(*gr.Req).Json`, but if JSON encoding fails, returns an error instead
of panicking.
*/
func (self *Req) JsonCatch(val interface{}) (err error) {
defer rec(&err)
self.Json(val)
return
}
/*
Assumes that the given string is valid JSON, and uses it as the request body.
Also sets "Content-Type: application/json". Shortcut for
`self.TypeJson().String(val)`. Mutates and returns the receiver.
*/
func (self *Req) JsonString(val string) *Req {
return self.TypeJson().String(val)
}
/*
Assumes that the given chunk is valid JSON, and uses it as the request body.
Also sets "Content-Type: application/json". Shortcut for
`self.TypeJson().Bytes(val)`. Mutates and returns the receiver.
*/
func (self *Req) JsonBytes(val []byte) *Req {
return self.TypeJson().Bytes(val)
}
/*
Shortcut for setting `.Body` and returning the request. Sets the body as-is
without affecting other fields. Mutates and returns the receiver.
*/
func (self *Req) ReadCloser(val io.ReadCloser) *Req {
self.Body = val
return self
}
/*
Sets the given reader as the request body. If the reader is nil, sets nil.
Otherwise wraps it in `io.NopCloser`. Mutates and returns the receiver.
*/
func (self *Req) Reader(val io.Reader) *Req {
if val == nil {
return self.ReadCloser(nil)
}
return self.ReadCloser(io.NopCloser(val))
}
/*
Returns a copy of the request body. Attempts to use `.GetBody` if possible. May
fully read, close, and replace the current body. If both `.Body` and `.GetBody`
are nil, returns nil.
*/
func (self *Req) CloneBody() io.ReadCloser {
if self.GetBody != nil {
body, err := self.GetBody()
if err != nil {
panic(errReqBodyClone(err))
}
return body
}
one, two := ForkReadCloser(self.Body)
self.Body = one
return two
}
/*
Returns a deep copy, like `(*http.Request).Clone`, but without forcing you to
provide a context. Cloning allows to reuse partially built requests, like
templates. This preserves everything, including the previous context and
client. Inner mutable references such as `.URL` and `.Header` are deeply
cloned. Unlike `(*http.Request).Clone`, this also clones the body, by calling
`(*gr.Req).CloneBody` which is available separately.
*/
func (self *Req) Clone() *Req {
if self == nil {
return nil
}
req := (*Req)(self.Req().Clone(self.Req().Context())).Ctx(self.Context())
req.Body = self.CloneBody()
return req
}
/*
Introspection shortcut. Uses `(*http.Request).Write`, but panics on error
instead of returning an error. Follows the write with a newline. Caution:
mutates the request by reading the body. If you intend to send the request, use
`.Clone` or `.Dump`.
*/
func (self *Req) Write(out io.Writer) {
if self != nil && out != nil {
err := self.Req().Write(out)
if err != nil {
panic(errWrite(err))
}
_, _ = out.Write(bytesNewline)
}
}
/*
Introspection tool. Shortcut for using `(*gr.Req).Write` to dump the request to
standard output. Clones before dumping. Can be used in method chains without
affecting the original request.
*/
func (self *Req) Dump() *Req {
self.Clone().Write(os.Stdout)
return self
}
/*
Short for "response". Shortcut for `(*gr.Res).CliRes(self.Client())`, which uses
`http.DefaultClient` if no client was given. Returns the response as `*gr.Res`.
Panics on transport errors, but NOT in case of successful HTTP responses with
non-OK HTTP status codes. To avoid panics, use `(*gr.Req).ResCatch`.
The caller MUST close the response body by calling `*gr.Res.Done` or its other
reading or closing methods.
*/
func (self *Req) Res() *Res {
return self.CliRes(self.Client())
}
/*
Variant of `(*gr.Req).Res` that returns an error instead of panicking. If the
response is non-nil, the caller MUST close the response body by calling
`*gr.Res.Done` or its other reading or closing methods.
*/
func (self *Req) ResCatch() (_ *Res, err error) {
defer rec(&err)
return self.Res(), nil
}
/*
Short for "client response" or "response using client". Performs the request
using the given client, returning the response as `*gr.Res`. If the client is
nil, uses `http.DefaultClient`. Panics on transport errors, but NOT in case of
successful HTTP responses with non-OK HTTP status codes. To avoid panics, use
`(*gr.Req).CliResCatch`.
The caller MUST close the response body by calling `*gr.Res.Done` or its other
reading or closing methods.
*/
func (self *Req) CliRes(cli *http.Client) *Res {
if cli == nil {
cli = http.DefaultClient
}
res, err := cli.Do(self.Init().Req())
if err != nil {
panic(fmt.Errorf(`[gr] failed to perform HTTP request: %w`, err))
}
return (*Res)(res)
}
/*
Variant of `(*gr.Req).CliRes` that returns an error instead of panicking. If the
response is non-nil, the caller MUST close the response body by calling
`*gr.Res.Done` or its other reading or closing methods.
*/
func (self *Req) CliResCatch(cli *http.Client) (_ *Res, err error) {
defer rec(&err)
return self.CliRes(cli), nil
}
func (self *Req) initCtx() *Req {
if self.Context() == nil {
return self.Ctx(context.Background())
}
return self
}
func (self *Req) initMeth() *Req {
if self.Method == `` {
self.Method = http.MethodGet
}
return self
}
func (self *Req) initUrl() *Req {
if self.URL == nil {
self.URL = &url.URL{}
}
return self
}
func (self *Req) initHead() *Req {
if self.Header == nil {
self.Header = http.Header{}
}
return self
}