Skip to content
This repository has been archived by the owner on Jun 21, 2024. It is now read-only.

Commit

Permalink
add private field Client.apiKeySet
Browse files Browse the repository at this point in the history
Keep backward compatibility for users who emitted an empty X-Auth-Token
(verbatim `X-Auth-Token: ` and end of line). Could happen in tests
and whatnot.

Signed-off-by: Jakub Bielecki <[email protected]>
  • Loading branch information
jabielecki committed Oct 28, 2022
1 parent c8f9431 commit b386c0b
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
1 change: 1 addition & 0 deletions clientopt.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ func WithAuth(consumerToken string, apiKey string) ClientOpt {
return func(c *Client) error {
c.ConsumerToken = consumerToken
c.APIKey = apiKey
c.apiKeySet = true

return nil
}
Expand Down
11 changes: 7 additions & 4 deletions packngo.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ type Client struct {
UserAgent string
ConsumerToken string
APIKey string
apiKeySet bool
header http.Header

RateLimit Rate
Expand Down Expand Up @@ -424,12 +425,14 @@ func NewClient(opts ...ClientOpt) (*Client, error) {
}
}

if c.APIKey == "" {
if !c.apiKeySet {
c.APIKey = os.Getenv(authTokenEnvVar)
}

if c.APIKey == "" {
return nil, fmt.Errorf("you must export %s", authTokenEnvVar)
if c.APIKey == "" {
return nil, fmt.Errorf("you must export %s", authTokenEnvVar)
}

c.apiKeySet = true
}

return c, nil
Expand Down

0 comments on commit b386c0b

Please sign in to comment.