From a853522f69a0e3740db0725597ab6a506505342f Mon Sep 17 00:00:00 2001 From: Rob Loranger Date: Mon, 27 May 2019 09:04:23 -0700 Subject: [PATCH] adds GetCollectionPost this adds a client method to retrieve a collection post along with basic tests - phabricator task T588 --- collection.go | 24 ++++++++++++++++++++++++ collection_test.go | 17 +++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/collection.go b/collection.go index 273c37e..9b4a925 100644 --- a/collection.go +++ b/collection.go @@ -113,6 +113,30 @@ func (c *Client) GetCollectionPosts(alias string) (*[]Post, error) { } } +// GetCollectionPost retrieves a post from a collection +// and any error (in user-friendly form) that occurs). See +// https://developers.write.as/docs/api/#retrieve-a-collection-post +func (c *Client) GetCollectionPost(alias, slug string) (*Post, error) { + post := Post{} + + env, err := c.get(fmt.Sprintf("/collections/%s/posts/%s", alias, slug), &post) + if err != nil { + return nil, err + } + + if _, ok := env.Data.(*Post); !ok { + return nil, fmt.Errorf("Wrong data returned from API.") + } + + if env.Code == http.StatusOK { + return &post, nil + } else if env.Code == http.StatusNotFound { + return nil, fmt.Errorf("Post %s not found in collection %s", slug, alias) + } + + return nil, fmt.Errorf("Problem getting post %s from collection %s: %d. %v\n", slug, alias, env.Code, err) +} + // GetUserCollections retrieves the authenticated user's collections. // See https://developers.write.as/docs/api/#retrieve-user-39-s-collections func (c *Client) GetUserCollections() (*[]Collection, error) { diff --git a/collection_test.go b/collection_test.go index 93e82c7..5e8e9ef 100644 --- a/collection_test.go +++ b/collection_test.go @@ -34,6 +34,23 @@ func TestGetCollectionPosts(t *testing.T) { } } +func TestGetCollectionPost(t *testing.T) { + wac := NewClient() + + res, err := wac.GetCollectionPost("blog", "extending-write-as") + if err != nil { + t.Errorf("Unexpected fetch results: %+v, err: %v\n", res, err) + } + + if res == nil { + t.Errorf("No post returned!") + } + + if len(res.Content) == 0 { + t.Errorf("Post content is empty!") + } +} + func TestGetUserCollections(t *testing.T) { wac := NewDevClient() _, err := wac.LogIn("demo", "demo")