forked from labd/contentful-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp_installation.go
105 lines (83 loc) · 2.69 KB
/
app_installation.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
package contentful
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"net/url"
"strconv"
)
// AppInstallationsService service
type AppInstallationsService service
// AppInstallation model
type AppInstallation struct {
Sys *Sys `json:"sys"`
Parameters map[string]string `json:"parameters"`
}
// GetVersion returns entity version
func (appInstallation *AppInstallation) GetVersion() int {
version := 1
if appInstallation.Sys != nil {
version = appInstallation.Sys.Version
}
return version
}
// List returns an app installations collection
func (service *AppInstallationsService) List(spaceID string) *Collection {
path := fmt.Sprintf("/spaces/%s/environments/%s/app_installations", spaceID, service.c.Environment)
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 installation
func (service *AppInstallationsService) Get(spaceID, appInstallationID string) (*AppInstallation, error) {
path := fmt.Sprintf("/spaces/%s/environments/%s/app_installations/%s", spaceID, service.c.Environment, appInstallationID)
query := url.Values{}
method := "GET"
req, err := service.c.newRequest(method, path, query, nil)
if err != nil {
return &AppInstallation{}, err
}
var installation AppInstallation
if ok := service.c.do(req, &installation); ok != nil {
return nil, err
}
return &installation, err
}
// Upsert updates or creates a new app installation
func (service *AppInstallationsService) Upsert(spaceID, appInstallationID string, installation *AppInstallation) error {
bytesArray, err := json.Marshal(installation)
if err != nil {
return err
}
var path string
var method string
if appInstallationID != "" {
path = fmt.Sprintf("/spaces/%s/environments/%s/app_installations/%s", spaceID, service.c.Environment, appInstallationID)
method = "PUT"
} else {
path = fmt.Sprintf("/spaces/%s/environments/%s/app_installations", spaceID, service.c.Environment)
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(installation.GetVersion()))
return service.c.do(req, installation)
}
// Delete the app installation
func (service *AppInstallationsService) Delete(spaceID, appInstallationID string) error {
path := fmt.Sprintf("/spaces/%s/environments/%s/app_installations/%s", spaceID, service.c.Environment, appInstallationID)
method := "DELETE"
req, err := service.c.newRequest(method, path, nil, nil)
if err != nil {
return err
}
return service.c.do(req, nil)
}