Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to set API Key & API Key Secret manually #319

Merged
merged 4 commits into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ type NewClientInput struct {
AuthenticationMethod AuthenticationMethod
OAuthToken string
OAuthTokenSecret string
APIKey string
APIKeySecret string
Debug bool
}

Expand All @@ -63,6 +65,8 @@ type Client struct {
oauthToken string
oauthConsumerKey string
signingKey string
apiKeyOverride string
apiKeySecretOverride string
debug bool
}

Expand Down Expand Up @@ -90,6 +94,8 @@ func NewClient(in *NewClientInput) (*Client, error) {
c := Client{
Client: defaultHTTPClient,
authenticationMethod: in.AuthenticationMethod,
apiKeyOverride: in.APIKey,
apiKeySecretOverride: in.APIKeySecret,
debug: in.Debug,
}

Expand Down Expand Up @@ -127,8 +133,8 @@ func NewClientWithAccessToken(in *NewClientWithAccessTokenInput) (*Client, error
}

func (c *Client) authorize(oauthToken, oauthTokenSecret string) error {
apiKey := os.Getenv(APIKeyEnvName)
apiKeySecret := os.Getenv(APIKeySecretEnvName)
apiKey := c.APIKey()
apiKeySecret := c.APIKeySecret()
if apiKey == "" || apiKeySecret == "" {
return fmt.Errorf("env '%s' and '%s' is required.", APIKeyEnvName, APIKeySecretEnvName)
}
Expand Down Expand Up @@ -187,6 +193,20 @@ func (c *Client) AuthenticationMethod() AuthenticationMethod {
return c.authenticationMethod
}

func (c *Client) APIKey() string {
if c.apiKeyOverride != "" {
return c.apiKeyOverride
}
return os.Getenv(APIKeyEnvName)
}

func (c *Client) APIKeySecret() string {
if c.apiKeySecretOverride != "" {
return c.apiKeySecretOverride
}
return os.Getenv(APIKeySecretEnvName)
}

func (c *Client) OAuthToken() string {
return c.oauthToken
}
Expand Down
36 changes: 33 additions & 3 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ func (tp testParameter) ParameterMap() map[string]string { return nil }
type gotwiClientField struct {
AuthenticationMethod gotwi.AuthenticationMethod
AccessToken string
APIKey string
APIKeySecret string
OAuthToken string
OAuthConsumerKey string
SigningKey string
Expand Down Expand Up @@ -68,7 +70,7 @@ func Test_NewClient(t *testing.T) {
expect gotwiClientField
}{
{
name: "normal: OAuth1.0",
name: "normal: OAuth1.0 with api keys",
envAPIKey: "api-key",
envAPIKeySecret: "api-key-secret",
in: &gotwi.NewClientInput{
Expand All @@ -80,13 +82,35 @@ func Test_NewClient(t *testing.T) {
expect: gotwiClientField{
AuthenticationMethod: gotwi.AuthenMethodOAuth1UserContext,
AccessToken: "",
APIKey: "api-key",
APIKeySecret: "api-key-secret",
OAuthToken: "oauth-token",
OAuthConsumerKey: "api-key",
SigningKey: "api-key-secret&oauth-token-secret",
},
},
{
name: "normal: OAuth2.0",
name: "normal: OAuth1.0 with env api keys",
envAPIKey: "api-key",
envAPIKeySecret: "api-key-secret",
in: &gotwi.NewClientInput{
AuthenticationMethod: gotwi.AuthenMethodOAuth1UserContext,
OAuthToken: "oauth-token",
OAuthTokenSecret: "oauth-token-secret",
},
wantErr: false,
expect: gotwiClientField{
AuthenticationMethod: gotwi.AuthenMethodOAuth1UserContext,
AccessToken: "",
APIKey: "api-key",
APIKeySecret: "api-key-secret",
OAuthToken: "oauth-token",
OAuthConsumerKey: "api-key",
SigningKey: "api-key-secret&oauth-token-secret",
},
},
{
name: "normal: OAuth2.0 with override api key and override api key secret",
envAPIKey: "api-key",
envAPIKeySecret: "api-key-secret",
mockInput: &mockInput{
Expand All @@ -95,12 +119,16 @@ func Test_NewClient(t *testing.T) {
},
in: &gotwi.NewClientInput{
AuthenticationMethod: gotwi.AuthenMethodOAuth2BearerToken,
APIKey: "override-api-key",
APIKeySecret: "override-api-key-secret",
},
wantErr: false,
expect: gotwiClientField{
AuthenticationMethod: gotwi.AuthenMethodOAuth2BearerToken,
AccessToken: "access_token",
OAuthConsumerKey: "api-key",
APIKey: "override-api-key",
APIKeySecret: "override-api-key-secret",
OAuthConsumerKey: "override-api-key",
},
},
{
Expand Down Expand Up @@ -200,6 +228,8 @@ func Test_NewClient(t *testing.T) {
assert.NoError(tt, err)
assert.Equal(tt, c.expect.AuthenticationMethod, gc.AuthenticationMethod())
assert.Equal(tt, c.expect.AccessToken, gc.AccessToken())
assert.Equal(tt, c.expect.APIKey, gc.APIKey())
assert.Equal(tt, c.expect.APIKeySecret, gc.APIKeySecret())
assert.Equal(tt, c.expect.OAuthToken, gc.OAuthToken())
assert.Equal(tt, c.expect.OAuthConsumerKey, gc.OAuthConsumerKey())
assert.Equal(tt, c.expect.SigningKey, gc.SigningKey())
Expand Down
Loading