-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathapi_key.go
More file actions
142 lines (112 loc) · 3.17 KB
/
api_key.go
File metadata and controls
142 lines (112 loc) · 3.17 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package sendgrid
import (
"context"
"fmt"
)
type APIKey struct {
ApiKeyId string `json:"api_key_id,omitempty"`
Name string `json:"name,omitempty"`
}
type OutputGetAPIKeys struct {
APIKeys []APIKey `json:"result,omitempty"`
}
func (c *Client) GetAPIKeys(ctx context.Context) (*OutputGetAPIKeys, error) {
req, err := c.NewRequest("GET", "/api_keys", nil)
if err != nil {
return nil, err
}
r := new(OutputGetAPIKeys)
if err := c.Do(ctx, req, &r); err != nil {
return nil, err
}
return r, nil
}
type OutputGetAPIKey struct {
ApiKeyId string `json:"api_key_id,omitempty"`
Name string `json:"name,omitempty"`
Scopes []string `json:"scopes,omitempty"`
}
func (c *Client) GetAPIKey(ctx context.Context, apiKeyId string) (*OutputGetAPIKey, error) {
path := fmt.Sprintf("/api_keys/%s", apiKeyId)
req, err := c.NewRequest("GET", path, nil)
if err != nil {
return nil, err
}
r := new(OutputGetAPIKey)
if err := c.Do(ctx, req, &r); err != nil {
return nil, err
}
return r, nil
}
type InputCreateAPIKey struct {
Name string `json:"name,omitempty"`
Scopes []string `json:"scopes,omitempty"`
}
type OutputCreateAPIKey struct {
ApiKey string `json:"api_key,omitempty"`
ApiKeyId string `json:"api_key_id,omitempty"`
Name string `json:"name,omitempty"`
Scopes []string `json:"scopes,omitempty"`
}
func (c *Client) CreateAPIKey(ctx context.Context, input *InputCreateAPIKey) (*OutputCreateAPIKey, error) {
req, err := c.NewRequest("POST", "/api_keys", input)
if err != nil {
return nil, err
}
r := new(OutputCreateAPIKey)
if err := c.Do(ctx, req, &r); err != nil {
return nil, err
}
return r, nil
}
type InputUpdateAPIKeyName struct {
Name string `json:"name"`
}
type OutputUpdateAPIKeyName struct {
ApiKeyId string `json:"api_key_id,omitempty"`
Name string `json:"name,omitempty"`
}
func (c *Client) UpdateAPIKeyName(ctx context.Context, apiKeyId string, input *InputUpdateAPIKeyName) (*OutputUpdateAPIKeyName, error) {
u := fmt.Sprintf("/api_keys/%s", apiKeyId)
req, err := c.NewRequest("PATCH", u, input)
if err != nil {
return nil, err
}
r := new(OutputUpdateAPIKeyName)
if err := c.Do(ctx, req, &r); err != nil {
return nil, err
}
return r, nil
}
type InputUpdateAPIKeyNameAndScopes struct {
Name string `json:"name"`
Scopes []string `json:"scopes,omitempty"`
}
type OutputUpdateAPIKeyNameAndScopes struct {
ApiKeyId string `json:"api_key_id,omitempty"`
Name string `json:"name"`
Scopes []string `json:"scopes,omitempty"`
}
func (c *Client) UpdateAPIKeyNameAndScopes(ctx context.Context, apiKeyId string, input *InputUpdateAPIKeyNameAndScopes) (*OutputUpdateAPIKeyNameAndScopes, error) {
u := fmt.Sprintf("/api_keys/%s", apiKeyId)
req, err := c.NewRequest("PUT", u, input)
if err != nil {
return nil, err
}
r := new(OutputUpdateAPIKeyNameAndScopes)
if err := c.Do(ctx, req, &r); err != nil {
return nil, err
}
return r, nil
}
func (c *Client) DeleteAPIKey(ctx context.Context, apiKeyId string) error {
u := fmt.Sprintf("/api_keys/%s", apiKeyId)
req, err := c.NewRequest("DELETE", u, nil)
if err != nil {
return err
}
if err := c.Do(ctx, req, nil); err != nil {
return err
}
return nil
}