-
Notifications
You must be signed in to change notification settings - Fork 0
/
type.go
105 lines (85 loc) · 1.95 KB
/
type.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
package request
import (
"context"
"fmt"
"io"
"net/http"
"net/http/httputil"
"net/url"
)
const (
MimeTypeJson = `application/json`
MimeTypeXml = `application/xml`
MimeTypeFormUrlEncoded = `application/x-www-form-urlencoded`
)
const (
HeaderAccept = `Accept`
HeaderContentType = `Content-Type`
)
type Option = func(*builder)
type RequestBuilder interface {
Get(url string) GetBuilder
Post(url string) PostBuilder
Patch(url string) PatchBuilder
Put(url string) PutBuilder
Delete(url string) DeleteBuilder
}
type NoPayloadBuilder interface {
CommonBuilder
}
type PayloadBuilder interface {
CommonBuilder
JsonBody(v any) CommonBuilder
XmlBody(v any) CommonBuilder
Body(r io.Reader) CommonBuilder
}
type GetBuilder interface {
NoPayloadBuilder
}
type PostBuilder interface {
PayloadBuilder
FormBody(form Form) PostBuilder
}
type PatchBuilder interface {
PayloadBuilder
}
type PutBuilder interface {
PayloadBuilder
}
type DeleteBuilder interface {
NoPayloadBuilder
}
type CommonBuilder interface {
Context(ctx context.Context) CommonBuilder
AddParmeter(key string, value any) CommonBuilder
SetParmeter(key string, value any) CommonBuilder
AddHeader(key string, value string) CommonBuilder
SetHeader(key string, value string) CommonBuilder
Build() (*http.Request, error)
Do() (*http.Response, error)
Handle(...ResponseHandler) error
JsonHandler(v any) error
XmlHandler(v any) error
Before(fn BeforeFn) CommonBuilder
After(fn AfterFn) CommonBuilder
}
type BeforeFn = func(req *http.Request) error
type AfterFn = func(res *http.Response) error
type ResponseHandler interface {
Handle(res *http.Response) error
}
type FormBuilder interface {
Add(k, v string) FormBuilder
Set(k, v string) FormBuilder
}
type Form struct {
Values url.Values
}
func Dump(req *http.Request) error {
b, err := httputil.DumpRequestOut(req, true)
if err != nil {
return err
}
fmt.Printf("request dump:\n%s\n", string(b))
return nil
}