-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
request_builder_header.go
69 lines (58 loc) · 2.4 KB
/
request_builder_header.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
package fastshot
import (
"github.com/opus-domini/fast-shot/constant/header"
"github.com/opus-domini/fast-shot/constant/mime"
)
// BuilderHeader is the interface that wraps the basic methods for setting custom HTTP BuilderHeader.
var _ BuilderHeader[RequestBuilder] = (*RequestHeaderBuilder)(nil)
// RequestHeaderBuilder is a builder for setting custom HTTP BuilderHeader.
type RequestHeaderBuilder struct {
parentBuilder *RequestBuilder
requestConfig *RequestConfigBase
}
// Header returns a new RequestHeaderBuilder for setting custom HTTP BuilderHeader.
func (b *RequestBuilder) Header() *RequestHeaderBuilder {
return &RequestHeaderBuilder{
parentBuilder: b,
requestConfig: b.request.config,
}
}
// Add adds a custom header to the HTTP request. If header already exists, it will be appended.
func (b *RequestHeaderBuilder) Add(key header.Type, value string) *RequestBuilder {
b.requestConfig.httpHeader.Add(key, value)
return b.parentBuilder
}
// AddAll adds custom headers to the HTTP request. If header already exists, it will be appended.
func (b *RequestHeaderBuilder) AddAll(headers map[header.Type]string) *RequestBuilder {
for key, value := range headers {
b.Add(key, value)
}
return b.parentBuilder
}
// Set sets a custom header to the HTTP request. If header already exists, it will be overwritten.
func (b *RequestHeaderBuilder) Set(key header.Type, value string) *RequestBuilder {
b.requestConfig.httpHeader.Set(key, value)
return b.parentBuilder
}
// SetAll sets custom headers to the HTTP request. If header already exists, it will be overwritten.
func (b *RequestHeaderBuilder) SetAll(headers map[header.Type]string) *RequestBuilder {
for key, value := range headers {
b.Set(key, value)
}
return b.parentBuilder
}
// AddAccept sets the Accept header. If header already exists, it will be appended.
func (b *RequestHeaderBuilder) AddAccept(value mime.Type) *RequestBuilder {
b.Add(header.Accept, value.String())
return b.parentBuilder
}
// AddContentType sets the Content-Type header. If header already exists, it will be appended.
func (b *RequestHeaderBuilder) AddContentType(value mime.Type) *RequestBuilder {
b.Add(header.ContentType, value.String())
return b.parentBuilder
}
// AddUserAgent sets the User-Agent header. If header already exists, it will be appended.
func (b *RequestHeaderBuilder) AddUserAgent(value string) *RequestBuilder {
b.Add(header.UserAgent, value)
return b.parentBuilder
}