-
Notifications
You must be signed in to change notification settings - Fork 8
/
keys.go
61 lines (49 loc) · 1.19 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
package scalingo
import (
"context"
"gopkg.in/errgo.v1"
)
type KeysService interface {
KeysList(context.Context) ([]Key, error)
KeysAdd(ctx context.Context, name string, content string) (*Key, error)
KeysDelete(ctx context.Context, id string) error
}
var _ KeysService = (*Client)(nil)
type Key struct {
ID string `json:"id,omitempty"`
Name string `json:"name"`
Content string `json:"content"`
}
type KeyRes struct {
Key Key `json:"key"`
}
type KeysRes struct {
Keys []Key `json:"keys"`
}
func (c *Client) KeysList(ctx context.Context) ([]Key, error) {
var res KeysRes
err := c.AuthAPI().ResourceList(ctx, "keys", nil, &res)
if err != nil {
return nil, errgo.Mask(err)
}
return res.Keys, nil
}
func (c *Client) KeysAdd(ctx context.Context, name string, content string) (*Key, error) {
payload := KeyRes{Key{
Name: name,
Content: content,
}}
var res KeyRes
err := c.AuthAPI().ResourceAdd(ctx, "keys", payload, &res)
if err != nil {
return nil, errgo.Mask(err)
}
return &res.Key, nil
}
func (c *Client) KeysDelete(ctx context.Context, id string) error {
err := c.AuthAPI().ResourceDelete(ctx, "keys", id)
if err != nil {
return errgo.Mask(err)
}
return nil
}