Browse Source

Support fetching collection posts

This closes #3
pull/6/head
Matt Baer 6 years ago
parent
commit
f4196014b5
2 changed files with 41 additions and 0 deletions
  1. +28
    -0
      collection.go
  2. +13
    -0
      collection_test.go

+ 28
- 0
collection.go View File

@@ -19,6 +19,8 @@ type (
Email string `json:"email,omitempty"`

TotalPosts int `json:"total_posts"`

Posts *[]Post `json:"posts,omitempty"`
}

// CollectionParams holds values for creating a collection.
@@ -83,3 +85,29 @@ func (c *Client) CreateCollection(sp *CollectionParams) (*Collection, error) {
}
return nil, fmt.Errorf("Problem getting post: %s. %v\n", status, err)
}

// GetCollectionPosts retrieves a collection's posts, returning the Posts
// and any error (in user-friendly form) that occurs. See
// https://developer.write.as/docs/api/#retrieve-collection-posts
func (c *Client) GetCollectionPosts(alias string) (*[]Post, error) {
coll := &Collection{}
env, err := c.get(fmt.Sprintf("/collections/%s/posts", alias), coll)
if err != nil {
return nil, err
}

var ok bool
if coll, ok = env.Data.(*Collection); !ok {
return nil, fmt.Errorf("Wrong data returned from API.")
}
status := env.Code

if status == http.StatusOK {
return coll.Posts, nil
} else if status == http.StatusNotFound {
return nil, fmt.Errorf("Collection not found.")
} else {
return nil, fmt.Errorf("Problem getting collection: %s. %v\n", status, err)
}
return coll.Posts, nil
}

+ 13
- 0
collection_test.go View File

@@ -19,6 +19,19 @@ func TestGetCollection(t *testing.T) {
}
}

func TestGetCollectionPosts(t *testing.T) {
wac := NewClient()

res, err := wac.GetCollectionPosts("blog")
if err != nil {
t.Errorf("Unexpected fetch results: %+v, err: %v\n", res, err)
} else {
if len(*res) == 0 {
t.Errorf("No posts returned!")
}
}
}

func ExampleClient_GetCollection() {
c := NewClient()
coll, err := c.GetCollection("blog")


Loading…
Cancel
Save