forked from ohookins/contentful-go
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlocale.go
128 lines (98 loc) · 3.17 KB
/
locale.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
package contentful
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
"strconv"
)
// LocalesService service
type LocalesService service
// Locale model
type Locale struct {
Sys *Sys `json:"sys,omitempty"`
// Locale name
Name string `json:"name,omitempty"`
// Language code
Code string `json:"code,omitempty"`
// If no content is provided for the locale, the Delivery API will return content in a locale specified below:
FallbackCode string `json:"fallbackCode,omitempty"`
// Make the locale as default locale for your account
Default bool `json:"default,omitempty"`
// Entries with required fields can still be published if locale is empty.
Optional bool `json:"optional,omitempty"`
// Includes locale in the Delivery API response.
CDA bool `json:"contentDeliveryApi"`
// Displays locale to editors and enables it in Management API.
CMA bool `json:"contentManagementApi"`
}
// GetVersion returns entity version
func (locale *Locale) GetVersion() int {
version := 1
if locale.Sys != nil {
version = locale.Sys.Version
}
return version
}
// List returns a locales collection
func (service *LocalesService) List(ctx context.Context, spaceID string) *Collection {
path := fmt.Sprintf("/spaces/%s%s/locales", spaceID, getEnvPath(service.c))
method := http.MethodGet
req, err := service.c.newRequest(ctx, method, path, nil, nil, nil)
if err != nil {
return &Collection{}
}
col := NewCollection(&CollectionOptions{})
col.c = service.c
col.req = req
return col
}
// Get returns a single locale entity
func (service *LocalesService) Get(ctx context.Context, spaceID, localeID string) (*Locale, error) {
path := fmt.Sprintf("/spaces/%s%s/locales/%s", spaceID, getEnvPath(service.c), localeID)
method := http.MethodGet
req, err := service.c.newRequest(ctx, method, path, nil, nil, nil)
if err != nil {
return nil, err
}
var locale Locale
if err := service.c.do(req, &locale); err != nil {
return nil, err
}
return &locale, nil
}
// Delete the locale
func (service *LocalesService) Delete(ctx context.Context, spaceID string, locale *Locale) error {
path := fmt.Sprintf("/spaces/%s%s/locales/%s", spaceID, getEnvPath(service.c), locale.Sys.ID)
method := http.MethodDelete
req, err := service.c.newRequest(ctx, method, path, nil, nil, nil)
if err != nil {
return err
}
version := strconv.Itoa(locale.Sys.Version)
req.Header.Set("X-Contentful-Version", version)
return service.c.do(req, nil)
}
// Upsert updates or creates a new locale entity
func (service *LocalesService) Upsert(ctx context.Context, spaceID string, locale *Locale) error {
bytesArray, err := json.Marshal(locale)
if err != nil {
return err
}
var path string
var method string
if locale.Sys != nil && locale.Sys.CreatedAt != "" {
path = fmt.Sprintf("/spaces/%s%s/locales/%s", spaceID, getEnvPath(service.c), locale.Sys.ID)
method = "PUT"
} else {
path = fmt.Sprintf("/spaces/%s%s/locales", spaceID, getEnvPath(service.c))
method = "POST"
}
req, err := service.c.newRequest(ctx, method, path, nil, bytes.NewReader(bytesArray), nil)
if err != nil {
return err
}
req.Header.Set("X-Contentful-Version", strconv.Itoa(locale.GetVersion()))
return service.c.do(req, locale)
}