-
Notifications
You must be signed in to change notification settings - Fork 7
/
api_token.go
117 lines (90 loc) · 2.44 KB
/
api_token.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
package appoptics
import "fmt"
type ApiToken struct {
ID *int `json:"id,omitempty"`
Active *bool `json:"active,omitempty"`
Name *string `json:"name,omitempty"`
Role *string `json:"role,omitempty"`
Token *string `json:"token,omitempty"`
}
type ApiTokensResponse struct {
Query QueryInfo `json:"query,omitempty"`
ApiTokens []*ApiToken `json:"api_tokens,omitempty"`
}
type ApiTokensCommunicator interface {
List() (*ApiTokensResponse, error)
Retrieve(string) (*ApiTokensResponse, error)
Create(*ApiToken) (*ApiToken, error)
Update(*ApiToken) (*ApiToken, error)
Delete(int) error
}
type ApiTokensService struct {
client *Client
}
func NewApiTokensService(c *Client) *ApiTokensService {
return &ApiTokensService{c}
}
// List retrieves all ApiTokens
func (ts *ApiTokensService) List() (*ApiTokensResponse, error) {
req, err := ts.client.NewRequest("GET", "api_tokens", nil)
if err != nil {
return nil, err
}
apiResponse := &ApiTokensResponse{}
_, err = ts.client.Do(req, &apiResponse)
if err != nil {
return nil, err
}
return apiResponse, nil
}
// Retrieve returns the ApiToken identified by the parameter
func (ts *ApiTokensService) Retrieve(name string) (*ApiTokensResponse, error) {
tokenResponse := &ApiTokensResponse{}
path := fmt.Sprintf("api_tokens/%s", name)
req, err := ts.client.NewRequest("GET", path, nil)
if err != nil {
return nil, err
}
_, err = ts.client.Do(req, tokenResponse)
if err != nil {
return nil, err
}
return tokenResponse, nil
}
// Create creates the ApiToken
func (ts *ApiTokensService) Create(at *ApiToken) (*ApiToken, error) {
req, err := ts.client.NewRequest("POST", "api_tokens", at)
if err != nil {
return nil, err
}
createdToken := &ApiToken{}
_, err = ts.client.Do(req, createdToken)
if err != nil {
return nil, err
}
return createdToken, nil
}
// Update updates the ApiToken
func (ts *ApiTokensService) Update(at *ApiToken) (*ApiToken, error) {
path := fmt.Sprintf("api_tokens/%d", at.ID)
req, err := ts.client.NewRequest("PUT", path, at)
if err != nil {
return nil, err
}
updatedToken := &ApiToken{}
_, err = ts.client.Do(req, updatedToken)
if err != nil {
return nil, err
}
return updatedToken, nil
}
// Delete deletes the ApiToken
func (ts *ApiTokensService) Delete(id int) error {
path := fmt.Sprintf("api_tokens/%d", id)
req, err := ts.client.NewRequest("DELETE", path, nil)
if err != nil {
return err
}
_, err = ts.client.Do(req, nil)
return err
}