forked from labd/contentful-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnapshot.go
114 lines (91 loc) · 3.21 KB
/
snapshot.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
package contentful
import (
"fmt"
"net/http"
"net/url"
)
// SnapshotsService service
type SnapshotsService service
// EntrySnapshot model
type EntrySnapshot struct {
Sys *Sys `json:"sys"`
EntrySnapshotDetail EntrySnapshotDetail `json:"snapshot"`
}
// EntrySnapshotDetail model
type EntrySnapshotDetail struct {
Fields map[string]interface{} `json:"fields"`
Sys *Sys `json:"sys"`
}
// ContentTypeSnapshot model
type ContentTypeSnapshot struct {
Sys *Sys `json:"sys"`
ContentTypeSnapshotDetail ContentTypeSnapshotDetail `json:"snapshot"`
}
// ContentTypeSnapshotDetail model
type ContentTypeSnapshotDetail struct {
Name string `json:"name"`
Fields []ContentTypeFields `json:"fields"`
Sys *Sys `json:"sys"`
}
// ContentTypeFields model
type ContentTypeFields struct {
ID string `json:"id"`
Name string `json:"name"`
Required bool `json:"required"`
Localized bool `json:"localized"`
Type string `json:"type"`
}
// ListEntrySnapshots returns snapshot collection
func (service *SnapshotsService) ListEntrySnapshots(spaceID, entryID string) *Collection {
path := fmt.Sprintf("/spaces/%s/environments/%s/entries/%s/snapshots", spaceID, service.c.Environment, entryID)
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
}
// GetEntrySnapshot returns a single snapshot of an entry
func (service *SnapshotsService) GetEntrySnapshot(spaceID, entryID, snapshotID string) (*EntrySnapshot, error) {
path := fmt.Sprintf("/spaces/%s/environments/%s/entries/%s/snapshots/%s", spaceID, service.c.Environment, entryID, snapshotID)
query := url.Values{}
method := "GET"
req, err := service.c.newRequest(method, path, query, nil)
if err != nil {
return &EntrySnapshot{}, err
}
var entrySnapshot EntrySnapshot
if ok := service.c.do(req, &entrySnapshot); ok != nil {
return nil, err
}
return &entrySnapshot, err
}
// ListContentTypeSnapshots returns snapshot collection
func (service *SnapshotsService) ListContentTypeSnapshots(spaceID, contentTypeID string) *Collection {
path := fmt.Sprintf("/spaces/%s/environments/%s/content_types/%s/snapshots", spaceID, service.c.Environment, contentTypeID)
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
}
// GetContentTypeSnapshots returns a single snapshot of an entry
func (service *SnapshotsService) GetContentTypeSnapshots(spaceID, contentTypeID, snapshotID string) (*ContentTypeSnapshot, error) {
path := fmt.Sprintf("/spaces/%s/environments/%s/content_types/%s/snapshots/%s", spaceID, service.c.Environment, contentTypeID, snapshotID)
query := url.Values{}
method := "GET"
req, err := service.c.newRequest(method, path, query, nil)
if err != nil {
return &ContentTypeSnapshot{}, err
}
var contentTypeSnapshot ContentTypeSnapshot
if ok := service.c.do(req, &contentTypeSnapshot); ok != nil {
return nil, err
}
return &contentTypeSnapshot, err
}