-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequest.go
50 lines (44 loc) · 885 Bytes
/
request.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
package telegraph
import (
"fmt"
"io"
"net/http"
"net/url"
)
// request define an API request
type request struct {
method string
endpoint string
secured bool
form url.Values
header http.Header
body io.Reader
fullURL string
}
// setFormParam set param with key/value to request form body
func (r *request) setFormParam(key string, value any) {
if r.form == nil {
r.form = url.Values{}
}
r.form.Set(key, fmt.Sprintf("%v", value))
}
func (r *request) validate() error {
if r.form == nil {
r.form = url.Values{}
}
return nil
}
// RequestOption define option type for request
type RequestOption func(*request)
func WithHeader(key, value string, replace bool) RequestOption {
return func(r *request) {
if r.header == nil {
r.header = http.Header{}
}
if replace {
r.header.Set(key, value)
} else {
r.header.Add(key, value)
}
}
}