-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathitems.go
268 lines (241 loc) · 7.38 KB
/
items.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
package directus
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/url"
)
// ItemsClient access the items API in a type-safe way.
type ItemsClient[T any] struct {
c *Client
collection string
opts []ReadOption
}
// ReadOption configures the returned data from Directus when reading or returning items.
type ReadOption func(req *http.Request)
// WithFields filters the fields of each returned item. It can add relations deep fields to the response to obtain them
// in the same request.
func WithFields(fields ...string) ReadOption {
return func(req *http.Request) {
q := req.URL.Query()
for _, field := range fields {
q.Add("fields[]", field)
}
req.URL.RawQuery = q.Encode()
}
}
// WithSort sorts the returned items by the given fields. Use a minus sign (-) to sort in descending order.
// It does not order deep relations inside each of the items. To sort deep relations, use WithDeepSort.
func WithSort(sort ...string) ReadOption {
return func(req *http.Request) {
q := req.URL.Query()
for _, s := range sort {
q.Add("sort[]", s)
}
req.URL.RawQuery = q.Encode()
}
}
// WithLimit limits the number of returned items.
func WithLimit(limit int64) ReadOption {
return func(req *http.Request) {
q := req.URL.Query()
q.Set("limit", fmt.Sprintf("%d", limit))
req.URL.RawQuery = q.Encode()
}
}
// WithOffset skips the first n items.
func WithOffset(offset int64) ReadOption {
return func(req *http.Request) {
q := req.URL.Query()
q.Add("offset", fmt.Sprintf("%d", offset))
req.URL.RawQuery = q.Encode()
}
}
// WithDeepSort sorts the deep relations of each returned item by the given fields. Use a minus sign (-) to sort in
// descending order. It does not order the items themselves. To sort the items, use WithSort.
func WithDeepSort(field string, sort ...string) ReadOption {
return func(req *http.Request) {
q := req.URL.Query()
for _, s := range sort {
q.Add(fmt.Sprintf("deep[%s][_sort][]", field), s)
}
req.URL.RawQuery = q.Encode()
}
}
// WithDeepLimit limits the number of returned deep relations of each item.
func WithDeepLimit(field string, limit int64) ReadOption {
return func(req *http.Request) {
q := req.URL.Query()
q.Add(fmt.Sprintf("deep[%s][_limit]", field), fmt.Sprintf("%d", limit))
req.URL.RawQuery = q.Encode()
}
}
// NewItemsClient creates a new client to access & write items in a type-safe way.
func NewItemsClient[T any](client *Client, collection string, opts ...ReadOption) *ItemsClient[T] {
return &ItemsClient[T]{
c: client,
collection: collection,
opts: opts,
}
}
type doOption func(req *http.Request)
func (items *ItemsClient[T]) itemsdo(ctx context.Context, method, url string, request, reply any, opts ...doOption) error {
var body io.Reader
if request != nil {
var buf bytes.Buffer
if err := json.NewEncoder(&buf).Encode(request); err != nil {
return fmt.Errorf("directus: cannot encode request: %v", err)
}
body = &buf
}
req, err := http.NewRequestWithContext(ctx, method, url, body)
if err != nil {
return fmt.Errorf("directus: cannot prepare request: %v", err)
}
for _, opt := range items.opts {
opt(req)
}
for _, opt := range opts {
opt(req)
}
return items.c.sendRequest(req, &reply)
}
// List items of the collection.
func (items *ItemsClient[T]) List(ctx context.Context, opts ...ReadOption) ([]*T, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, items.c.urlf("/items/%s", items.collection), nil)
if err != nil {
return nil, fmt.Errorf("directus: cannot prepare request: %v", err)
}
// Return all items of the collection by default.
q := req.URL.Query()
q.Set("limit", "-1")
req.URL.RawQuery = q.Encode()
for _, opt := range items.opts {
opt(req)
}
for _, opt := range opts {
opt(req)
}
reply := struct {
Data []*T `json:"data"`
}{}
if err := items.c.sendRequest(req, &reply); err != nil {
return nil, err
}
return reply.Data, nil
}
// Filter items of the collection.
func (items *ItemsClient[T]) Filter(ctx context.Context, filter Filter, opts ...ReadOption) ([]*T, error) {
u, err := url.Parse(items.c.urlf("/items/%s", items.collection))
if err != nil {
return nil, err
}
qs := u.Query()
f, err := FilterJSON(filter)
if err != nil {
return nil, err
}
qs.Set("filter", f)
u.RawQuery = qs.Encode()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, u.String(), nil)
if err != nil {
return nil, fmt.Errorf("directus: cannot prepare request: %v", err)
}
for _, opt := range items.opts {
opt(req)
}
for _, opt := range opts {
opt(req)
}
reply := struct {
Data []*T `json:"data"`
}{}
if err := items.c.sendRequest(req, &reply); err != nil {
return nil, err
}
return reply.Data, nil
}
// Get a single item by its primary key. If it cannot be found, it returns ErrItemNotFound.
func (items *ItemsClient[T]) Get(ctx context.Context, id string, opts ...ReadOption) (*T, error) {
if id == "" {
return nil, fmt.Errorf("%w: %v", ErrItemNotFound, id)
}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, items.c.urlf("/items/%s/%s", items.collection, id), nil)
if err != nil {
return nil, fmt.Errorf("directus: cannot prepare request: %v", err)
}
for _, opt := range items.opts {
opt(req)
}
for _, opt := range opts {
opt(req)
}
reply := struct {
Data *T `json:"data"`
}{}
if err := items.c.sendRequest(req, &reply); err != nil {
var e *unexpectedStatusError
if errors.As(err, &e) && e.status == http.StatusForbidden {
return nil, fmt.Errorf("%w: %v", ErrItemNotFound, id)
}
return nil, err
}
return reply.Data, nil
}
// Create a new item in the collection.
func (items *ItemsClient[T]) Create(ctx context.Context, item *T) (*T, error) {
reply := struct {
Data *T `json:"data"`
}{}
if err := items.itemsdo(ctx, http.MethodPost, items.c.urlf("/items/%s", items.collection), item, &reply); err != nil {
return nil, err
}
return reply.Data, nil
}
// Update an item in the collection by its primary key.
func (items *ItemsClient[T]) Update(ctx context.Context, id string, item *T) (*T, error) {
reply := struct {
Data *T `json:"data"`
}{}
if err := items.itemsdo(ctx, http.MethodPatch, items.c.urlf("/items/%s/%s", items.collection, id), item, &reply); err != nil {
if errors.Is(err, ErrItemNotFound) {
return nil, fmt.Errorf("%w: %v", err, id)
}
return nil, err
}
return reply.Data, nil
}
// Delete an item from the collection by its primary key.
func (items *ItemsClient[T]) Delete(ctx context.Context, id string) error {
return items.itemsdo(ctx, http.MethodDelete, items.c.urlf("/items/%s/%s", items.collection, id), nil, nil)
}
type SingletonClient[T any] struct {
items *ItemsClient[T]
}
func NewSingletonClient[T any](client *Client, collection string, opts ...ReadOption) *SingletonClient[T] {
return &SingletonClient[T]{
items: NewItemsClient[T](client, collection, opts...),
}
}
func (s *SingletonClient[T]) Get(ctx context.Context) (*T, error) {
reply := struct {
Data *T `json:"data"`
}{}
if err := s.items.itemsdo(ctx, http.MethodGet, s.items.c.urlf("/items/%s", s.items.collection), nil, &reply); err != nil {
return nil, err
}
return reply.Data, nil
}
func (s *SingletonClient[T]) Update(ctx context.Context, item *T) (*T, error) {
reply := struct {
Data *T `json:"data"`
}{}
if err := s.items.itemsdo(ctx, http.MethodPatch, s.items.c.urlf("/items/%s", s.items.collection), item, &reply); err != nil {
return nil, err
}
return reply.Data, nil
}