-
Notifications
You must be signed in to change notification settings - Fork 0
/
endpoint.go
96 lines (72 loc) · 1.61 KB
/
endpoint.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
package withttp
import (
"context"
"io"
"net/url"
)
type (
Header interface {
SetHeader(k, v string)
AddHeader(k, v string)
Header(k string) (string, bool)
RangeHeaders(func(string, string))
}
Request interface {
Header
Method() string
SetMethod(string)
SetURL(*url.URL)
// SetBodyStream sets the stream of body data belonging to a request. bodySize parameter is needed
// when using fasthttp implementation.
SetBodyStream(rc io.ReadWriteCloser, bodySize int)
SetBody([]byte)
Body() []byte
BodyStream() io.ReadWriteCloser
URL() *url.URL
}
Response interface {
Header
Status() int
StatusText() string
Body() io.ReadCloser
SetBody(rc io.ReadCloser)
SetStatus(status int)
}
Client interface {
Request(ctx context.Context) (Request, error)
Do(ctx context.Context, req Request) (Response, error)
}
Endpoint struct {
name string
requestOpts []ReqOption
responseOpts []ResOption
}
MockEndpoint struct {
inner *Endpoint
}
ReqOption interface {
Configure(r Request) error
}
ReqOptionFunc func(req Request) error
ResOption interface {
Parse(r Response) error
}
ResOptionFunc func(res Response) error
)
func (f ResOptionFunc) Parse(res Response) error {
return f(res)
}
func (f ReqOptionFunc) Configure(req Request) error {
return f(req)
}
func (e *Endpoint) Request(opts ...ReqOption) *Endpoint {
e.requestOpts = append(e.requestOpts, opts...)
return e
}
func (e *Endpoint) Response(opts ...ResOption) *Endpoint {
e.responseOpts = append(e.responseOpts, opts...)
return e
}
func NewEndpoint(name string) *Endpoint {
return &Endpoint{name: name}
}