Skip to content
This repository was archived by the owner on Mar 23, 2023. It is now read-only.

Commit b93e6ff

Browse files
authored
Make client configurable (#5)
1 parent 20e8a33 commit b93e6ff

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

client.go

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,36 @@ type Client struct {
1818
Logger *log.Logger
1919
}
2020

21+
// ClientOption modifies fields on a Client
22+
type ClientOption func(c *Client)
23+
2124
// NewClient returns a Client pointer
22-
func NewClient() *Client {
25+
func NewClient(opts ...ClientOption) *Client {
2326
baseURL, _ := url.Parse("https://app.opslevel.com")
24-
return &Client{
27+
client := &Client{
2528
baseURL: baseURL,
2629
httpClient: &http.Client{},
2730
Logger: log.StandardLogger(),
2831
}
32+
for _, o := range opts {
33+
o(client)
34+
}
35+
return client
36+
}
37+
38+
// WithBaseURL modifies the Client baseURL.
39+
func WithBaseURL(baseURL string) ClientOption {
40+
return func(c *Client) {
41+
bu, _ := url.Parse(baseURL)
42+
c.baseURL = bu
43+
}
44+
}
45+
46+
// WithHTTPClient modifies the Client http.Client.
47+
func WithHTTPClient(hc *http.Client) ClientOption {
48+
return func(c *Client) {
49+
c.httpClient = hc
50+
}
2951
}
3052

3153
func (c *Client) do(method string, path string, body io.Reader, recv interface{}) error {

0 commit comments

Comments
 (0)