@@ -37,7 +37,7 @@ var basicAuthHandlerFunc = func(client *Client, req *http.Request) error {
37
37
}
38
38
39
39
var bearerAuthHandlerFunc = func (client * Client , req * http.Request ) error {
40
- if client .options .Auth == nil || client .options .Auth .Type () != clients .AuthTypeBasic {
40
+ if client .options .Auth == nil || client .options .Auth .Type () != clients .AuthTypeBearer {
41
41
return fmt .Errorf ("invalid auth type" )
42
42
}
43
43
req .Header .Set ("Authorization" , "Bearer " + client .options .Auth .Token ())
@@ -66,127 +66,127 @@ type ClientOpts struct {
66
66
67
67
type ClientOptsBuilder struct {
68
68
* clients.OptionsBuilder
69
- opts * ClientOpts
69
+ restOptions * ClientOpts
70
70
}
71
71
72
- func ClientOptBuilder () * ClientOptsBuilder {
72
+ func CliOptsBuilder () * ClientOptsBuilder {
73
73
return & ClientOptsBuilder {
74
74
OptionsBuilder : clients .NewOptionsBuilder (),
75
- opts : & ClientOpts {
76
- ClientOptions : clients .EmptyClientOptions ,
77
- tlsConfig : & tls.Config {},
75
+ restOptions : & ClientOpts {
76
+ tlsConfig : & tls.Config {},
78
77
},
79
78
}
80
79
}
81
80
82
81
func (co * ClientOptsBuilder ) EnvProxy (proxyBasicAuth string ) * ClientOptsBuilder {
83
- co .opts .proxyBasicAuth = proxyBasicAuth
82
+ co .restOptions .proxyBasicAuth = proxyBasicAuth
84
83
return co
85
84
}
86
85
87
86
func (co * ClientOptsBuilder ) CodecOpts (options map [string ]any ) * ClientOptsBuilder {
88
- co .opts .codecOptions = options
87
+ co .restOptions .codecOptions = options
89
88
return co
90
89
}
91
90
92
91
func (co * ClientOptsBuilder ) MaxIdlePerHost (maxIdleConnPerHost int ) * ClientOptsBuilder {
93
- co .opts .maxIdlePerHost = maxIdleConnPerHost
92
+ co .restOptions .maxIdlePerHost = maxIdleConnPerHost
94
93
return co
95
94
}
96
95
97
96
func (co * ClientOptsBuilder ) ProxyAuth (user , password , bypass string ) * ClientOptsBuilder {
98
- co .opts .proxyBasicAuth = "Basic " + base64 .StdEncoding .EncodeToString ([]byte (user + ":" + password ))
97
+ co .restOptions .proxyBasicAuth = "Basic " + base64 .StdEncoding .EncodeToString ([]byte (user + ":" + password ))
99
98
return co
100
99
}
101
100
102
101
func (co * ClientOptsBuilder ) BaseUrl (baseurl string ) (err error ) {
103
102
if baseurl == textutils .EmptyStr {
104
103
return errors .New ("invalid base url" )
105
104
}
106
- co .opts .baseUrl , err = url .Parse (baseurl )
107
- if err == nil && co .opts .baseUrl .Scheme == textutils .EmptyStr && co .opts .baseUrl .Host == textutils .EmptyStr {
105
+ co .restOptions .baseUrl , err = url .Parse (baseurl )
106
+ if err == nil && co .restOptions .baseUrl .Scheme == textutils .EmptyStr && co .restOptions .baseUrl .Host == textutils .EmptyStr {
108
107
err = errors .New ("invalid base url" )
109
108
} else {
110
- if ! strings .HasSuffix (co .opts .baseUrl .Path , textutils .ForwardSlashStr ) {
111
- co .opts .baseUrl .Path = co .opts .baseUrl .Path + textutils .ForwardSlashStr
109
+ if ! strings .HasSuffix (co .restOptions .baseUrl .Path , textutils .ForwardSlashStr ) {
110
+ co .restOptions .baseUrl .Path = co .restOptions .baseUrl .Path + textutils .ForwardSlashStr
112
111
}
113
112
}
114
113
115
114
return
116
115
}
117
116
118
117
func (co * ClientOptsBuilder ) ErrOnStatus (httpStatusCodes ... int ) * ClientOptsBuilder {
119
- if co .opts .errorOnMap == nil {
120
- co .opts .errorOnMap = make (map [int ]int )
118
+ if co .restOptions .errorOnMap == nil {
119
+ co .restOptions .errorOnMap = make (map [int ]int )
121
120
}
122
121
for _ , code := range httpStatusCodes {
123
- co .opts .errorOnMap [code ] = code
122
+ co .restOptions .errorOnMap [code ] = code
124
123
}
125
124
return co
126
125
}
127
126
128
127
func (co * ClientOptsBuilder ) SSLVerify (verify bool ) * ClientOptsBuilder {
129
- if co .opts .tlsConfig == nil {
130
- co .opts .tlsConfig = & tls.Config {}
128
+ if co .restOptions .tlsConfig == nil {
129
+ co .restOptions .tlsConfig = & tls.Config {}
131
130
}
132
- co .opts .tlsConfig .InsecureSkipVerify = verify
133
- co .opts .useCustomTLSConfig = true
131
+ co .restOptions .tlsConfig .InsecureSkipVerify = verify
132
+ co .restOptions .useCustomTLSConfig = true
134
133
return co
135
134
}
136
135
137
136
func (co * ClientOptsBuilder ) CaCerts (caFilePath ... string ) * ClientOptsBuilder {
138
- co .opts .useCustomTLSConfig = true
139
- if co .opts .tlsConfig == nil {
140
- co .opts .tlsConfig = & tls.Config {}
137
+ co .restOptions .useCustomTLSConfig = true
138
+ if co .restOptions .tlsConfig == nil {
139
+ co .restOptions .tlsConfig = & tls.Config {}
141
140
}
142
- if co .opts .tlsConfig .RootCAs == nil {
143
- co .opts .tlsConfig .RootCAs = x509 .NewCertPool ()
141
+ if co .restOptions .tlsConfig .RootCAs == nil {
142
+ co .restOptions .tlsConfig .RootCAs = x509 .NewCertPool ()
144
143
}
145
144
for _ , v := range caFilePath {
146
145
caCert , err := os .ReadFile (v )
147
146
if err != nil {
148
147
logger .Error ("error reading ca cert file" , err )
149
148
continue
150
149
}
151
- co .opts .tlsConfig .RootCAs .AppendCertsFromPEM (caCert )
150
+ co .restOptions .tlsConfig .RootCAs .AppendCertsFromPEM (caCert )
152
151
}
153
152
return co
154
153
}
155
154
156
155
func (co * ClientOptsBuilder ) TlsCerts (certs ... tls.Certificate ) * ClientOptsBuilder {
157
- if co .opts .tlsConfig == nil {
158
- co .opts .tlsConfig = & tls.Config {}
156
+ if co .restOptions .tlsConfig == nil {
157
+ co .restOptions .tlsConfig = & tls.Config {}
159
158
}
160
- co .opts .useCustomTLSConfig = true
161
- co .opts .tlsConfig .Certificates = append (co .opts .tlsConfig .Certificates , certs ... )
159
+ co .restOptions .useCustomTLSConfig = true
160
+ co .restOptions .tlsConfig .Certificates = append (co .restOptions .tlsConfig .Certificates , certs ... )
162
161
return co
163
162
}
164
163
165
164
func (co * ClientOptsBuilder ) IdleTimeoutMs (t int ) * ClientOptsBuilder {
166
- co .opts .idleTimeout = time .Duration (t ) * time .Millisecond
165
+ co .restOptions .idleTimeout = time .Duration (t ) * time .Millisecond
167
166
return co
168
167
}
169
168
170
169
func (co * ClientOptsBuilder ) RequestTimeoutMs (t int ) * ClientOptsBuilder {
171
- co .opts .requestTimeout = time .Duration (t ) * time .Millisecond
170
+ co .restOptions .requestTimeout = time .Duration (t ) * time .Millisecond
172
171
return co
173
172
}
174
173
func (co * ClientOptsBuilder ) TlsHandShakeTimeoutMs (t int ) * ClientOptsBuilder {
175
- co .opts .tlsHandShakeTimeout = time .Duration (t ) * time .Millisecond
174
+ co .restOptions .tlsHandShakeTimeout = time .Duration (t ) * time .Millisecond
176
175
return co
177
176
}
178
177
179
178
func (co * ClientOptsBuilder ) ExpectContinueTimeoutMs (t int ) * ClientOptsBuilder {
180
- co .opts .expectContinueTimeout = time .Duration (t ) * time .Millisecond
179
+ co .restOptions .expectContinueTimeout = time .Duration (t ) * time .Millisecond
181
180
return co
182
181
}
183
182
func (co * ClientOptsBuilder ) CookieJar (jar http.CookieJar ) * ClientOptsBuilder {
184
- co .opts .jar = jar
183
+ co .restOptions .jar = jar
185
184
return co
186
185
}
187
186
188
187
func (co * ClientOptsBuilder ) Build () * ClientOpts {
189
- return co .opts
188
+ co .restOptions .ClientOptions = co .OptionsBuilder .Build ()
189
+ return co .restOptions
190
190
}
191
191
192
192
// Client represents a REST client.
@@ -228,7 +228,7 @@ func NewClientWithOptions(options *ClientOpts) *Client {
228
228
// NewClient creates a new REST client with default values.
229
229
func NewClient () * Client {
230
230
return NewClientWithOptions (& ClientOpts {
231
- ClientOptions : clients .EmptyClientOptions ,
231
+ ClientOptions : clients .NewOptionsBuilder (). Build () ,
232
232
tlsConfig : & tls.Config {
233
233
InsecureSkipVerify : false ,
234
234
},
@@ -269,50 +269,57 @@ func (c *Client) Execute(req *Request) (res *Response, err error) {
269
269
if c .options .proxyBasicAuth != textutils .EmptyStr {
270
270
httpReq .Header .Set (proxyAuthHdr , c .options .proxyBasicAuth )
271
271
}
272
- if err == nil {
273
- if c .options .Auth != nil {
274
- if handlerFunc , ok := c .options .AuthHandlers [c .options .Auth .Type ()]; ok {
275
- err = handlerFunc (c , httpReq )
276
- } else {
277
- err = fmt .Errorf ("invalid auth type or no handlerfunc found for auth type %v" , c .options .Auth .Type ())
278
- return
279
- }
280
- }
281
- // Check if the circuit breaker is open
282
- if c .options .CircuitBreaker != nil {
283
- err = c .options .CircuitBreaker .CanExecute ()
284
- // If the circuit breaker is open, return an error
272
+ if err != nil {
273
+ return
274
+ }
275
+ // Check if the request has an auth type and if so, execute the auth handler
276
+ if c .options .Auth != nil {
277
+ if handlerFunc , ok := c .options .AuthHandlers [c .options .Auth .Type ()]; ok {
278
+ err = handlerFunc (c , httpReq )
285
279
if err != nil {
286
280
return
287
281
}
282
+ } else {
283
+ err = fmt .Errorf ("invalid auth type or no handlerfunc found for auth type %v" , c .options .Auth .Type ())
284
+ return
288
285
}
289
- // Execute the request
290
- httpRes , err = c .httpClient .Do (httpReq )
291
- // Check if the response is an error
292
- isErr := c .isError (err , httpRes )
293
- if c .options .CircuitBreaker != nil {
294
- c .options .CircuitBreaker .OnExecution (! isErr )
286
+ }
287
+ // Check if the circuit breaker is open
288
+ if c .options .CircuitBreaker != nil {
289
+ err = c .options .CircuitBreaker .CanExecute ()
290
+ // If the circuit breaker is open, return an error
291
+ if err != nil {
292
+ return
295
293
}
296
- if isErr && c .options .RetryPolicy != nil {
297
- retryCount := 0
298
- // For each retry, sleep for the backoff interval and retry the request
299
- for isErr && retryCount < c .options .RetryPolicy .MaxRetries {
300
- time .Sleep (c .options .RetryPolicy .WaitTime (retryCount ))
301
- retryCount ++
302
- httpRes , err = c .httpClient .Do (httpReq )
303
- isErr = c .isError (err , httpRes )
304
- if c .options .CircuitBreaker != nil {
305
- c .options .CircuitBreaker .OnExecution (! isErr )
306
- }
294
+ }
295
+ // Execute the request
296
+ httpRes , err = c .httpClient .Do (httpReq )
297
+ // Check if the response is an error
298
+ isErr := c .isError (err , httpRes )
299
+ if c .options .CircuitBreaker != nil {
300
+ c .options .CircuitBreaker .OnExecution (! isErr )
301
+ }
302
+ if isErr && c .options .RetryPolicy != nil {
303
+ retryCount := 0
304
+ // For each retry, sleep for the backoff interval and retry the request
305
+ for isErr && retryCount < c .options .RetryPolicy .MaxRetries {
306
+ sleepFor := c .options .RetryPolicy .WaitTime (retryCount )
307
+ time .Sleep (sleepFor )
308
+ retryCount ++
309
+ httpRes , err = c .httpClient .Do (httpReq )
310
+ isErr = c .isError (err , httpRes )
311
+ if c .options .CircuitBreaker != nil {
312
+ c .options .CircuitBreaker .OnExecution (! isErr )
307
313
}
308
314
}
315
+ }
309
316
310
- httpRes , err = c .httpClient .Do (httpReq )
317
+ httpRes , err = c .httpClient .Do (httpReq )
311
318
312
- if err == nil {
313
- res = & Response {raw : httpRes , client : c }
314
- }
319
+ if err == nil {
320
+ res = & Response {raw : httpRes , client : c }
315
321
}
322
+
316
323
return
317
324
}
318
325
0 commit comments