@@ -65,7 +65,7 @@ type Client struct {
65
65
}
66
66
67
67
// NewClient initializes a new {{ spec .title | caseUcfirst }} client with a given timeout
68
- func NewClient() Client {
68
+ func NewClient(optionalSetters ...ClientOption ) Client {
69
69
headers := map[string]string{
70
70
{% for key ,header in spec .global .defaultHeaders %}
71
71
"{{key }}" : "{{header }}",
@@ -80,13 +80,23 @@ func NewClient() Client {
80
80
if err != nil {
81
81
panic(err)
82
82
}
83
- return Client{
83
+
84
+ client := Client{
84
85
endpoint: "{{spec .endpoint }}",
85
86
client: httpClient,
86
87
timeout: defaultTimeout,
87
88
headers: headers,
88
89
chunkSize: defaultChunkSize,
89
90
}
91
+
92
+ for _, opt := range optionalSetters {
93
+ err = opt(& client)
94
+ if err != nil {
95
+ panic(err)
96
+ }
97
+ }
98
+
99
+ return client
90
100
}
91
101
92
102
func (clt *Client) String() string {
@@ -104,29 +114,41 @@ func getDefaultClient(timeout time.Duration) (*http.Client, error) {
104
114
}, nil
105
115
}
106
116
107
- func (clt *Client) SetTimeout(timeout time.Duration) error {
108
- clt.timeout = timeout
109
- httpClient, err := getDefaultClient(timeout)
110
- if err != nil {
111
- return err
117
+ type ClientOption func(*Client) error
118
+
119
+ func (_ Client) WithTimeout(timeout time.Duration) ClientOption {
120
+ return func(clt *Client) error {
121
+ httpClient, err := getDefaultClient(timeout)
122
+ if err != nil {
123
+ return err
124
+ }
125
+
126
+ clt.timeout = timeout
127
+ clt.client = httpClient
128
+
129
+ return nil
112
130
}
113
- clt.client = httpClient
114
- return nil
115
131
}
116
132
117
- // SetEndpoint sets the default endpoint to which the Client connects to
118
- func (clt *Client) SetEndpoint(endpoint string) {
119
- clt.endpoint = endpoint
133
+ func WithEndpoint(endpoint string) ClientOption {
134
+ return func(clt *Client) error {
135
+ clt.endpoint = endpoint
136
+ return nil
137
+ }
120
138
}
121
139
122
- // SetSelfSigned sets the condition that specify if the Client should allow connections to a server using a self-signed certificate
123
- func (clt *Client) SetSelfSigned(status bool) {
124
- clt.selfSigned = status
140
+ func WithSelfSigned(status bool) ClientOption {
141
+ return func(clt *Client) error {
142
+ clt.selfSigned = status
143
+ return nil
144
+ }
125
145
}
126
146
127
- // SetChunkSize sets the chunk size for file upload
128
- func (clt *Client) SetChunkSize(size int64) {
129
- clt.chunkSize = size
147
+ func WithChunkSize(size int64) ClientOption {
148
+ return func(clt *Client) error {
149
+ clt.chunkSize = size
150
+ return nil
151
+ }
130
152
}
131
153
132
154
// AddHeader add a new custom header that the Client should send on each request
@@ -138,8 +160,11 @@ func (clt *Client) AddHeader(key string, value string) {
138
160
{% if header .description %}
139
161
// {{header .description }}
140
162
{% endif %}
141
- func (clt *Client) Set{{header .key | caseUcfirst }}(value string) {
142
- clt.headers["{{header .name }}"] = value
163
+ func With{{header .key | caseUcfirst }}(value string) ClientOption {
164
+ return func(clt *Client) error {
165
+ clt.headers["{{header .name }}"] = value
166
+ return nil
167
+ }
143
168
}
144
169
{% endfor %}
145
170
0 commit comments