Skip to content

Commit

Permalink
Make arguments to APIKey-related methods consistent by accepting poin…
Browse files Browse the repository at this point in the history
…ters as arguments everywhere
  • Loading branch information
Slavek Kabrda committed Apr 16, 2019
1 parent 6de206f commit 7a0e23c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
10 changes: 5 additions & 5 deletions api_keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,17 +81,17 @@ func (client *Client) GetAPIKeys() ([]APIKey, error) {
}

// GetAPIKey returns a single API key or error on failure
func (client *Client) GetAPIKey(key string) (*APIKey, error) {
func (client *Client) GetAPIKey(key *string) (*APIKey, error) {
var out reqAPIKey
if err := client.doJsonRequest("GET", fmt.Sprintf("/v1/api_key/%s", key), nil, &out); err != nil {
if err := client.doJsonRequest("GET", fmt.Sprintf("/v1/api_key/%s", *key), nil, &out); err != nil {
return nil, err
}

return out.APIKey, nil
}

// CreateAPIKey creates an API key from given struct and fills the rest of its
// fileds, or returns an error on failure
// fields, or returns an error on failure
func (client *Client) CreateAPIKey(name *string) (*APIKey, error) {
toPost := struct {
Name *string `json:"name,omitempty"`
Expand All @@ -117,6 +117,6 @@ func (client *Client) UpdateAPIKey(apikey *APIKey) error {
}

// DeleteAPIKey deletes API key given by key, returns an error
func (client *Client) DeleteAPIKey(key string) error {
return client.doJsonRequest("DELETE", fmt.Sprintf("/v1/api_key/%s", key), nil, nil)
func (client *Client) DeleteAPIKey(key *string) error {
return client.doJsonRequest("DELETE", fmt.Sprintf("/v1/api_key/%s", *key), nil, nil)
}
12 changes: 6 additions & 6 deletions integration/api_keys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ func TestAPIKeyCreateGetAndDelete(t *testing.T) {
if err != nil {
t.Fatalf("Creating API key failed when it shouldn't. (%s)", err)
}
defer cleanUpAPIKey(t, *expected.Key)
defer cleanUpAPIKey(t, expected.Key)

if *expected.Name != keyName {
t.Errorf("Created key has wrong name. Got %s, want %s", *expected.Name, keyName)
}

// now try to fetch it freshly and compare it again
actual, err := client.GetAPIKey(*expected.Key)
actual, err := client.GetAPIKey(expected.Key)
if err != nil {
t.Fatalf("Retrieving API key failed when it shouldn't. (%s)", err)
}
Expand All @@ -40,7 +40,7 @@ func TestAPIKeyUpdateName(t *testing.T) {
if err != nil {
t.Fatalf("Creating API key failed when it shouldn't. (%s)", err)
}
defer cleanUpAPIKey(t, *keyStruct.Key)
defer cleanUpAPIKey(t, keyStruct.Key)

keyStruct.Name = newKeyName
err = client.UpdateAPIKey(keyStruct)
Expand All @@ -60,12 +60,12 @@ func TestAPIKeyGetMultipleKeys(t *testing.T) {
if err != nil {
t.Fatalf("Creating API key failed when it shouldn't. (%s)", err)
}
defer cleanUpAPIKey(t, *key1.Key)
defer cleanUpAPIKey(t, key1.Key)
key2, err := client.CreateAPIKey(key2Name)
if err != nil {
t.Fatalf("Creating API key failed when it shouldn't. (%s)", err)
}
defer cleanUpAPIKey(t, *key2.Key)
defer cleanUpAPIKey(t, key2.Key)
allKeys, err := client.GetAPIKeys()
if err != nil {
t.Fatalf("Getting all API keys failed when it shouldn't (%s)", err)
Expand Down Expand Up @@ -104,7 +104,7 @@ func assertAPIKeyEquals(t *testing.T, actual, expected *datadog.APIKey) {
}
}

func cleanUpAPIKey(t *testing.T, key string) {
func cleanUpAPIKey(t *testing.T, key *string) {
if err := client.DeleteAPIKey(key); err != nil {
t.Fatalf("Deleting api key failed when it shouldn't. Manual cleanup needed. (%s)", err)
}
Expand Down

0 comments on commit 7a0e23c

Please sign in to comment.