Skip to content

Commit

Permalink
Merge pull request #9 from foomo/feature/tags
Browse files Browse the repository at this point in the history
tags support
  • Loading branch information
cvidmar authored Oct 11, 2024
2 parents 003b59e + 0a6aede commit 1728e2a
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 7 deletions.
5 changes: 3 additions & 2 deletions asset.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions contentful.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type Contentful struct {
ContentTypes *ContentTypesService
Entries *EntriesService
Locales *LocalesService
Tags *TagsService
Webhooks *WebhooksService
}

Expand Down Expand Up @@ -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}

Expand All @@ -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}

Expand All @@ -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}

Expand Down
5 changes: 3 additions & 2 deletions entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions metadata.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package contentful

type Metadata struct {
Tags []Tag `json:"tags"`
}
3 changes: 0 additions & 3 deletions query.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
56 changes: 56 additions & 0 deletions tag.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 1728e2a

Please sign in to comment.