forked from contentful-labs/contentful-go
-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathapp_definition.go
112 lines (89 loc) · 2.66 KB
/
app_definition.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
package contentful
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"net/url"
"strconv"
)
// AppDefinitionsService service
type AppDefinitionsService service
// AppDefinition model
type AppDefinition struct {
Sys *Sys `json:"sys"`
Name string `json:"name"`
SRC string `json:"src"`
Locations []Locations `json:"locations"`
}
// Locations model
type Locations struct {
Location string `json:"location"`
}
// GetVersion returns entity version
func (appDefinition *AppDefinition) GetVersion() int {
version := 1
if appDefinition.Sys != nil {
version = appDefinition.Sys.Version
}
return version
}
// List returns an app definitions collection
func (service *AppDefinitionsService) List(organizationID string) *Collection {
path := fmt.Sprintf("/organizations/%s/app_definitions", organizationID)
req, err := service.c.newRequest(http.MethodGet, path, nil, nil)
if err != nil {
return &Collection{}
}
col := NewCollection(&CollectionOptions{})
col.c = service.c
col.req = req
return col
}
// Get returns a single app definition
func (service *AppDefinitionsService) Get(organizationID, appDefinitionID string) (*AppDefinition, error) {
path := fmt.Sprintf("/organizations/%s/app_definitions/%s", organizationID, appDefinitionID)
query := url.Values{}
method := "GET"
req, err := service.c.newRequest(method, path, query, nil)
if err != nil {
return &AppDefinition{}, err
}
var definition AppDefinition
if ok := service.c.do(req, &definition); ok != nil {
return nil, err
}
return &definition, err
}
// Upsert updates or creates a new app definition
func (service *AppDefinitionsService) Upsert(organizationID string, definition *AppDefinition) error {
bytesArray, err := json.Marshal(definition)
if err != nil {
return err
}
var path string
var method string
if definition.Sys != nil && definition.Sys.ID != "" {
path = fmt.Sprintf("/organizations/%s/app_definitions/%s", organizationID, definition.Sys.ID)
method = "PUT"
} else {
path = fmt.Sprintf("/organizations/%s/app_definitions", organizationID)
method = "POST"
}
req, err := service.c.newRequest(method, path, nil, bytes.NewReader(bytesArray))
if err != nil {
return err
}
req.Header.Set("X-Contentful-Version", strconv.Itoa(definition.GetVersion()))
return service.c.do(req, definition)
}
// Delete the app definition
func (service *AppDefinitionsService) Delete(organizationID, appDefinitionID string) error {
path := fmt.Sprintf("/organizations/%s/app_definitions/%s", organizationID, appDefinitionID)
method := "DELETE"
req, err := service.c.newRequest(method, path, nil, nil)
if err != nil {
return err
}
return service.c.do(req, nil)
}