forked from ohookins/contentful-go
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcollection.go
314 lines (265 loc) · 7.56 KB
/
collection.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
package contentful
import (
"bytes"
"encoding/json"
"net/http"
"regexp"
)
// CollectionOptions holds init options
type CollectionOptions struct {
Limit uint16
}
// Includes model
type Includes struct {
Entry map[string]interface{} `json:"Entry"`
Asset map[string]interface{} `json:"Asset"`
}
// Collection model
type Collection struct {
Query
c *Contentful
req *http.Request
page uint16
Sys *Sys `json:"sys"`
Total int `json:"total"`
Skip int `json:"skip"`
Limit uint16 `json:"limit"`
Items []interface{} `json:"items"`
Includes map[string]interface{} `json:"includes"`
NextSyncURL string `json:"nextSyncUrl"`
NextPageURL string `json:"nextPageUrl"`
SyncToken string `json:"syncToken"`
// Errors which occur in the contentful structure. They are not checked in
// this source code. Please do it yourself as you might still want to parse
// the result despite the error.
Errors []Error `json:"errors"`
// Details might also get set in case of errors.
Details *ErrorDetails `json:"details"`
}
var syncTokenRegex = regexp.MustCompile(`sync_token=([a-zA-Z0-9_\-]+)`)
// NewCollection initializes a new collection
func NewCollection(options *CollectionOptions) *Collection {
query := NewQuery()
limit := uint16(100)
if options.Limit > 0 {
limit = options.Limit
}
query.Limit(limit)
return &Collection{
Query: *query,
page: 1,
Limit: limit,
}
}
// Next makes the col.req
func (col *Collection) Next() (*Collection, error) {
// setup query params
if col.SyncToken != "" {
col.Query = *NewQuery()
col.Query.SyncToken(col.SyncToken)
} else {
skip := col.Query.limit * (col.page - 1)
col.Query.Skip(skip)
}
// override request query
col.req.URL.RawQuery = col.Query.String()
// makes api call
err := col.c.do(col.req, col)
if err != nil {
return nil, err
}
col.page++
if col.NextPageURL != "" {
syncToken := syncTokenRegex.FindStringSubmatch(col.NextPageURL)
col.SyncToken = syncToken[1]
} else if col.NextSyncURL != "" {
syncToken := syncTokenRegex.FindStringSubmatch(col.NextSyncURL)
col.SyncToken = syncToken[1]
}
return col, nil
}
// Get makes the col.req with no automatic pagination
func (col *Collection) Get() (*Collection, error) {
// override request query
col.req.URL.RawQuery = col.Query.String()
// makes api call
err := col.c.do(col.req, col)
if err != nil {
return nil, err
}
return col, nil
}
// GetAll paginates and returns all items - beware of memory usage!
func (col *Collection) GetAll() (*Collection, error) {
var allItems []interface{}
col.Query.Limit(col.Limit)
for {
var errNext error
col, errNext = col.Next()
if errNext != nil {
return nil, errNext
}
allItems = append(allItems, col.Items...)
if len(col.Items) < int(col.Limit) {
break
}
}
col.Items = allItems
return col, nil
}
// ToContentType cast Items to ContentType model
func (col *Collection) ToContentType() ([]*ContentType, error) {
var contentTypes []*ContentType
byteArray, err := json.Marshal(col.Items)
if err != nil {
return nil, err
}
if err := json.NewDecoder(bytes.NewReader(byteArray)).Decode(&contentTypes); err != nil {
return nil, err
}
return contentTypes, nil
}
// ToSpace cast Items to Space model
func (col *Collection) ToSpace() ([]*Space, error) {
var spaces []*Space
byteArray, err := json.Marshal(col.Items)
if err != nil {
return nil, err
}
if err := json.NewDecoder(bytes.NewReader(byteArray)).Decode(&spaces); err != nil {
return nil, err
}
return spaces, nil
}
// ToEntry cast Items to Entry model
func (col *Collection) ToEntry() ([]*Entry, error) {
var entries []*Entry
byteArray, err := json.Marshal(col.Items)
if err != nil {
return nil, err
}
if err := json.NewDecoder(bytes.NewReader(byteArray)).Decode(&entries); err != nil {
return nil, err
}
return entries, nil
}
// ToLocale cast Items to Locale model
func (col *Collection) ToLocale() ([]*Locale, error) {
var locales []*Locale
byteArray, err := json.Marshal(col.Items)
if err != nil {
return nil, err
}
if err := json.NewDecoder(bytes.NewReader(byteArray)).Decode(&locales); err != nil {
return nil, err
}
return locales, nil
}
// ToAsset cast Items to Asset model
func (col *Collection) ToAsset() ([]*Asset, error) {
var assets []*Asset
byteArray, err := json.Marshal(col.Items)
if err != nil {
return nil, err
}
if err := json.NewDecoder(bytes.NewReader(byteArray)).Decode(&assets); err != nil {
return nil, err
}
return assets, nil
}
// ToAPIKey cast Items to APIKey model
func (col *Collection) ToAPIKey() ([]*APIKey, error) {
var apiKeys []*APIKey
byteArray, err := json.Marshal(col.Items)
if err != nil {
return nil, err
}
if err := json.NewDecoder(bytes.NewReader(byteArray)).Decode(&apiKeys); err != nil {
return nil, err
}
return apiKeys, nil
}
// ToWebhook cast Items to Webhook model
func (col *Collection) ToWebhook() ([]*Webhook, error) {
var webhooks []*Webhook
byteArray, err := json.Marshal(col.Items)
if err != nil {
return nil, err
}
if err := json.NewDecoder(bytes.NewReader(byteArray)).Decode(&webhooks); err != nil {
return nil, err
}
return webhooks, nil
}
// ToIncludesEntry cast includesEntry to Entry model
func (col *Collection) ToIncludesEntry() ([]*Entry, error) {
var includesEntry []*Entry
byteArray, err := json.Marshal(col.Includes["Entry"])
if err != nil {
return nil, err
}
if err := json.NewDecoder(bytes.NewReader(byteArray)).Decode(&includesEntry); err != nil {
return nil, err
}
return includesEntry, nil
}
// ToIncludesEntryMap returns a map of Entry's from the Includes
func (col *Collection) ToIncludesEntryMap() (map[string]*Entry, error) {
var includesEntry []*Entry
includesEntryMap := make(map[string]*Entry)
byteArray, err := json.Marshal(col.Includes["Entry"])
if err != nil {
return nil, err
}
if err := json.NewDecoder(bytes.NewReader(byteArray)).Decode(&includesEntry); err != nil {
return nil, err
}
for _, e := range includesEntry {
includesEntryMap[e.Sys.ID] = e
}
return includesEntryMap, nil
}
// ToIncludesAsset cast includesAsset to Asset model
func (col *Collection) ToIncludesAsset() ([]*IncludeAsset, error) {
var includesAsset []*IncludeAsset
byteArray, err := json.Marshal(col.Includes["Asset"])
if err != nil {
return nil, err
}
if err := json.NewDecoder(bytes.NewReader(byteArray)).Decode(&includesAsset); err != nil {
return nil, err
}
return includesAsset, nil
}
// ToIncludesAssetMap returns a map of Asset's from the Includes
func (col *Collection) ToIncludesAssetMap() (map[string]*IncludeAsset, error) {
var includesAsset []*IncludeAsset
includesAssetMap := make(map[string]*IncludeAsset)
byteArray, err := json.Marshal(col.Includes["Asset"])
if err != nil {
return nil, err
}
if err := json.NewDecoder(bytes.NewReader(byteArray)).Decode(&includesAsset); err != nil {
return nil, err
}
for _, a := range includesAsset {
includesAssetMap[a.Sys.ID] = a
}
return includesAssetMap, nil
}
// ToIncludesLocalizedAssetMap returns a map of Asset's from the Includes
func (col *Collection) ToIncludesLocalizedAssetMap() (map[string]*Asset, error) {
var includesAsset []*Asset
includesAssetMap := make(map[string]*Asset)
byteArray, err := json.Marshal(col.Includes["Asset"])
if err != nil {
return nil, err
}
if err := json.NewDecoder(bytes.NewReader(byteArray)).Decode(&includesAsset); err != nil {
return nil, err
}
for _, a := range includesAsset {
includesAssetMap[a.Sys.ID] = a
}
return includesAssetMap, nil
}