Skip to content

Commit

Permalink
Merge pull request #3 from fauzi-as/update-schedule-get-method
Browse files Browse the repository at this point in the history
Update schedule get method
  • Loading branch information
cvidmar authored Mar 13, 2024
2 parents b419ce1 + 0e061ae commit 8231f06
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 7 deletions.
10 changes: 10 additions & 0 deletions collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,16 @@ func (col *Collection) ToSpace() []*Space {
return spaces
}

// ToScheduledAction cast Items to ScheduledActions model
func (col *Collection) ToScheduledAction() []*ScheduledActions {
var scheduledActions []*ScheduledActions

byteArray, _ := json.Marshal(col.Items)
json.NewDecoder(bytes.NewReader(byteArray)).Decode(&scheduledActions)

return scheduledActions
}

// ToEntry cast Items to Entry model
func (col *Collection) ToEntry() []*Entry {
var entries []*Entry
Expand Down
16 changes: 9 additions & 7 deletions contentful.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,14 @@ type Contentful struct {
BaseURL string
Environment string

Spaces *SpacesService
APIKeys *APIKeyService
Assets *AssetsService
ContentTypes *ContentTypesService
Entries *EntriesService
Locales *LocalesService
Webhooks *WebhooksService
Spaces *SpacesService
APIKeys *APIKeyService
Assets *AssetsService
ContentTypes *ContentTypesService
Entries *EntriesService
Locales *LocalesService
Webhooks *WebhooksService
ScheduledActions *ScheduledActionsService
}

type service struct {
Expand Down Expand Up @@ -68,6 +69,7 @@ func NewCMA(token string) *Contentful {
c.Entries = &EntriesService{c: c}
c.Locales = &LocalesService{c: c}
c.Webhooks = &WebhooksService{c: c}
c.ScheduledActions = &ScheduledActionsService{c: c}

return c
}
Expand Down
53 changes: 53 additions & 0 deletions scheduled_actions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package contentful

import (
"fmt"
"net/url"
)

// SpacesService model
type ScheduledActionsService service

type ScheduledFor struct {
Datetime string `json:"datetime,omitempty"`
Timezone string `json:"timezone,omitempty"`
}

// ScheduledActions model
type ScheduledActions struct {
Sys *Sys `json:"sys,omitempty"`
Action string `json:"action,omitempty"`
ScheduledFor *ScheduledFor `json:"scheduledFor,omitempty"`
}

// Get returns a single scheduledActions entity
func (service *ScheduledActionsService) Get(spaceID string, entryID string, environmentID string) (*ScheduledActions, error) {
path := fmt.Sprintf("/spaces/%s/scheduled_actions", spaceID)

query := url.Values{}

query.Add("entity.sys.id", entryID)
query.Add("environment.sys.id", environmentID)
query.Add("sys.status[in]", "scheduled")

method := "GET"

req, err := service.c.newRequest(method, path, query, nil)
if err != nil {
return &ScheduledActions{}, err
}

col := NewCollection(&CollectionOptions{})
col.c = service.c
col.req = req

if ok := service.c.do(req, &col); ok != nil {
return &ScheduledActions{}, ok
}

for _, ct := range col.ToScheduledAction() {
fmt.Println(ct)
}

return &ScheduledActions{}, nil
}
24 changes: 24 additions & 0 deletions scheduled_actions_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package contentful

import (
"fmt"
"log"
"testing"

"github.com/stretchr/testify/assert"
)

func Test_ExampleScheduledActionsService_Get(t *testing.T) {

cma := NewCMA("cma-token")
assert.NotNil(t, cma)
assert.NotNil(t, cma.ScheduledActions)

scheduledActions, err := cma.ScheduledActions.Get("space-id", "entry-id", "env")

if err != nil {
log.Fatal(err)
}

fmt.Printf("%v", scheduledActions)
}

0 comments on commit 8231f06

Please sign in to comment.