@@ -65,7 +65,7 @@ type Client struct {
6565}
6666
6767// NewClient initializes a new {{ spec .title | caseUcfirst }} client with a given timeout
68- func NewClient() Client {
68+ func NewClient(optionalSetters ...ClientOption ) Client {
6969 headers := map[string]string{
7070{% for key ,header in spec .global .defaultHeaders %}
7171 "{{key }}" : "{{header }}",
@@ -80,13 +80,23 @@ func NewClient() Client {
8080 if err != nil {
8181 panic(err)
8282 }
83- return Client{
83+
84+ client := Client{
8485 endpoint: "{{spec .endpoint }}",
8586 client: httpClient,
8687 timeout: defaultTimeout,
8788 headers: headers,
8889 chunkSize: defaultChunkSize,
8990 }
91+
92+ for _, opt := range optionalSetters {
93+ err = opt(& client)
94+ if err != nil {
95+ panic(err)
96+ }
97+ }
98+
99+ return client
90100}
91101
92102func (clt *Client) String() string {
@@ -104,29 +114,41 @@ func getDefaultClient(timeout time.Duration) (*http.Client, error) {
104114 }, nil
105115}
106116
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
112130 }
113- clt.client = httpClient
114- return nil
115131}
116132
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+ }
120138}
121139
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+ }
125145}
126146
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+ }
130152}
131153
132154// 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) {
138160{% if header .description %}
139161// {{header .description }}
140162{% 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+ }
143168}
144169{% endfor %}
145170
0 commit comments