feat: tags support

This commit is contained in:
Cristian Vidmar
2024-10-10 16:13:08 +02:00
parent 003b59ec81
commit 0a6aedece9
6 changed files with 71 additions and 7 deletions

View File

@@ -50,8 +50,9 @@ type FileFieldsNoLocale struct {
// Asset model
type Asset struct {
Sys *Sys `json:"sys"`
Fields *FileFields `json:"fields"`
Metadata *Metadata `json:"metadata,omitempty"`
Sys *Sys `json:"sys"`
Fields *FileFields `json:"fields"`
}
// AssetNoLocale model

View File

@@ -32,6 +32,7 @@ type Contentful struct {
ContentTypes *ContentTypesService
Entries *EntriesService
Locales *LocalesService
Tags *TagsService
Webhooks *WebhooksService
}
@@ -66,6 +67,7 @@ func NewCMA(token string) *Contentful {
c.Assets = &AssetsService{c: c}
c.ContentTypes = &ContentTypesService{c: c}
c.Entries = &EntriesService{c: c}
c.Tags = &TagsService{c: c}
c.Locales = &LocalesService{c: c}
c.Webhooks = &WebhooksService{c: c}
@@ -92,6 +94,7 @@ func NewCDA(token string) *Contentful {
c.Assets = &AssetsService{c: c}
c.ContentTypes = &ContentTypesService{c: c}
c.Entries = &EntriesService{c: c}
c.Tags = &TagsService{c: c}
c.Locales = &LocalesService{c: c}
c.Webhooks = &WebhooksService{c: c}
@@ -116,6 +119,7 @@ func NewCPA(token string) *Contentful {
c.Assets = &AssetsService{c: c}
c.ContentTypes = &ContentTypesService{c: c}
c.Entries = &EntriesService{c: c}
c.Tags = &TagsService{c: c}
c.Locales = &LocalesService{c: c}
c.Webhooks = &WebhooksService{c: c}

View File

@@ -15,8 +15,9 @@ type EntriesService service
// Entry model
type Entry struct {
Sys *Sys `json:"sys"`
Fields map[string]interface{} `json:"fields,omitempty"`
Metadata *Metadata `json:"metadata,omitempty"`
Sys *Sys `json:"sys"`
Fields map[string]interface{} `json:"fields,omitempty"`
}
// GetVersion returns entity version

5
metadata.go Normal file
View File

@@ -0,0 +1,5 @@
package contentful
type Metadata struct {
Tags []Tag `json:"tags"`
}

View File

@@ -255,9 +255,6 @@ func (q *Query) Values() url.Values {
}
params.Set("include", strconv.Itoa(int(q.include)))
}
if q.include == 0 {
params.Set("include", "0")
}
if q.contentType != "" {
params.Set("content_type", q.contentType)
}

56
tag.go Normal file
View File

@@ -0,0 +1,56 @@
package contentful
import (
"context"
"fmt"
"net/http"
"net/url"
)
// TagsService servıce
type TagsService service
// Tag model
type Tag struct {
Sys *Sys `json:"sys"`
Name string `json:"name,omitempty"`
}
// List returns tags collection
func (service *TagsService) List(ctx context.Context, spaceID string) *Collection {
path := fmt.Sprintf("/spaces/%s%s/tags", spaceID, getEnvPath(service.c))
method := http.MethodGet
req, err := service.c.newRequest(ctx, method, path, nil, nil)
if err != nil {
return &Collection{}
}
col := NewCollection(&CollectionOptions{})
col.c = service.c
col.req = req
return col
}
// Get returns a single entry
func (service *TagsService) Get(ctx context.Context, spaceID, tagID string, locale ...string) (*Tag, error) {
path := fmt.Sprintf("/spaces/%s%s/entries/%s", spaceID, getEnvPath(service.c), tagID)
query := url.Values{}
if len(locale) > 0 {
query["locale"] = locale
}
method := http.MethodGet
req, err := service.c.newRequest(ctx, method, path, query, nil)
if err != nil {
return &Tag{}, err
}
var tag Tag
if ok := service.c.do(req, &tag); ok != nil {
return nil, err
}
return &tag, err
}