contentful/collection.go
2019-05-31 11:50:27 +02:00

212 lines
5.1 KiB
Go

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 int `json:"limit"`
Items []interface{} `json:"items"`
Includes map[string]interface{} `json:"includes"`
NextSyncURL string `json:"nextSyncUrl"`
NextPageURL string `json:"nextPageUrl"`
SyncToken string
}
// NewCollection initilazies a new collection
func NewCollection(options *CollectionOptions) *Collection {
query := NewQuery()
if options.Limit > 0 {
query.Limit(options.Limit)
}
return &Collection{
Query: *query,
page: 1,
}
}
// 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++
r, _ := regexp.Compile("sync_token=([a-zA-Z0-9\\-\\_]+)")
if col.NextPageURL != "" {
syncToken := r.FindStringSubmatch(col.NextPageURL)
col.SyncToken = syncToken[1]
} else if col.NextSyncURL != "" {
syncToken := r.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
}
// ToContentType cast Items to ContentType model
func (col *Collection) ToContentType() []*ContentType {
var contentTypes []*ContentType
byteArray, _ := json.Marshal(col.Items)
json.NewDecoder(bytes.NewReader(byteArray)).Decode(&contentTypes)
return contentTypes
}
// ToSpace cast Items to Space model
func (col *Collection) ToSpace() []*Space {
var spaces []*Space
byteArray, _ := json.Marshal(col.Items)
json.NewDecoder(bytes.NewReader(byteArray)).Decode(&spaces)
return spaces
}
// ToEntry cast Items to Entry model
func (col *Collection) ToEntry() []*Entry {
var entries []*Entry
byteArray, _ := json.Marshal(col.Items)
json.NewDecoder(bytes.NewReader(byteArray)).Decode(&entries)
return entries
}
// ToLocale cast Items to Locale model
func (col *Collection) ToLocale() []*Locale {
var locales []*Locale
byteArray, _ := json.Marshal(col.Items)
json.NewDecoder(bytes.NewReader(byteArray)).Decode(&locales)
return locales
}
// ToAsset cast Items to Asset model
func (col *Collection) ToAsset() []*Asset {
var assets []*Asset
byteArray, _ := json.Marshal(col.Items)
json.NewDecoder(bytes.NewReader(byteArray)).Decode(&assets)
return assets
}
// ToAPIKey cast Items to APIKey model
func (col *Collection) ToAPIKey() []*APIKey {
var apiKeys []*APIKey
byteArray, _ := json.Marshal(col.Items)
json.NewDecoder(bytes.NewReader(byteArray)).Decode(&apiKeys)
return apiKeys
}
// ToWebhook cast Items to Webhook model
func (col *Collection) ToWebhook() []*Webhook {
var webhooks []*Webhook
byteArray, _ := json.Marshal(col.Items)
json.NewDecoder(bytes.NewReader(byteArray)).Decode(&webhooks)
return webhooks
}
// ToIncludesEntry cast includesEntry to Entry model
func (col *Collection) ToIncludesEntry() []*Entry {
var includesEntry []*Entry
byteArray, _ := json.Marshal(col.Includes["Entry"])
json.NewDecoder(bytes.NewReader(byteArray)).Decode(&includesEntry)
return includesEntry
}
// ToIncludesEntryMap returns a map of Entry's from the Includes
func (col *Collection) ToIncludesEntryMap() map[string]*Entry {
var includesEntry []*Entry
includesEntryMap := make(map[string]*Entry)
byteArray, _ := json.Marshal(col.Includes["Entry"])
json.NewDecoder(bytes.NewReader(byteArray)).Decode(&includesEntry)
for _, e := range includesEntry {
includesEntryMap[e.Sys.ID] = e
}
return includesEntryMap
}
// ToIncludesAsset cast includesAsset to Asset model
func (col *Collection) ToIncludesAsset() []*IncludeAsset {
var includesAsset []*IncludeAsset
byteArray, _ := json.Marshal(col.Includes["Asset"])
json.NewDecoder(bytes.NewReader(byteArray)).Decode(&includesAsset)
return includesAsset
}
// ToIncludesAssetMap returns a map of Asset's from the Includes
func (col *Collection) ToIncludesAssetMap() map[string]*IncludeAsset {
var includesAsset []*IncludeAsset
includesAssetMap := make(map[string]*IncludeAsset)
byteArray, _ := json.Marshal(col.Includes["Asset"])
json.NewDecoder(bytes.NewReader(byteArray)).Decode(&includesAsset)
for _, a := range includesAsset {
includesAssetMap[a.Sys.ID] = a
}
return includesAssetMap
}