-
Notifications
You must be signed in to change notification settings - Fork 39
/
keys.go
74 lines (64 loc) · 2.3 KB
/
keys.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package sentry
import (
"fmt"
"time"
)
// DSN is the actual connection strings used to send errors
type DSN struct {
Secret string `json:"secret,omitempty"`
CSP string `json:"csp,omitempty"`
Public string `json:"public,omitempty"`
}
// Key is a DSN that sentry has made
type Key struct {
Label string `json:"label,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"`
}
// KeyRateLimit is the rate limit for a DSN
type KeyRateLimit struct {
Count int `json:"count"`
Window int `json:"window"`
}
type nameReq struct {
Name string `json:"name"`
}
// CreateClientKey creates a new client key for a project and org
func (c *Client) CreateClientKey(o Organization, p Project, name string) (Key, error) {
var key Key
req := &nameReq{
Name: name,
}
err := c.do("POST", fmt.Sprintf("projects/%s/%s/keys", *o.Slug, *p.Slug), &key, &req)
return key, err
}
// DeleteClientKey deletes a client key for a project and org
func (c *Client) DeleteClientKey(o Organization, p Project, k Key) error {
return c.do("DELETE", fmt.Sprintf("projects/%s/%s/keys/%s", *o.Slug, *p.Slug, k.ID), nil, nil)
}
// UpdateClientKey updates the name only of a key
func (c *Client) UpdateClientKey(o Organization, p Project, k Key, name string) (Key, error) {
var key Key
req := &nameReq{
Name: name,
}
err := c.do("PUT", fmt.Sprintf("projects/%s/%s/keys/%s", *o.Slug, *p.Slug, k.ID), &key, &req)
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
err := c.do("GET", fmt.Sprintf("projects/%s/%s/keys", *o.Slug, *p.Slug), &keys, nil)
return keys, err
}
// SetClientKeyRateLimit updates the rate limit only of a key. window is in seconds.
func (c *Client) SetClientKeyRateLimit(o Organization, p Project, k Key, count, window int) (Key, error) {
var key Key
req := &Key{RateLimit: &KeyRateLimit{Count: count, Window: window}}
err := c.do("PUT", fmt.Sprintf("projects/%s/%s/keys/%s", *o.Slug, *p.Slug, k.ID), &key, &req)
return key, err
}