From e21b04172d5825ba6145f89527b514450d43661f Mon Sep 17 00:00:00 2001
From: Arkadiusz Adamski
Date: Wed, 9 Oct 2024 17:40:10 +0200
Subject: [PATCH] Refactor key struct and add GetClientKey method
---
keys.go | 10 +++++++++-
keys_test.go | 25 +++++++++++++++++++++++--
2 files changed, 32 insertions(+), 3 deletions(-)
diff --git a/keys.go b/keys.go
index c6c25f7..3bd5a6e 100644
--- a/keys.go
+++ b/keys.go
@@ -14,10 +14,11 @@ type DSN struct {
// Key is a DSN that sentry has made
type Key struct {
+ ID string `json:"id,omitempty"`
Label string `json:"label,omitempty"`
+ Name string `json:"name,omitempty"`
DSN DSN `json:"dsn,omitempty"`
Secret string `json:"secret,omitempty"`
- ID string `json:"id,omitempty"`
DateCreated time.Time `json:"dateCreated,omitempty"`
Public string `json:"public,omitempty"`
RateLimit *KeyRateLimit `json:"rateLimit,omitempty"`
@@ -58,6 +59,13 @@ func (c *Client) UpdateClientKey(o Organization, p Project, k Key, name string)
return key, err
}
+// GetClientKey fetches a client key of the given project, org and key ID
+func (c *Client) GetClientKey(o Organization, p Project, keyId string) (Key, error) {
+ var key Key
+ err := c.do("GET", fmt.Sprintf("projects/%s/%s/keys/%s", *o.Slug, *p.Slug, keyId), &key, nil)
+ return key, err
+}
+
// GetClientKeys fetches all client keys of the given project
func (c *Client) GetClientKeys(o Organization, p Project) ([]Key, error) {
var keys []Key
diff --git a/keys_test.go b/keys_test.go
index 5945acb..c34c96f 100644
--- a/keys_test.go
+++ b/keys_test.go
@@ -4,6 +4,10 @@ import (
"testing"
)
+const (
+ testClientKeyName = "Test client key"
+)
+
func TestKeysResource(t *testing.T) {
t.Parallel()
@@ -21,13 +25,16 @@ func TestKeysResource(t *testing.T) {
defer cleanupproj()
t.Run("Create a new key for project", func(t *testing.T) {
- key, err := client.CreateClientKey(org, project, "Test client key")
+ key, err := client.CreateClientKey(org, project, testClientKeyName)
if err != nil {
t.Error(err)
}
- if key.Label != "Test client key" {
+ if key.Label != testClientKeyName {
t.Error("Key does not have correct label")
}
+ if key.Label != testClientKeyName {
+ t.Error("Key does not have correct name")
+ }
if key.RateLimit != nil {
t.Error("freshly created keys should not have rate limiting")
}
@@ -70,6 +77,20 @@ func TestKeysResource(t *testing.T) {
}
})
})
+ t.Run("Get client key", func(t *testing.T) {
+ key, err := client.GetClientKey(org, project, key.ID)
+ if err != nil {
+ t.Error(err)
+ }
+
+ if key.ID != key.ID {
+ t.Error("Failed to fetch key")
+ }
+
+ if key.Name != testClientKeyName {
+ t.Error("Key does not have correct name")
+ }
+ })
})
}