-
Notifications
You must be signed in to change notification settings - Fork 9
/
api_keys.go
124 lines (97 loc) · 3.29 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
package resend
import (
"context"
"net/http"
)
type CreateApiKeyRequest struct {
Name string `json:"name"`
Permission string `json:"permission,omitempty"` // TODO: update permission to type
DomainId string `json:"domain_id,omitempty"`
}
type CreateApiKeyResponse struct {
Id string `json:"id"`
Token string `json:"token"`
}
type ListApiKeysResponse struct {
Data []ApiKey `json:"data"`
}
type ApiKey struct {
Id string `json:"id"`
Name string `json:"name"`
CreatedAt string `json:"created_at"`
}
type ApiKeysSvc interface {
CreateWithContext(ctx context.Context, params *CreateApiKeyRequest) (CreateApiKeyResponse, error)
Create(params *CreateApiKeyRequest) (CreateApiKeyResponse, error)
ListWithContext(ctx context.Context) (ListApiKeysResponse, error)
List() (ListApiKeysResponse, error)
RemoveWithContext(ctx context.Context, apiKeyId string) (bool, error)
Remove(apiKeyId string) (bool, error)
}
type ApiKeysSvcImpl struct {
client *Client
}
// CreateWithContext creates a new API Key based on the given params
// https://resend.com/docs/api-reference/api-keys/create-api-key
func (s *ApiKeysSvcImpl) CreateWithContext(ctx context.Context, params *CreateApiKeyRequest) (CreateApiKeyResponse, error) {
path := "api-keys"
// Prepare request
req, err := s.client.NewRequest(ctx, http.MethodPost, path, params)
if err != nil {
return CreateApiKeyResponse{}, ErrFailedToCreateApiKeysCreateRequest
}
// Build response recipient obj
apiKeysResp := new(CreateApiKeyResponse)
// Send Request
_, err = s.client.Perform(req, apiKeysResp)
if err != nil {
return CreateApiKeyResponse{}, err
}
return *apiKeysResp, nil
}
// Create creates a new API Key based on the given params
func (s *ApiKeysSvcImpl) Create(params *CreateApiKeyRequest) (CreateApiKeyResponse, error) {
return s.CreateWithContext(context.Background(), params)
}
// ListWithContext list all API Keys in the project
// https://resend.com/docs/api-reference/api-keys/list-api-keys
func (s *ApiKeysSvcImpl) ListWithContext(ctx context.Context) (ListApiKeysResponse, error) {
path := "api-keys"
// Prepare request
req, err := s.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return ListApiKeysResponse{}, ErrFailedToCreateApiKeysListRequest
}
// Build response recipient obj
apiKeysResp := new(ListApiKeysResponse)
// Send Request
_, err = s.client.Perform(req, apiKeysResp)
if err != nil {
return ListApiKeysResponse{}, err
}
return *apiKeysResp, nil
}
// List all API Keys in the project
func (s *ApiKeysSvcImpl) List() (ListApiKeysResponse, error) {
return s.ListWithContext(context.Background())
}
// RemoveWithContext deletes a given api key by id
// https://resend.com/docs/api-reference/api-keys/delete-api-key
func (s *ApiKeysSvcImpl) RemoveWithContext(ctx context.Context, apiKeyId string) (bool, error) {
path := "api-keys/" + apiKeyId
// Prepare request
req, err := s.client.NewRequest(ctx, http.MethodDelete, path, nil)
if err != nil {
return false, ErrFailedToCreateApiKeysRemoveRequest
}
// Send Request
_, err = s.client.Perform(req, nil)
if err != nil {
return false, err
}
return true, nil
}
// Remove deletes a given api key by id
func (s *ApiKeysSvcImpl) Remove(apiKeyId string) (bool, error) {
return s.RemoveWithContext(context.Background(), apiKeyId)
}