From f962e5052b1392d77e8e2b94cca479ec046b3d2c Mon Sep 17 00:00:00 2001 From: Abhinav Gupta Date: Thu, 27 Dec 2018 23:53:03 -0800 Subject: [PATCH 1/3] collection: Add support for deletion This adds support for deleting collections. Resolves #1. --- collection.go | 24 ++++++++++++++++++++++++ collection_test.go | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+) diff --git a/collection.go b/collection.go index d63e116..8c00a54 100644 --- a/collection.go +++ b/collection.go @@ -135,3 +135,27 @@ func (c *Client) GetUserCollections() (*[]Collection, error) { } return colls, nil } + +// DeleteCollection permanently deletes a collection and makes any posts on it +// anonymous. +// +// See https://developers.write.as/docs/api/#delete-a-collection. +func (c *Client) DeleteCollection(alias string) error { + endpoint := "/collections/" + alias + env, err := c.delete(endpoint, nil /* data */) + if err != nil { + return err + } + + status := env.Code + switch status { + case http.StatusNoContent: + return nil + case http.StatusUnauthorized: + return fmt.Errorf("Not authenticated.") + case http.StatusBadRequest: + return fmt.Errorf("Bad request: %s", env.ErrorMessage) + default: + return fmt.Errorf("Problem deleting collection: %d. %s\n", status, env.ErrorMessage) + } +} diff --git a/collection_test.go b/collection_test.go index da9a7f3..93e82c7 100644 --- a/collection_test.go +++ b/collection_test.go @@ -2,7 +2,9 @@ package writeas import ( "fmt" + "strings" "testing" + "time" ) func TestGetCollection(t *testing.T) { @@ -51,6 +53,44 @@ func TestGetUserCollections(t *testing.T) { } } +func TestCreateAndDeleteCollection(t *testing.T) { + wac := NewDevClient() + _, err := wac.LogIn("demo", "demo") + if err != nil { + t.Fatalf("Unable to log in: %v", err) + } + defer wac.LogOut() + + now := time.Now().Unix() + alias := fmt.Sprintf("test-collection-%v", now) + c, 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) + } + + if err := wac.DeleteCollection(c.Alias); err != nil { + t.Fatalf("Unable to delete collection %q: %v", alias, err) + } +} + +func TestDeleteCollectionUnauthenticated(t *testing.T) { + wac := NewDevClient() + + now := time.Now().Unix() + alias := fmt.Sprintf("test-collection-does-not-exist-%v", now) + err := wac.DeleteCollection(alias) + if err == nil { + t.Fatalf("Should not be able to delete collection %q unauthenticated.", alias) + } + + if !strings.Contains(err.Error(), "Not authenticated") { + t.Fatalf("Error message should be more informative: %v", err) + } +} + func ExampleClient_GetCollection() { c := NewClient() coll, err := c.GetCollection("blog") From 1d34eede72c856b5aa9a31ed4036f42aed3111eb Mon Sep 17 00:00:00 2001 From: Abhinav Gupta Date: Fri, 28 Dec 2018 06:42:42 -0800 Subject: [PATCH 2/3] DeleteCollection: Make params a struct Rather than accepting a naked string, accept a DeleteCollectionParams struct so that new optional parameters can be added in the future without breaking the API. --- collection.go | 10 ++++++++-- collection_test.go | 6 ++++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/collection.go b/collection.go index 8c00a54..9d16e48 100644 --- a/collection.go +++ b/collection.go @@ -29,6 +29,12 @@ type ( Alias string `json:"alias"` Title string `json:"title"` } + + // DeleteCollectionParams holds the parameters required to delete a + // collection. + DeleteCollectionParams struct { + Alias string `json:"-"` + } ) // CreateCollection creates a new collection, returning a user-friendly error @@ -140,8 +146,8 @@ func (c *Client) GetUserCollections() (*[]Collection, error) { // anonymous. // // See https://developers.write.as/docs/api/#delete-a-collection. -func (c *Client) DeleteCollection(alias string) error { - endpoint := "/collections/" + alias +func (c *Client) DeleteCollection(p *DeleteCollectionParams) error { + endpoint := "/collections/" + p.Alias env, err := c.delete(endpoint, nil /* data */) if err != nil { return err diff --git a/collection_test.go b/collection_test.go index 93e82c7..905290e 100644 --- a/collection_test.go +++ b/collection_test.go @@ -71,7 +71,8 @@ func TestCreateAndDeleteCollection(t *testing.T) { t.Fatalf("Unable to create collection %q: %v", alias, err) } - if err := wac.DeleteCollection(c.Alias); err != nil { + p := &DeleteCollectionParams{Alias: c.Alias} + if err := wac.DeleteCollection(p); err != nil { t.Fatalf("Unable to delete collection %q: %v", alias, err) } } @@ -81,7 +82,8 @@ func TestDeleteCollectionUnauthenticated(t *testing.T) { now := time.Now().Unix() alias := fmt.Sprintf("test-collection-does-not-exist-%v", now) - err := wac.DeleteCollection(alias) + p := &DeleteCollectionParams{Alias: alias} + err := wac.DeleteCollection(p) if err == nil { t.Fatalf("Should not be able to delete collection %q unauthenticated.", alias) } From 7a2c93ae6d3ee91dbe7cdd9c68b3d20be43762a2 Mon Sep 17 00:00:00 2001 From: Matt Baer Date: Sun, 26 May 2019 14:08:05 -0400 Subject: [PATCH 3/3] Revert "DeleteCollection: Make params a struct" This reverts commit 1d34eede72c856b5aa9a31ed4036f42aed3111eb. --- collection.go | 10 ++-------- collection_test.go | 6 ++---- 2 files changed, 4 insertions(+), 12 deletions(-) diff --git a/collection.go b/collection.go index 9d16e48..8c00a54 100644 --- a/collection.go +++ b/collection.go @@ -29,12 +29,6 @@ type ( Alias string `json:"alias"` Title string `json:"title"` } - - // DeleteCollectionParams holds the parameters required to delete a - // collection. - DeleteCollectionParams struct { - Alias string `json:"-"` - } ) // CreateCollection creates a new collection, returning a user-friendly error @@ -146,8 +140,8 @@ func (c *Client) GetUserCollections() (*[]Collection, error) { // anonymous. // // See https://developers.write.as/docs/api/#delete-a-collection. -func (c *Client) DeleteCollection(p *DeleteCollectionParams) error { - endpoint := "/collections/" + p.Alias +func (c *Client) DeleteCollection(alias string) error { + endpoint := "/collections/" + alias env, err := c.delete(endpoint, nil /* data */) if err != nil { return err diff --git a/collection_test.go b/collection_test.go index 905290e..93e82c7 100644 --- a/collection_test.go +++ b/collection_test.go @@ -71,8 +71,7 @@ func TestCreateAndDeleteCollection(t *testing.T) { t.Fatalf("Unable to create collection %q: %v", alias, err) } - p := &DeleteCollectionParams{Alias: c.Alias} - if err := wac.DeleteCollection(p); err != nil { + if err := wac.DeleteCollection(c.Alias); err != nil { t.Fatalf("Unable to delete collection %q: %v", alias, err) } } @@ -82,8 +81,7 @@ func TestDeleteCollectionUnauthenticated(t *testing.T) { now := time.Now().Unix() alias := fmt.Sprintf("test-collection-does-not-exist-%v", now) - p := &DeleteCollectionParams{Alias: alias} - err := wac.DeleteCollection(p) + err := wac.DeleteCollection(alias) if err == nil { t.Fatalf("Should not be able to delete collection %q unauthenticated.", alias) }