forked from contentful-labs/contentful-go
-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathscheduled_action.go
89 lines (71 loc) · 2.3 KB
/
scheduled_action.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
package contentful
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"strconv"
)
// ScheduledActionsService service
type ScheduledActionsService service
// ScheduledAction model
type ScheduledAction struct {
Sys *Sys `json:"sys"`
Entity Entity `json:"entity"`
Environment EnvironmentLink `json:"environment"`
ScheduledFor map[string]string `json:"scheduledFor"`
Action string `json:"action"`
}
// Entity model
type Entity struct {
Sys Sys `json:"sys"`
}
// EnvironmentLink model
type EnvironmentLink struct {
Sys Sys `json:"sys"`
}
// GetVersion returns entity version
func (scheduledAction *ScheduledAction) GetVersion() int {
version := 1
if scheduledAction.Sys != nil {
version = scheduledAction.Sys.Version
}
return version
}
// List returns scheduled actions collection
func (service *ScheduledActionsService) List(spaceID, entryID string) *Collection {
path := fmt.Sprintf("/spaces/%s/scheduled_actions?entity.sys.id=%s&environment.sys.id=%s", spaceID, entryID, 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
}
// Delete the scheduled action
func (service *ScheduledActionsService) Delete(spaceID, entryID, scheduledActionID string) error {
path := fmt.Sprintf("/spaces/%s/scheduled_actions/%s?entity.sys.id=%s&environment.sys.id=%s", spaceID, scheduledActionID, entryID, service.c.Environment)
method := "DELETE"
req, err := service.c.newRequest(method, path, nil, nil)
if err != nil {
return err
}
return service.c.do(req, nil)
}
// Create creates a new scheduled actions
func (service *ScheduledActionsService) Create(spaceID, entryID string, scheduledAction *ScheduledAction) error {
bytesArray, err := json.Marshal(scheduledAction)
if err != nil {
return err
}
path := fmt.Sprintf("/spaces/%s/scheduled_actions?entity.sys.id=%s&environment.sys.id=%s", spaceID, entryID, 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(scheduledAction.GetVersion()))
return service.c.do(req, scheduledAction)
}