forked from ns1/ns1-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaccount_apikey.go
140 lines (117 loc) · 3.21 KB
/
account_apikey.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
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
package rest
import (
"errors"
"fmt"
"net/http"
"gopkg.in/ns1/ns1-go.v2/rest/model/account"
)
// APIKeysService handles 'account/apikeys' endpoint.
type APIKeysService service
// List returns all api keys in the account.
//
// NS1 API docs: https://ns1.com/api/#apikeys-get
func (s *APIKeysService) List() ([]*account.APIKey, *http.Response, error) {
req, err := s.client.NewRequest("GET", "account/apikeys", nil)
if err != nil {
return nil, nil, err
}
kl := []*account.APIKey{}
resp, err := s.client.Do(req, &kl)
if err != nil {
return nil, resp, err
}
return kl, resp, nil
}
// Get returns details of an api key, including permissions, for a single API Key.
// Note: do not use the API Key itself as the keyid in the URL — use the id of the key.
//
// NS1 API docs: https://ns1.com/api/#apikeys-id-get
func (s *APIKeysService) Get(keyID string) (*account.APIKey, *http.Response, error) {
path := fmt.Sprintf("account/apikeys/%s", keyID)
req, err := s.client.NewRequest("GET", path, nil)
if err != nil {
return nil, nil, err
}
var a account.APIKey
resp, err := s.client.Do(req, &a)
if err != nil {
switch err.(type) {
case *Error:
if err.(*Error).Message == "unknown api key" {
return nil, resp, ErrKeyMissing
}
}
return nil, resp, err
}
return &a, resp, nil
}
// Create takes a *APIKey and creates a new account apikey.
//
// NS1 API docs: https://ns1.com/api/#apikeys-put
func (s *APIKeysService) Create(a *account.APIKey) (*http.Response, error) {
req, err := s.client.NewRequest("PUT", "account/apikeys", &a)
if err != nil {
return nil, err
}
// Update account fields with data from api(ensure consistent)
resp, err := s.client.Do(req, &a)
if err != nil {
switch err.(type) {
case *Error:
if err.(*Error).Message == fmt.Sprintf("api key with name \"%s\" exists", a.Name) {
return resp, ErrKeyExists
}
}
return resp, err
}
return resp, nil
}
// Update changes the name or access rights for an API Key.
//
// NS1 API docs: https://ns1.com/api/#apikeys-id-post
func (s *APIKeysService) Update(a *account.APIKey) (*http.Response, error) {
path := fmt.Sprintf("account/apikeys/%s", a.ID)
req, err := s.client.NewRequest("POST", path, &a)
if err != nil {
return nil, err
}
// Update apikey fields with data from api(ensure consistent)
resp, err := s.client.Do(req, &a)
if err != nil {
switch err.(type) {
case *Error:
if err.(*Error).Message == "unknown api key" {
return resp, ErrKeyMissing
}
}
return resp, err
}
return resp, nil
}
// Delete deletes an apikey.
//
// NS1 API docs: https://ns1.com/api/#apikeys-id-delete
func (s *APIKeysService) Delete(keyID string) (*http.Response, error) {
path := fmt.Sprintf("account/apikeys/%s", keyID)
req, err := s.client.NewRequest("DELETE", path, nil)
if err != nil {
return nil, err
}
resp, err := s.client.Do(req, nil)
if err != nil {
switch err.(type) {
case *Error:
if err.(*Error).Message == "unknown api key" {
return resp, ErrKeyMissing
}
}
return resp, err
}
return resp, nil
}
var (
// ErrKeyExists bundles PUT create error.
ErrKeyExists = errors.New("key already exists")
// ErrKeyMissing bundles GET/POST/DELETE error.
ErrKeyMissing = errors.New("key does not exist")
)