diff --git a/collection.go b/collection.go index 9d16e48..f928ccc 100644 --- a/collection.go +++ b/collection.go @@ -35,6 +35,35 @@ type ( DeleteCollectionParams struct { Alias string `json:"-"` } + + // CollectPostParams holds the parameters for moving posts to a collection. + CollectPostParams struct { + // Alias of the collection. + Alias string `json:"-"` + + // Posts to move to this collection. + Posts []*CollectPost + } + + // CollectPost is a post being moved to a collection. + CollectPost struct { + // ID of the post. + ID string `json:"id,omitempty"` + + // The post's modify token. + // + // This is required if the post does not belong to the user making + // this request. + Token string `json:"token,omitempty"` + } + + // CollectPostResult holds the result of moving a single post to a + // collection. + CollectPostResult struct { + Code int `json:"code,omitempty"` + ErrorMessage string `json:"error_msg,omitempty"` + Post *Post `json:"post,omitempty"` + } ) // CreateCollection creates a new collection, returning a user-friendly error @@ -165,3 +194,28 @@ func (c *Client) DeleteCollection(p *DeleteCollectionParams) error { return fmt.Errorf("Problem deleting collection: %d. %s\n", status, env.ErrorMessage) } } + +// CollectPosts adds a group of posts to a collection. +// +// See https://developers.write.as/docs/api/#move-a-post-to-a-collection. +func (c *Client) CollectPosts(sp *CollectPostParams) ([]*CollectPostResult, error) { + endpoint := "/collections/" + sp.Alias + "/collect" + + var p []*CollectPostResult + env, err := c.post(endpoint, sp.Posts, &p) + if err != nil { + return nil, err + } + + status := env.Code + switch { + case status == http.StatusOK: + return p, nil + case c.isNotLoggedIn(status): + return nil, fmt.Errorf("Not authenticated.") + case status == http.StatusBadRequest: + return nil, fmt.Errorf("Bad request: %s", env.ErrorMessage) + default: + return nil, fmt.Errorf("Problem claiming post: %d. %s\n", status, env.ErrorMessage) + } +} diff --git a/collection_test.go b/collection_test.go index 905290e..77a32cb 100644 --- a/collection_test.go +++ b/collection_test.go @@ -2,6 +2,7 @@ package writeas import ( "fmt" + "net/http" "strings" "testing" "time" @@ -103,3 +104,59 @@ func ExampleClient_GetCollection() { fmt.Printf("%s", coll.Title) // Output: write.as } + +func TestCollectPostsAnonymous(t *testing.T) { + // Create a post anonymously. + wac := NewDevClient() + p, err := wac.CreatePost(&PostParams{ + Title: "Title!", + Content: "This is a post.", + Font: "sans", + }) + if err != nil { + t.Errorf("Post create failed: %v", err) + return + } + t.Logf("Post created: %+v", p) + + // Log in. + if _, err := wac.LogIn("demo", "demo"); err != nil { + t.Fatalf("Unable to log in: %v", err) + } + defer wac.LogOut() + + now := time.Now().Unix() + alias := fmt.Sprintf("test-collection-%v", now) + + // Create a collection. + _, err = wac.CreateCollection(&CollectionParams{ + Alias: alias, + Title: fmt.Sprintf("Test Collection %v", now), + }) + if err != nil { + t.Fatalf("Unable to create collection %q: %v", alias, err) + } + defer wac.DeleteCollection(&DeleteCollectionParams{Alias: alias}) + + // Move the anonymous post to this collection. + res, err := wac.CollectPosts(&CollectPostParams{ + Alias: alias, + Posts: []*CollectPost{ + { + ID: p.ID, + Token: p.Token, + }, + }, + }) + if err != nil { + t.Fatalf("Could not collect post %q: %v", p.ID, err) + } + + for _, cr := range res { + if cr.Code != http.StatusOK { + t.Errorf("Failed to move post: %v", cr.ErrorMessage) + } else { + t.Logf("Moved post %q", cr.Post.ID) + } + } +}