Skip to content

Commit 05aef3f

Browse files
authored
Merge pull request #21 from vetinari/key-ratelimit
add SetClientKeyRateLimit
2 parents 1aca435 + 215be89 commit 05aef3f

File tree

2 files changed

+36
-6
lines changed

2 files changed

+36
-6
lines changed

keys.go

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,19 @@ type DSN struct {
1414

1515
// Key is a DSN that sentry has made
1616
type Key struct {
17-
Label string `json:"label,omitempty"`
18-
DSN DSN `json:"dsn,omitempty"`
19-
Secret string `json:"secret,omitempty"`
20-
ID string `json:"id,omitempty"`
21-
DateCreated time.Time `json:"dateCreated,omitempty"`
22-
Public string `json:"public,omitempty"`
17+
Label string `json:"label,omitempty"`
18+
DSN DSN `json:"dsn,omitempty"`
19+
Secret string `json:"secret,omitempty"`
20+
ID string `json:"id,omitempty"`
21+
DateCreated time.Time `json:"dateCreated,omitempty"`
22+
Public string `json:"public,omitempty"`
23+
RateLimit *KeyRateLimit `json:"rateLimit,omitempty"`
24+
}
25+
26+
// KeyRateLimit is the rate limit for a DSN
27+
type KeyRateLimit struct {
28+
Count int `json:"count"`
29+
Window int `json:"window"`
2330
}
2431

2532
type nameReq struct {
@@ -57,3 +64,11 @@ func (c *Client) GetClientKeys(o Organization, p Project) ([]Key, error) {
5764
err := c.do("GET", fmt.Sprintf("projects/%s/%s/keys", *o.Slug, *p.Slug), &keys, nil)
5865
return keys, err
5966
}
67+
68+
// SetClientKeyRateLimit updates the rate limit only of a key. window is in seconds.
69+
func (c *Client) SetClientKeyRateLimit(o Organization, p Project, k Key, count, window int) (Key, error) {
70+
var key Key
71+
req := &Key{RateLimit: &KeyRateLimit{Count: count, Window: window}}
72+
err := c.do("PUT", fmt.Sprintf("project/%s/%s/keys/%s", *o.Slug, *p.Slug, k.ID), &key, &req)
73+
return key, err
74+
}

keys_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ func TestKeysResource(t *testing.T) {
2828
if key.Label != "Test client key" {
2929
t.Error("Key does not have correct label")
3030
}
31+
if key.RateLimit != nil {
32+
t.Error("freshly created keys should not have rate limiting")
33+
}
3134
t.Run("List client keys", func(t *testing.T) {
3235
keys, err := client.GetClientKeys(org, project)
3336
if err != nil {
@@ -37,6 +40,18 @@ func TestKeysResource(t *testing.T) {
3740
t.Errorf("Expected 2 keys, got %d", len(keys))
3841
}
3942
})
43+
t.Run("Update rate limit for client key", func(t *testing.T) {
44+
resKey, err := client.SetClientKeyRateLimit(org, project, key, 1000, 60)
45+
if err != nil {
46+
t.Error(err)
47+
}
48+
if resKey.RateLimit == nil {
49+
t.Error("missing rate limit in updated key")
50+
}
51+
if resKey.RateLimit.Count != 1000 || resKey.RateLimit.Window != 60 {
52+
t.Error("failed to apply rate limit for key")
53+
}
54+
})
4055
t.Run("Update name of client key", func(t *testing.T) {
4156

4257
key, err = client.UpdateClientKey(org, project, key, "This is a new name")

0 commit comments

Comments
 (0)