-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopt_req.go
More file actions
150 lines (132 loc) · 3.19 KB
/
opt_req.go
File metadata and controls
150 lines (132 loc) · 3.19 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
package apimate
import (
"bytes"
"encoding/json"
"github.com/rollicks-c/apimate/internal/client"
"io"
"mime/multipart"
"net/http"
"net/url"
"strings"
)
func WithCookie(name string, value string) client.RequestOption {
return func(ctx *client.RequestContext) error {
cookie := &http.Cookie{
Name: name,
Value: value,
}
ctx.Req.AddCookie(cookie)
return nil
}
}
func WithHeaders(Headers http.Header) client.RequestOption {
return func(ctx *client.RequestContext) error {
for key, values := range Headers {
for _, value := range values {
ctx.Req.Header.Add(key, value)
}
}
return nil
}
}
func WithHeader(key, value string) client.RequestOption {
return func(ctx *client.RequestContext) error {
ctx.Req.Header.Add(key, value)
return nil
}
}
func WithDefaultRequest() client.RequestOption {
return func(ctx *client.RequestContext) error {
req, err := http.NewRequest(ctx.Method, ctx.Endpoint, nil)
if err != nil {
return err
}
ctx.Req = req
return nil
}
}
func WithPayload(body io.Reader) client.RequestOption {
return func(ctx *client.RequestContext) error {
req, err := http.NewRequest(ctx.Method, ctx.Endpoint, body)
if err != nil {
return err
}
ctx.Req = req
return nil
}
}
func WithFormPayload(body io.Reader) client.RequestOption {
return func(ctx *client.RequestContext) error {
req, err := http.NewRequest(ctx.Method, ctx.Endpoint, body)
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
ctx.Req = req
return nil
}
}
func WithJSONPayload(data any) client.RequestOption {
return func(ctx *client.RequestContext) error {
raw, err := json.Marshal(data)
if err != nil {
return err
}
body := bytes.NewReader(raw)
req, err := http.NewRequest(ctx.Method, ctx.Endpoint, body)
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/json")
ctx.Req = req
return nil
}
}
func WithValues(values url.Values) client.RequestOption {
return func(ctx *client.RequestContext) error {
req, err := http.NewRequest(ctx.Method, ctx.Endpoint, strings.NewReader(values.Encode()))
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
ctx.Req = req
return nil
}
}
func WithQuery(values url.Values) client.RequestOption {
return func(ctx *client.RequestContext) error {
// parse url
parsedURL, err := url.Parse(ctx.Endpoint)
if err != nil {
return err
}
// add values
parsedURL.RawQuery = values.Encode()
ctx.Endpoint = parsedURL.String()
ctx.Req.URL = parsedURL
return nil
}
}
func WithFilePayload(name string, data []byte) client.RequestOption {
return func(ctx *client.RequestContext) error {
// write file to form field
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
formFile, err := writer.CreateFormFile("file", name)
if err != nil {
return err
}
if _, err = io.Copy(formFile, bytes.NewReader(data)); err != nil {
return err
}
_ = writer.Close()
// create request
req, err := http.NewRequest(ctx.Method, ctx.Endpoint, body)
if err != nil {
return err
}
req.Header.Set("Content-Type", writer.FormDataContentType())
ctx.Req = req
return nil
}
}