From ed584d036d92cacf5ffc343134aa26798c398031 Mon Sep 17 00:00:00 2001 From: Matt Baer Date: Fri, 21 Sep 2018 01:00:59 +0200 Subject: [PATCH] Support unpinning posts --- post.go | 35 +++++++++++++++++++++++++++++++++++ post_test.go | 14 ++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/post.go b/post.go index 225bda7..4e1f11f 100644 --- a/post.go +++ b/post.go @@ -264,3 +264,38 @@ func (c *Client) PinPost(alias string, pp *PinnedPostParams) error { } return nil } + +// UnpinPost unpins a post from the given collection. +// See https://developers.write.as/docs/api/#unpin-a-post-from-a-collection +func (c *Client) UnpinPost(alias string, pp *PinnedPostParams) error { + res := &[]BatchPostResult{} + env, err := c.post(fmt.Sprintf("/collections/%s/unpin", alias), []*PinnedPostParams{pp}, res) + if err != nil { + return err + } + + var ok bool + if res, ok = env.Data.(*[]BatchPostResult); !ok { + return fmt.Errorf("Wrong data returned from API.") + } + + // Check for basic request errors on top level response + status := env.Code + if status != http.StatusOK { + if c.isNotLoggedIn(status) { + return fmt.Errorf("Not authenticated.") + } + return fmt.Errorf("Problem unpinning post: %d. %v\n", status, err) + } + + // Check the individual post result + if len(*res) == 0 || len(*res) > 1 { + return fmt.Errorf("Wrong data returned from API.") + } + if (*res)[0].Code != http.StatusOK { + return fmt.Errorf("Problem unpinning post: %d", (*res)[0].Code) + // TODO: return ErrorMessage (right now it'll be empty) + // return fmt.Errorf("Problem unpinning post: %v", res[0].ErrorMessage) + } + return nil +} diff --git a/post_test.go b/post_test.go index 11562d6..358fe3e 100644 --- a/post_test.go +++ b/post_test.go @@ -87,6 +87,20 @@ func TestPinPost(t *testing.T) { } } +func TestUnpinPost(t *testing.T) { + dwac := NewDevClient() + _, err := dwac.LogIn("demo", "demo") + if err != nil { + t.Fatalf("Unable to log in: %v", err) + } + defer dwac.LogOut() + + err = dwac.UnpinPost("tester", &PinnedPostParams{ID: "olx6uk7064heqltf"}) + if err != nil { + t.Fatalf("Unpin failed: %v", err) + } +} + func ExampleClient_CreatePost() { c := NewClient()