-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
request_builder_body.go
61 lines (52 loc) · 1.79 KB
/
request_builder_body.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
package fastshot
import (
"errors"
"io"
"github.com/opus-domini/fast-shot/constant"
)
// BuilderRequestBody is the interface that wraps the basic methods for setting custom HTTP Body's.
var _ BuilderRequestBody[RequestBuilder] = (*RequestBodyBuilder)(nil)
// RequestBodyBuilder serves as the main entry point for configuring BuilderRequestBody.
type RequestBodyBuilder struct {
parentBuilder *RequestBuilder
requestConfig *RequestConfigBase
}
// Body returns a new RequestBodyBuilder for setting custom HTTP Body's.
func (b *RequestBuilder) Body() *RequestBodyBuilder {
return &RequestBodyBuilder{
parentBuilder: b,
requestConfig: b.request.config,
}
}
// AsReader sets the body as IO Reader.
func (b *RequestBodyBuilder) AsReader(body io.Reader) *RequestBuilder {
err := b.requestConfig.Body().Set(body)
if err != nil {
b.requestConfig.Validations().Add(errors.Join(errors.New(constant.ErrMsgSetBody), err))
}
return b.parentBuilder
}
// AsString sets the body as string.
func (b *RequestBodyBuilder) AsString(body string) *RequestBuilder {
err := b.requestConfig.Body().WriteAsString(body)
if err != nil {
b.requestConfig.Validations().Add(errors.Join(errors.New(constant.ErrMsgSetBody), err))
}
return b.parentBuilder
}
// AsJSON sets the body as JSON.
func (b *RequestBodyBuilder) AsJSON(obj interface{}) *RequestBuilder {
err := b.requestConfig.Body().WriteAsJSON(obj)
if err != nil {
b.requestConfig.Validations().Add(errors.Join(errors.New(constant.ErrMsgMarshalJSON), err))
}
return b.parentBuilder
}
// AsXML sets the body as XML.
func (b *RequestBodyBuilder) AsXML(obj interface{}) *RequestBuilder {
err := b.requestConfig.Body().WriteAsXML(obj)
if err != nil {
b.requestConfig.Validations().Add(errors.Join(errors.New(constant.ErrMsgMarshalXML), err))
}
return b.parentBuilder
}