Skip to content

Commit

Permalink
ref: make NewClient() api wrapper accept token as input
Browse files Browse the repository at this point in the history
It feels like the proper thing to do is have the NewClient() wrapper
func accept the token as a struct field param to use to configure the
api client similar to the other params, rather than have this internal
wrapper func find and retrieve the token itself from the env.

This refactors things to surface the token retreival to the main package
in the `Run()` func for use with creating the api client to provide to
the exporter/sd workers.
  • Loading branch information
tjhop committed Jan 29, 2024
1 parent b778c1d commit c19442a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
7 changes: 7 additions & 0 deletions cmd/ns1_exporter/ns1_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,14 @@ func main() {
}

func Run(logger log.Logger) {
token := os.Getenv("NS1_APIKEY")
if token == "" {
level.Error(logger).Log("err", "NS1_APIKEY environment variable is not set")
os.Exit(1)
}

apiClient := ns1.NewClient(ns1.APIConfig{
Token: token,
Concurrency: *flagNS1Concurrency,
UserAgent: fmt.Sprintf("ns1_exporter/%s", version.Version),
// EnableDDI: *flagNS1EnableDDI,
Expand Down
10 changes: 2 additions & 8 deletions pkg/ns1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package ns1
import (
"crypto/tls"
"net/http"
"os"
"regexp"
"time"

Expand All @@ -30,6 +29,7 @@ import (
)

type APIConfig struct {
Token string
Concurrency int
Endpoint string
TLSSkipVerify bool
Expand Down Expand Up @@ -66,14 +66,8 @@ type QPS struct {

// NewClient creates a new NS1 API client based on the provided config
func NewClient(config APIConfig) *api.Client {
token := os.Getenv("NS1_APIKEY")
if token == "" {
level.Error(logger).Log("err", "NS1_APIKEY environment variable is not set")
os.Exit(1)
}

httpClient := &http.Client{Timeout: time.Second * 15}
clientOpts := []func(*api.Client){api.SetAPIKey(token), api.SetFollowPagination(true), api.SetUserAgent(config.UserAgent)}
clientOpts := []func(*api.Client){api.SetAPIKey(config.Token), api.SetFollowPagination(true), api.SetUserAgent(config.UserAgent)}

if config.Endpoint != "" {
clientOpts = append(clientOpts, api.SetEndpoint(config.Endpoint))
Expand Down

0 comments on commit c19442a

Please sign in to comment.