Browse Source

Merge pull request #13 from abhinav/delete-collection

collection: Add support for deletion
pull/17/head
Matt Baer 4 years ago
committed by GitHub
parent
commit
801952e0b5
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 64 additions and 0 deletions
  1. +24
    -0
      collection.go
  2. +40
    -0
      collection_test.go

+ 24
- 0
collection.go View File

@@ -136,3 +136,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)
}
}

+ 40
- 0
collection_test.go View File

@@ -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")


Loading…
Cancel
Save