-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcall_req.go
81 lines (61 loc) · 2.08 KB
/
call_req.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
package withttp
import (
"io"
"strconv"
)
type (
StreamCallReqOption[T any] interface {
CallReqOption[T]
stream()
}
StreamCallReqOptionFunc[T any] func(c *Call[T], req Request) error
)
func (s StreamCallReqOptionFunc[T]) stream() {}
func (s StreamCallReqOptionFunc[T]) Configure(c *Call[T], req Request) error {
return s(c, req)
}
func (c *Call[T]) Request(opts ...ReqOption) *Call[T] {
c.reqOptions = append(c.reqOptions, opts...)
return c
}
func (c *Call[T]) WithURL(raw string) *Call[T] {
return c.withReq(WithURL[T](raw))
}
func (c *Call[T]) WithURI(raw string) *Call[T] {
return c.withReq(WithURI[T](raw))
}
func (c *Call[T]) WithMethod(method string) *Call[T] {
return c.withReq(WithMethod[T](method))
}
func (c *Call[T]) WithRequestSniffed(fn func([]byte, error)) *Call[T] {
return c.withReq(WithRequestSniffer[T](fn))
}
func (c *Call[T]) WithRequestStreamBody(opt StreamCallReqOptionFunc[T]) *Call[T] {
return c.withReq(opt)
}
// WithBodyStream receives a stream of data to set on the request. Second parameter `bodySize` indicates
// the estimated content-length of this stream. Required when employing fasthttp http client.
func (c *Call[T]) WithBodyStream(rc io.ReadWriteCloser, bodySize int) *Call[T] {
return c.withReq(WithBodyStream[T](rc, bodySize))
}
func (c *Call[T]) WithBody(payload any) *Call[T] {
return c.withReq(WithBody[T](payload))
}
func (c *Call[T]) WithRawBody(payload []byte) *Call[T] {
return c.withReq(WithRawBody[T](payload))
}
func (c *Call[T]) WithContentLength(length int) *Call[T] {
return c.WithHeader("content-length", strconv.FormatInt(int64(length), 10), true)
}
func (c *Call[T]) WithHeader(key, value string, override bool) *Call[T] {
return c.withReq(WithHeader[T](key, value, override))
}
func (c *Call[T]) WithHeaderFunc(fn func() (key, value string, override bool)) *Call[T] {
return c.withReq(WithHeaderFunc[T](fn))
}
func (c *Call[T]) WithBasicAuth(user, pass string) *Call[T] {
return c.withReq(WithBasicAuth[T](user, pass))
}
func (c *Call[T]) WithContentType(ct ContentType) *Call[T] {
return c.withReq(WithContentType[T](ct))
}