-
Notifications
You must be signed in to change notification settings - Fork 0
/
request_options.go
71 lines (59 loc) · 1.9 KB
/
request_options.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
package client
import (
"fmt"
"net/http"
"strings"
)
type CompressionType string
const (
CompressionNone CompressionType = ""
CompressionGzip CompressionType = "gzip"
CompressionDeflate CompressionType = "deflate"
CompressionBrotli CompressionType = "br"
// Add other compression types as needed
)
// RequestOptions represents additional options for the HTTP request.
type RequestOptions struct {
Headers []Header // Custom headers to be added to the request.
Cookies []*http.Cookie // Cookies to be included in the request.
ProtocolScheme string // define a custom protocol scheme. It defaults to https
Compression CompressionType
UserAgent string
}
// AddHeader adds a new header to the RequestOptions.
func (opt *RequestOptions) AddHeader(key string, value string) {
opt.Headers = append(opt.Headers, Header{Key: key, Value: value})
}
// ListHeaders prints out the list of headers in the RequestOptions.
func (opt *RequestOptions) ListHeaders() {
for _, h := range opt.Headers {
fmt.Printf("%s=%s", h.Key, h.Value)
}
}
// ClearHeaders clears all headers in the RequestOptions.
func (opt *RequestOptions) ClearHeaders() {
opt.Headers = nil
}
// AddCookie adds a new cookie to the RequestOptions.
func (opt *RequestOptions) AddCookie(cookie *http.Cookie) {
opt.Cookies = append(opt.Cookies, cookie)
}
// ListCookies prints out the list of cookies in the RequestOptions.
func (opt *RequestOptions) ListCookies() {
for _, c := range opt.Cookies {
fmt.Printf("%s=%s", c.Name, c.Value)
}
}
// ClearCookies clears all cookies in the RequestOptions.
func (opt *RequestOptions) ClearCookies() {
opt.Cookies = nil
}
func (opt *RequestOptions) SetProtocolScheme(scheme string) {
if !strings.Contains(scheme, "://") {
scheme += "://"
}
opt.ProtocolScheme = scheme
}
func (opt *RequestOptions) Compress(compressionType CompressionType) {
opt.Compression = compressionType
}