-
Notifications
You must be signed in to change notification settings - Fork 1
/
api_keys.go
executable file
·276 lines (224 loc) · 7.82 KB
/
api_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
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
// Code generated by `gogenitor`. DO NOT EDIT.
package sumup
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
"time"
)
// Apikey is the type definition for a Apikey.
type Apikey struct {
CreatedAt time.Time `json:"created_at"`
// Unique identifier of the API Key.
Id string `json:"id"`
// User-assigned name of the API Key.
Name string `json:"name"`
// The plaintext value of the API key. This field is returned only in the response to API key creation and is never again available in the plaintext form.
Plaintext *string `json:"plaintext,omitempty"`
// Last 8 characters of the API key.
Preview string `json:"preview"`
Scopes Oauth2Scopes `json:"scopes"`
Type ApikeyType `json:"type"`
UpdatedAt time.Time `json:"updated_at"`
}
type ApikeyType string
const (
ApikeyTypePublic ApikeyType = "public"
ApikeyTypeSecret ApikeyType = "secret"
)
// ApikeysList is the type definition for a ApikeysList.
type ApikeysList struct {
Items []Apikey `json:"items"`
TotalCount int `json:"total_count"`
}
type Oauth2Scope string
const (
Oauth2ScopeAccountingRead Oauth2Scope = "accounting.read"
Oauth2ScopeAccountingWrite Oauth2Scope = "accounting.write"
Oauth2ScopeEmail Oauth2Scope = "email"
Oauth2ScopeInvoicesRead Oauth2Scope = "invoices.read"
Oauth2ScopeInvoicesWrite Oauth2Scope = "invoices.write"
Oauth2ScopePaymentInstruments Oauth2Scope = "payment_instruments"
Oauth2ScopePayments Oauth2Scope = "payments"
Oauth2ScopeProducts Oauth2Scope = "products"
Oauth2ScopeProfile Oauth2Scope = "profile"
Oauth2ScopeReadersRead Oauth2Scope = "readers.read"
Oauth2ScopeReadersWrite Oauth2Scope = "readers.write"
Oauth2ScopeTransactionsHistory Oauth2Scope = "transactions.history"
Oauth2ScopeUserAppSettings Oauth2Scope = "user.app-settings"
Oauth2ScopeUserPayoutSettings Oauth2Scope = "user.payout-settings"
Oauth2ScopeUserProfile Oauth2Scope = "user.profile"
Oauth2ScopeUserProfileReadonly Oauth2Scope = "user.profile_readonly"
Oauth2ScopeUserSubaccounts Oauth2Scope = "user.subaccounts"
)
// Oauth2Scopes is the type definition for a Oauth2Scopes.
type Oauth2Scopes []Oauth2Scope
// ListApikeysParams are query parameters for ListApikeys
type ListApikeysParams struct {
Limit *int `json:"limit,omitempty"`
Offset *int `json:"offset,omitempty"`
}
// CreateAPIKey request body.
type CreateApikeyBody struct {
Name string `json:"name"`
Scopes Oauth2Scopes `json:"scopes"`
}
// UpdateAPIKey request body.
type UpdateApikeyBody struct {
// New name for the API key.
Name string `json:"name"`
Scopes Oauth2Scopes `json:"scopes"`
}
type ApiKeysService service
// ListApikeys: List API keys
// Returns paginated list of API keys.
func (s *ApiKeysService) ListApikeys(ctx context.Context, merchantCode string, params ListApikeysParams) (*ApikeysList, error) {
path := fmt.Sprintf("/v0.1/merchants/%v/api-keys", merchantCode)
req, err := s.client.NewRequest(ctx, http.MethodGet, path, http.NoBody)
if err != nil {
return nil, fmt.Errorf("error building request: %v", err)
}
resp, err := s.client.Do(req)
if err != nil {
return nil, fmt.Errorf("error sending request: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode >= 500 {
return nil, fmt.Errorf("invalid response: %d - %s", resp.StatusCode, http.StatusText(resp.StatusCode))
}
dec := json.NewDecoder(resp.Body)
if resp.StatusCode >= 400 {
var apiErr APIError
if err := dec.Decode(&apiErr); err != nil {
return nil, fmt.Errorf("read error response: %s", err.Error())
}
return nil, &apiErr
}
var v ApikeysList
if err := dec.Decode(&v); err != nil {
return nil, fmt.Errorf("decode response: %s", err.Error())
}
return &v, nil
}
// CreateApikey: Create an API key
// Creates a new API key for the user.
func (s *ApiKeysService) CreateApikey(ctx context.Context, merchantCode string, body CreateApikeyBody) (*Apikey, error) {
buf := new(bytes.Buffer)
if err := json.NewEncoder(buf).Encode(body); err != nil {
return nil, fmt.Errorf("encoding json body request failed: %v", err)
}
path := fmt.Sprintf("/v0.1/merchants/%v/api-keys", merchantCode)
req, err := s.client.NewRequest(ctx, http.MethodPost, path, buf)
if err != nil {
return nil, fmt.Errorf("error building request: %v", err)
}
resp, err := s.client.Do(req)
if err != nil {
return nil, fmt.Errorf("error sending request: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode >= 500 {
return nil, fmt.Errorf("invalid response: %d - %s", resp.StatusCode, http.StatusText(resp.StatusCode))
}
dec := json.NewDecoder(resp.Body)
if resp.StatusCode >= 400 {
var apiErr APIError
if err := dec.Decode(&apiErr); err != nil {
return nil, fmt.Errorf("read error response: %s", err.Error())
}
return nil, &apiErr
}
var v Apikey
if err := dec.Decode(&v); err != nil {
return nil, fmt.Errorf("decode response: %s", err.Error())
}
return &v, nil
}
// RevokeApikey: Revoke an API key
// Revokes an API key.
func (s *ApiKeysService) RevokeApikey(ctx context.Context, merchantCode string, keyId string) error {
path := fmt.Sprintf("/v0.1/merchants/%v/api-keys/%v", merchantCode, keyId)
req, err := s.client.NewRequest(ctx, http.MethodDelete, path, http.NoBody)
if err != nil {
return fmt.Errorf("error building request: %v", err)
}
resp, err := s.client.Do(req)
if err != nil {
return fmt.Errorf("error sending request: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode >= 500 {
return fmt.Errorf("invalid response: %d - %s", resp.StatusCode, http.StatusText(resp.StatusCode))
}
dec := json.NewDecoder(resp.Body)
if resp.StatusCode >= 400 {
var apiErr APIError
if err := dec.Decode(&apiErr); err != nil {
return fmt.Errorf("read error response: %s", err.Error())
}
return &apiErr
}
return nil
}
// GetApikey: Retrieve an API Key
// Gets an API key.
func (s *ApiKeysService) GetApikey(ctx context.Context, merchantCode string, keyId string) (*Apikey, error) {
path := fmt.Sprintf("/v0.1/merchants/%v/api-keys/%v", merchantCode, keyId)
req, err := s.client.NewRequest(ctx, http.MethodGet, path, http.NoBody)
if err != nil {
return nil, fmt.Errorf("error building request: %v", err)
}
resp, err := s.client.Do(req)
if err != nil {
return nil, fmt.Errorf("error sending request: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode >= 500 {
return nil, fmt.Errorf("invalid response: %d - %s", resp.StatusCode, http.StatusText(resp.StatusCode))
}
dec := json.NewDecoder(resp.Body)
if resp.StatusCode >= 400 {
var apiErr APIError
if err := dec.Decode(&apiErr); err != nil {
return nil, fmt.Errorf("read error response: %s", err.Error())
}
return nil, &apiErr
}
var v Apikey
if err := dec.Decode(&v); err != nil {
return nil, fmt.Errorf("decode response: %s", err.Error())
}
return &v, nil
}
// UpdateApikey: Update an API key
// Updates an API key.
func (s *ApiKeysService) UpdateApikey(ctx context.Context, merchantCode string, keyId string, body UpdateApikeyBody) error {
buf := new(bytes.Buffer)
if err := json.NewEncoder(buf).Encode(body); err != nil {
return fmt.Errorf("encoding json body request failed: %v", err)
}
path := fmt.Sprintf("/v0.1/merchants/%v/api-keys/%v", merchantCode, keyId)
req, err := s.client.NewRequest(ctx, http.MethodPut, path, buf)
if err != nil {
return fmt.Errorf("error building request: %v", err)
}
resp, err := s.client.Do(req)
if err != nil {
return fmt.Errorf("error sending request: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode >= 500 {
return fmt.Errorf("invalid response: %d - %s", resp.StatusCode, http.StatusText(resp.StatusCode))
}
dec := json.NewDecoder(resp.Body)
if resp.StatusCode >= 400 {
var apiErr APIError
if err := dec.Decode(&apiErr); err != nil {
return fmt.Errorf("read error response: %s", err.Error())
}
return &apiErr
}
return nil
}