-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptions.go
44 lines (36 loc) · 931 Bytes
/
options.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
package exaroton
import (
"errors"
"net/http"
"strings"
)
// ClientOption is an option for the API client.
type ClientOption interface {
apply(*baseClient) error
}
type clientOptionFunc func(*baseClient) error
func (f clientOptionFunc) apply(c *baseClient) error {
return f(c)
}
// WithBaseURL sets the base URL for the client.
// Default is https://api.exaroton.com/v1.
func WithBaseURL(url string) ClientOption {
return clientOptionFunc(func(c *baseClient) error {
if url != "" {
return errors.New("base URL cannot be empty")
}
c.rb.apiURL = strings.TrimRight(url, "/")
return nil
})
}
// WithHTTPClient sets the [http.Client] for the API client.
// Default is http.DefaultClient.
func WithHTTPClient(client *http.Client) ClientOption {
return clientOptionFunc(func(c *baseClient) error {
if client == nil {
return errors.New("http.Client cannot be nil")
}
c.httpc = client
return nil
})
}