Use functional options pattern

This commit is contained in:
ekzyis 2024-04-07 05:21:07 +02:00
parent 6debdd8ba6
commit 2a663690a8
1 changed files with 25 additions and 15 deletions

View File

@ -9,29 +9,39 @@ import (
"os"
)
type ClientOptions struct {
BaseUrl string
ApiKey string
}
type Client struct {
BaseUrl string
ApiUrl string
ApiKey string
}
func NewClient(options *ClientOptions) *Client {
if options.BaseUrl == "" {
options.BaseUrl = "https://stacker.news"
}
if options.ApiKey == "" {
options.ApiKey = os.Getenv("SN_API_KEY")
func NewClient(options ...func(*Client)) *Client {
c := &Client{}
for _, o := range options {
o(c)
}
return &Client{
BaseUrl: options.BaseUrl,
ApiUrl: fmt.Sprintf("%s/api/graphql", options.BaseUrl),
ApiKey: options.ApiKey,
// set defaults
if c.BaseUrl == "" {
c.BaseUrl = "https://stacker.news"
}
if c.ApiKey == "" {
c.ApiKey = os.Getenv("SN_API_KEY")
}
c.ApiUrl = fmt.Sprintf("%s/api/graphql", c.BaseUrl)
return c
}
func WithApiKey(apiKey string) func(*Client) {
return func(c *Client) {
c.ApiKey = apiKey
}
}
func WithBaseUrl(baseUrl string) func(*Client) {
return func(c *Client) {
c.BaseUrl = baseUrl
}
}