/
endpoint.go
198 lines (172 loc) · 5.6 KB
/
endpoint.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
package upapi
import (
"context"
"fmt"
"net/http"
)
type Itemable[Item any] interface {
Item() Item
}
type Listable[Item any] interface {
List() []Item
}
type PrimaryKeyable interface {
PrimaryKey() PrimaryKey
}
type PrimaryKey int
func (p PrimaryKey) PrimaryKey() PrimaryKey {
return p
}
// EndpointGetter is a generic interface for getting a single item from an endpoint.
type EndpointGetter[ResponseType Itemable[ItemType], ItemType any] interface {
Get(ctx context.Context, pk PrimaryKeyable) (*ItemType, error)
}
func NewEndpointGetter[ResponseType Itemable[ItemType], ItemType any](cbd CBD, endpoint string) EndpointGetter[ResponseType, ItemType] {
return &endpointGetterImpl[ResponseType, ItemType]{cbd, endpoint}
}
type endpointGetterImpl[ResponseType Itemable[ItemType], ItemType any] struct {
CBD
endpoint string
}
func (p *endpointGetterImpl[ResponseType, ItemType]) Get(ctx context.Context, pk PrimaryKeyable) (*ItemType, error) {
rq, err := p.BuildRequest(ctx, http.MethodGet, fmt.Sprintf("%s/%d/", p.endpoint, pk.PrimaryKey()), nil, nil)
if err != nil {
return nil, err
}
rs, err := p.Do(rq)
if err != nil {
return nil, err
}
defer rs.Body.Close()
if rs.StatusCode != http.StatusOK {
return nil, ErrorFromResponse(rs)
}
var data ResponseType
err = p.DecodeResponse(rs, &data)
if err != nil {
return nil, err
}
item := data.Item()
return &item, nil
}
// EndpointLister is a generic interface for listing items from an endpoint.
type EndpointLister[ResponseType Listable[ItemType], ItemType any, OptionsType any] interface {
List(ctx context.Context, opts OptionsType) ([]ItemType, error)
}
func NewEndpointLister[ResponseType Listable[ItemType], ItemType any, OptionsType any](cbd CBD, endpoint string) EndpointLister[Listable[ItemType], ItemType, OptionsType] {
return &endpointListerImpl[ResponseType, ItemType, OptionsType]{cbd, endpoint}
}
type endpointListerImpl[ResponseType Listable[ItemType], ItemType any, OptionsType any] struct {
CBD
endpoint string
}
func (p *endpointListerImpl[ResponseType, ItemType, OptionsType]) List(ctx context.Context, opts OptionsType) ([]ItemType, error) {
rq, err := p.BuildRequest(ctx, http.MethodGet, fmt.Sprintf("%s/", p.endpoint), opts, nil)
if err != nil {
return nil, err
}
rs, err := p.Do(rq)
if err != nil {
return nil, err
}
defer rs.Body.Close()
if rs.StatusCode != http.StatusOK {
return nil, ErrorFromResponse(rs)
}
var data ResponseType
err = p.DecodeResponse(rs, &data)
if err != nil {
return nil, err
}
return data.List(), nil
}
// EndpointCreator is a generic interface for creating an item from an endpoint.
type EndpointCreator[RequestType any, ResponseType Itemable[ItemType], ItemType any] interface {
Create(ctx context.Context, arg RequestType) (*ItemType, error)
}
func NewEndpointCreator[RequestType any, ResponseType Itemable[ItemType], ItemType any](cbd CBD, endpoint string) EndpointCreator[RequestType, ResponseType, ItemType] {
return &endpointCreatorImpl[RequestType, ResponseType, ItemType]{cbd, endpoint}
}
type endpointCreatorImpl[RequestType any, ResponseType Itemable[ItemType], ItemType any] struct {
CBD
endpoint string
}
func (p *endpointCreatorImpl[RequestType, ResponseType, ItemType]) Create(ctx context.Context, arg RequestType) (*ItemType, error) {
rq, err := p.BuildRequest(ctx, http.MethodPost, fmt.Sprintf("%s/", p.endpoint), nil, arg)
if err != nil {
return nil, err
}
rs, err := p.Do(rq)
if err != nil {
return nil, err
}
defer rs.Body.Close()
if rs.StatusCode != http.StatusOK {
return nil, ErrorFromResponse(rs)
}
var data ResponseType
err = p.DecodeResponse(rs, &data)
if err != nil {
return nil, err
}
item := data.Item()
return &item, nil
}
// EndpointUpdater is a generic interface for updating an item from an endpoint.
type EndpointUpdater[ArgumentType any, ResultType Itemable[ItemType], ItemType any] interface {
Update(ctx context.Context, pk PrimaryKeyable, arg ArgumentType) (*ItemType, error)
}
func NewEndpointUpdater[RequestType any, ResponseType Itemable[ItemType], ItemType any](cbd CBD, endpoint string) EndpointUpdater[RequestType, ResponseType, ItemType] {
return &endpointUpdaterImpl[RequestType, ResponseType, ItemType]{cbd, endpoint}
}
type endpointUpdaterImpl[RequestType any, ResponseType Itemable[ItemType], ItemType any] struct {
CBD
endpoint string
}
func (p *endpointUpdaterImpl[RequestType, ResponseType, ItemType]) Update(ctx context.Context, pk PrimaryKeyable, arg RequestType) (*ItemType, error) {
rq, err := p.BuildRequest(ctx, http.MethodPatch, fmt.Sprintf("%s/%d/", p.endpoint, pk.PrimaryKey()), nil, arg)
if err != nil {
return nil, err
}
rs, err := p.Do(rq)
if err != nil {
return nil, err
}
defer rs.Body.Close()
if rs.StatusCode != http.StatusOK {
return nil, ErrorFromResponse(rs)
}
var data ResponseType
err = p.DecodeResponse(rs, &data)
if err != nil {
return nil, err
}
item := data.Item()
return &item, nil
}
// EndpointDeleter is a generic interface for deleting an item from an endpoint.
type EndpointDeleter interface {
Delete(ctx context.Context, pk PrimaryKeyable) error
}
func NewEndpointDeleter(cbd CBD, endpoint string) EndpointDeleter {
return &endpointDeleterImpl{cbd, endpoint}
}
type endpointDeleterImpl struct {
CBD
endpoint string
}
func (p *endpointDeleterImpl) Delete(ctx context.Context, pk PrimaryKeyable) error {
rq, err := p.BuildRequest(ctx, http.MethodDelete, fmt.Sprintf("%s/%d/", p.endpoint, pk.PrimaryKey()), nil, nil)
if err != nil {
return err
}
rs, err := p.Do(rq)
if err != nil {
return err
}
defer rs.Body.Close()
if rs.StatusCode != http.StatusOK {
return ErrorFromResponse(rs)
}
return nil
}