Skip to content

Commit 1728e2a

Browse files
authored
Merge pull request #9 from foomo/feature/tags
tags support
2 parents 003b59e + 0a6aede commit 1728e2a

File tree

6 files changed

+71
-7
lines changed

6 files changed

+71
-7
lines changed

asset.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,9 @@ type FileFieldsNoLocale struct {
5050

5151
// Asset model
5252
type Asset struct {
53-
Sys *Sys `json:"sys"`
54-
Fields *FileFields `json:"fields"`
53+
Metadata *Metadata `json:"metadata,omitempty"`
54+
Sys *Sys `json:"sys"`
55+
Fields *FileFields `json:"fields"`
5556
}
5657

5758
// AssetNoLocale model

contentful.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ type Contentful struct {
3232
ContentTypes *ContentTypesService
3333
Entries *EntriesService
3434
Locales *LocalesService
35+
Tags *TagsService
3536
Webhooks *WebhooksService
3637
}
3738

@@ -66,6 +67,7 @@ func NewCMA(token string) *Contentful {
6667
c.Assets = &AssetsService{c: c}
6768
c.ContentTypes = &ContentTypesService{c: c}
6869
c.Entries = &EntriesService{c: c}
70+
c.Tags = &TagsService{c: c}
6971
c.Locales = &LocalesService{c: c}
7072
c.Webhooks = &WebhooksService{c: c}
7173

@@ -92,6 +94,7 @@ func NewCDA(token string) *Contentful {
9294
c.Assets = &AssetsService{c: c}
9395
c.ContentTypes = &ContentTypesService{c: c}
9496
c.Entries = &EntriesService{c: c}
97+
c.Tags = &TagsService{c: c}
9598
c.Locales = &LocalesService{c: c}
9699
c.Webhooks = &WebhooksService{c: c}
97100

@@ -116,6 +119,7 @@ func NewCPA(token string) *Contentful {
116119
c.Assets = &AssetsService{c: c}
117120
c.ContentTypes = &ContentTypesService{c: c}
118121
c.Entries = &EntriesService{c: c}
122+
c.Tags = &TagsService{c: c}
119123
c.Locales = &LocalesService{c: c}
120124
c.Webhooks = &WebhooksService{c: c}
121125

entry.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ type EntriesService service
1515

1616
// Entry model
1717
type Entry struct {
18-
Sys *Sys `json:"sys"`
19-
Fields map[string]interface{} `json:"fields,omitempty"`
18+
Metadata *Metadata `json:"metadata,omitempty"`
19+
Sys *Sys `json:"sys"`
20+
Fields map[string]interface{} `json:"fields,omitempty"`
2021
}
2122

2223
// GetVersion returns entity version

metadata.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package contentful
2+
3+
type Metadata struct {
4+
Tags []Tag `json:"tags"`
5+
}

query.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -255,9 +255,6 @@ func (q *Query) Values() url.Values {
255255
}
256256
params.Set("include", strconv.Itoa(int(q.include)))
257257
}
258-
if q.include == 0 {
259-
params.Set("include", "0")
260-
}
261258
if q.contentType != "" {
262259
params.Set("content_type", q.contentType)
263260
}

tag.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package contentful
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"net/http"
7+
"net/url"
8+
)
9+
10+
// TagsService servıce
11+
type TagsService service
12+
13+
// Tag model
14+
type Tag struct {
15+
Sys *Sys `json:"sys"`
16+
Name string `json:"name,omitempty"`
17+
}
18+
19+
// List returns tags collection
20+
func (service *TagsService) List(ctx context.Context, spaceID string) *Collection {
21+
path := fmt.Sprintf("/spaces/%s%s/tags", spaceID, getEnvPath(service.c))
22+
method := http.MethodGet
23+
24+
req, err := service.c.newRequest(ctx, method, path, nil, nil)
25+
if err != nil {
26+
return &Collection{}
27+
}
28+
29+
col := NewCollection(&CollectionOptions{})
30+
col.c = service.c
31+
col.req = req
32+
33+
return col
34+
}
35+
36+
// Get returns a single entry
37+
func (service *TagsService) Get(ctx context.Context, spaceID, tagID string, locale ...string) (*Tag, error) {
38+
path := fmt.Sprintf("/spaces/%s%s/entries/%s", spaceID, getEnvPath(service.c), tagID)
39+
query := url.Values{}
40+
if len(locale) > 0 {
41+
query["locale"] = locale
42+
}
43+
method := http.MethodGet
44+
45+
req, err := service.c.newRequest(ctx, method, path, query, nil)
46+
if err != nil {
47+
return &Tag{}, err
48+
}
49+
50+
var tag Tag
51+
if ok := service.c.do(req, &tag); ok != nil {
52+
return nil, err
53+
}
54+
55+
return &tag, err
56+
}

0 commit comments

Comments
 (0)