-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapi_keys.go
executable file
·261 lines (219 loc) · 7.49 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
// Code generated by `go-sdk-gen`. DO NOT EDIT.
package sumup
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
"net/url"
"strconv"
"time"
)
// Apikey is a schema definition.
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"`
// Max items: 128
Scopes Oauth2Scopes `json:"scopes"`
Type ApikeyType `json:"type"`
UpdatedAt time.Time `json:"updated_at"`
}
// ApikeyType is a schema definition.
type ApikeyType string
const (
ApikeyTypePublic ApikeyType = "public"
ApikeyTypeSecret ApikeyType = "secret"
)
// ApikeysList is a schema definition.
type ApikeysList struct {
Items []Apikey `json:"items"`
TotalCount int `json:"total_count"`
}
// Oauth2Scope is a schema definition.
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 a schema definition.
// Max items: 128
type Oauth2Scopes []Oauth2Scope
// CreateApikeyBody is a schema definition.
type CreateApikeyBody struct {
// Max length: 255
Name string `json:"name"`
// Max items: 128
Scopes Oauth2Scopes `json:"scopes"`
}
// UpdateApikeyBody is a schema definition.
type UpdateApikeyBody struct {
// New name for the API key.
// Max length: 255
Name string `json:"name"`
// Max items: 128
Scopes Oauth2Scopes `json:"scopes"`
}
// ListApikeysParams: query parameters for ListAPIKeys
type ListApikeysParams struct {
// Maximum number of keys to return.
Limit *int
// Offset of the first key to return.
Offset *int
}
// QueryValues converts [ListApikeysParams] into [url.Values].
func (p *ListApikeysParams) QueryValues() url.Values {
q := make(url.Values)
if p.Limit != nil {
q.Set("limit", strconv.Itoa(*p.Limit))
}
if p.Offset != nil {
q.Set("offset", strconv.Itoa(*p.Offset))
}
return q
}
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)
}
req.URL.RawQuery = params.QueryValues().Encode()
resp, err := s.client.Do(req)
if err != nil {
return nil, fmt.Errorf("error sending request: %v", err)
}
defer resp.Body.Close()
switch resp.StatusCode {
case http.StatusOK:
var v ApikeysList
if err := json.NewDecoder(resp.Body).Decode(&v); err != nil {
return nil, fmt.Errorf("decode response: %s", err.Error())
}
return &v, nil
default:
return nil, fmt.Errorf("unexpected response %d: %s", resp.StatusCode, http.StatusText(resp.StatusCode))
}
}
// 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()
switch resp.StatusCode {
case http.StatusCreated:
var v Apikey
if err := json.NewDecoder(resp.Body).Decode(&v); err != nil {
return nil, fmt.Errorf("decode response: %s", err.Error())
}
return &v, nil
default:
return nil, fmt.Errorf("unexpected response %d: %s", resp.StatusCode, http.StatusText(resp.StatusCode))
}
}
// 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()
switch resp.StatusCode {
case http.StatusNoContent:
return nil
default:
return fmt.Errorf("unexpected response %d: %s", resp.StatusCode, http.StatusText(resp.StatusCode))
}
}
// 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()
switch resp.StatusCode {
case http.StatusOK:
var v Apikey
if err := json.NewDecoder(resp.Body).Decode(&v); err != nil {
return nil, fmt.Errorf("decode response: %s", err.Error())
}
return &v, nil
default:
return nil, fmt.Errorf("unexpected response %d: %s", resp.StatusCode, http.StatusText(resp.StatusCode))
}
}
// 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()
switch resp.StatusCode {
case http.StatusNoContent:
return nil
default:
return fmt.Errorf("unexpected response %d: %s", resp.StatusCode, http.StatusText(resp.StatusCode))
}
}