-
Notifications
You must be signed in to change notification settings - Fork 17
/
api_key.go
61 lines (50 loc) · 1.93 KB
/
api_key.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 easypost
import (
"context"
"net/http"
)
// APIKey represents a single API key.
type APIKey struct {
Object string `json:"object,omitempty" url:"object,omitempty"`
Mode string `json:"mode,omitempty" url:"mode,omitempty"`
CreatedAt *DateTime `json:"created_at,omitempty" url:"created_at,omitempty"`
Key string `json:"key,omitempty" url:"key,omitempty"`
}
// APIKeys contains information about a list of API keys for the given user and
// any child users.
type APIKeys struct {
ID string `json:"id,omitempty" url:"id,omitempty"`
Children []*APIKeys `json:"children,omitempty" url:"children,omitempty"`
Keys []*APIKey `json:"keys,omitempty" url:"keys,omitempty"`
}
// GetAPIKeys returns the list of API keys associated with the current user.
func (c *Client) GetAPIKeys() (out *APIKeys, err error) {
return c.GetAPIKeysWithContext(context.Background())
}
// GetAPIKeysWithContext performs the same operation as GetAPIKeys, but allows
// specifying a context that can interrupt the request.
func (c *Client) GetAPIKeysWithContext(ctx context.Context) (out *APIKeys, err error) {
err = c.do(ctx, http.MethodGet, "api_keys", nil, &out)
return
}
// GetAPIKeysForUser returns the list of API keys associated with the given user ID.
func (c *Client) GetAPIKeysForUser(userID string) (out *APIKeys, err error) {
return c.GetAPIKeysForUserWithContext(context.Background(), userID)
}
// GetAPIKeysForUserWithContext performs the same operation as GetAPIKeysForUser, but allows
// specifying a context that can interrupt the request.
func (c *Client) GetAPIKeysForUserWithContext(ctx context.Context, userID string) (out *APIKeys, err error) {
keys, err := c.GetAPIKeysWithContext(ctx)
if err != nil {
return nil, err
}
if keys.ID == userID {
return keys, nil
}
for _, child := range keys.Children {
if child.ID == userID {
return child, nil
}
}
return nil, newFilteringError(NoUserFoundForId)
}