Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for context.Context #25

Open
wants to merge 2 commits into
base: v3
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions auth.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package writeas

import (
"context"
"fmt"
"net/http"
)

// LogIn authenticates a user with Write.as.
// See https://developers.write.as/docs/api/#authenticate-a-user
func (c *Client) LogIn(username, pass string) (*AuthUser, error) {
func (c *Client) LogIn(ctx context.Context, username, pass string) (*AuthUser, error) {
u := &AuthUser{}
up := struct {
Alias string `json:"alias"`
Expand All @@ -17,7 +18,7 @@ func (c *Client) LogIn(username, pass string) (*AuthUser, error) {
Pass: pass,
}

env, err := c.post("/auth/login", up, u)
env, err := c.post(ctx, "/auth/login", up, u)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -47,8 +48,8 @@ func (c *Client) LogIn(username, pass string) (*AuthUser, error) {

// LogOut logs the current user out, making the Client's current access token
// invalid.
func (c *Client) LogOut() error {
env, err := c.delete("/auth/me", nil)
func (c *Client) LogOut(ctx context.Context) error {
env, err := c.delete(ctx, "/auth/me", nil)
if err != nil {
return err
}
Expand Down
10 changes: 7 additions & 3 deletions auth_test.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
package writeas

import "testing"
import (
"context"
"testing"
)

func TestAuthentication(t *testing.T) {
dwac := NewDevClient()
ctx := context.Background()

// Log in
_, err := dwac.LogIn("demo", "demo")
_, err := dwac.LogIn(ctx, "demo", "demo")
if err != nil {
t.Fatalf("Unable to log in: %v", err)
}

// Log out
err = dwac.LogOut()
err = dwac.LogOut(ctx)
if err != nil {
t.Fatalf("Unable to log out: %v", err)
}
Expand Down
5 changes: 3 additions & 2 deletions author.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package writeas

import (
"context"
"fmt"
"net/http"
)
Expand All @@ -27,13 +28,13 @@ type (
)

// CreateContributor creates a new contributor on the given organization.
func (c *Client) CreateContributor(sp *AuthorParams) (*Author, error) {
func (c *Client) CreateContributor(ctx context.Context, sp *AuthorParams) (*Author, error) {
if sp.OrgAlias == "" {
return nil, fmt.Errorf("AuthorParams.OrgAlias is required.")
}

a := &Author{}
env, err := c.post("/organizations/"+sp.OrgAlias+"/contributors", sp, a)
env, err := c.post(ctx, "/organizations/"+sp.OrgAlias+"/contributors", sp, a)
if err != nil {
return nil, err
}
Expand Down
10 changes: 7 additions & 3 deletions author_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package writeas

import "testing"
import (
"context"
"testing"
)

func TestClient_CreateContributor(t *testing.T) {
c := NewClientWith(Config{URL: "http://localhost:7777/api"})
_, err := c.LogIn("test", "test")
ctx := context.Background()
_, err := c.LogIn(ctx, "test", "test")
if err != nil {
t.Fatalf("login: %s", err)
}
Expand All @@ -24,7 +28,7 @@ func TestClient_CreateContributor(t *testing.T) {
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
_, err = c.CreateContributor(&AuthorParams{
_, err = c.CreateContributor(ctx, &AuthorParams{
Name: test.AName,
Slug: test.ASlug,
OrgAlias: test.AOrg,
Expand Down
25 changes: 13 additions & 12 deletions collection.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package writeas

import (
"context"
"fmt"
"net/http"
)
Expand Down Expand Up @@ -35,9 +36,9 @@ type (
// CreateCollection creates a new collection, returning a user-friendly error
// if one comes up. Requires a Write.as subscription. See
// https://developers.write.as/docs/api/#create-a-collection
func (c *Client) CreateCollection(sp *CollectionParams) (*Collection, error) {
func (c *Client) CreateCollection(ctx context.Context, sp *CollectionParams) (*Collection, error) {
p := &Collection{}
env, err := c.post("/collections", sp, p)
env, err := c.post(ctx, "/collections", sp, p)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -66,9 +67,9 @@ func (c *Client) CreateCollection(sp *CollectionParams) (*Collection, error) {
// GetCollection retrieves a collection, returning the Collection and any error
// (in user-friendly form) that occurs. See
// https://developers.write.as/docs/api/#retrieve-a-collection
func (c *Client) GetCollection(alias string) (*Collection, error) {
func (c *Client) GetCollection(ctx context.Context, alias string) (*Collection, error) {
coll := &Collection{}
env, err := c.get(fmt.Sprintf("/collections/%s", alias), coll)
env, err := c.get(ctx, fmt.Sprintf("/collections/%s", alias), coll)
if err != nil {
return nil, err
}
Expand All @@ -91,13 +92,13 @@ func (c *Client) GetCollection(alias string) (*Collection, error) {
// GetCollectionPosts retrieves a collection's posts, returning the Posts
// and any error (in user-friendly form) that occurs. See
// https://developers.write.as/docs/api/#retrieve-collection-posts
func (c *Client) GetCollectionPosts(alias string, page int) (*[]Post, error) {
func (c *Client) GetCollectionPosts(ctx context.Context, alias string, page int) (*[]Post, error) {
coll := &Collection{}
q := ""
if page > 0 {
q = fmt.Sprintf("?page=%d", page)
}
env, err := c.get(fmt.Sprintf("/collections/%s/posts%s", alias, q), coll)
env, err := c.get(ctx, fmt.Sprintf("/collections/%s/posts%s", alias, q), coll)
if err != nil {
return nil, err
}
Expand All @@ -120,10 +121,10 @@ func (c *Client) GetCollectionPosts(alias string, page int) (*[]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) {
func (c *Client) GetCollectionPost(ctx context.Context, alias, slug string) (*Post, error) {
post := Post{}

env, err := c.get(fmt.Sprintf("/collections/%s/posts/%s", alias, slug), &post)
env, err := c.get(ctx, fmt.Sprintf("/collections/%s/posts/%s", alias, slug), &post)
if err != nil {
return nil, err
}
Expand All @@ -143,9 +144,9 @@ func (c *Client) GetCollectionPost(alias, slug string) (*Post, error) {

// 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) {
func (c *Client) GetUserCollections(ctx context.Context) (*[]Collection, error) {
colls := &[]Collection{}
env, err := c.get("/me/collections", colls)
env, err := c.get(ctx, "/me/collections", colls)
if err != nil {
return nil, err
}
Expand All @@ -169,9 +170,9 @@ func (c *Client) GetUserCollections() (*[]Collection, error) {
// anonymous.
//
// See https://developers.write.as/docs/api/#delete-a-collection.
func (c *Client) DeleteCollection(alias string) error {
func (c *Client) DeleteCollection(ctx context.Context, alias string) error {
endpoint := "/collections/" + alias
env, err := c.delete(endpoint, nil /* data */)
env, err := c.delete(ctx, endpoint, nil /* data */)
if err != nil {
return err
}
Expand Down
26 changes: 15 additions & 11 deletions collection_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package writeas

import (
"context"
"fmt"
"strings"
"testing"
Expand All @@ -10,7 +11,7 @@ import (
func TestGetCollection(t *testing.T) {
dwac := NewDevClient()

res, err := dwac.GetCollection("tester")
res, err := dwac.GetCollection(context.Background(), "tester")
if err != nil {
t.Errorf("Unexpected fetch results: %+v, err: %v\n", res, err)
}
Expand All @@ -22,9 +23,10 @@ func TestGetCollection(t *testing.T) {
func TestGetCollectionPosts(t *testing.T) {
dwac := NewDevClient()
posts := []Post{}
ctx := context.Background()

t.Run("Get all posts in collection", func(t *testing.T) {
res, err := dwac.GetCollectionPosts("tester")
res, err := dwac.GetCollectionPosts(ctx, "tester")
if err != nil {
t.Errorf("Unexpected fetch results: %+v, err: %v\n", res, err)
}
Expand All @@ -34,7 +36,7 @@ func TestGetCollectionPosts(t *testing.T) {
posts = *res
})
t.Run("Get one post from collection", func(t *testing.T) {
res, err := dwac.GetCollectionPost("tester", posts[0].Slug)
res, err := dwac.GetCollectionPost(ctx, "tester", posts[0].Slug)
if err != nil {
t.Errorf("Unexpected fetch results: %+v, err: %v\n", res, err)
}
Expand All @@ -51,13 +53,14 @@ func TestGetCollectionPosts(t *testing.T) {

func TestGetUserCollections(t *testing.T) {
wac := NewDevClient()
_, err := wac.LogIn("demo", "demo")
ctx := context.Background()
_, err := wac.LogIn(ctx, "demo", "demo")
if err != nil {
t.Fatalf("Unable to log in: %v", err)
}
defer wac.LogOut()
defer wac.LogOut(ctx)

res, err := wac.GetUserCollections()
res, err := wac.GetUserCollections(ctx)
if err != nil {
t.Errorf("Unexpected fetch results: %+v, err: %v\n", res, err)
} else {
Expand All @@ -70,23 +73,24 @@ func TestGetUserCollections(t *testing.T) {

func TestCreateAndDeleteCollection(t *testing.T) {
wac := NewDevClient()
_, err := wac.LogIn("demo", "demo")
ctx := context.Background()
_, err := wac.LogIn(ctx, "demo", "demo")
if err != nil {
t.Fatalf("Unable to log in: %v", err)
}
defer wac.LogOut()
defer wac.LogOut(ctx)

now := time.Now().Unix()
alias := fmt.Sprintf("test-collection-%v", now)
c, err := wac.CreateCollection(&CollectionParams{
c, err := wac.CreateCollection(ctx, &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 {
if err := wac.DeleteCollection(ctx, c.Alias); err != nil {
t.Fatalf("Unable to delete collection %q: %v", alias, err)
}
}
Expand All @@ -96,7 +100,7 @@ func TestDeleteCollectionUnauthenticated(t *testing.T) {

now := time.Now().Unix()
alias := fmt.Sprintf("test-collection-does-not-exist-%v", now)
err := wac.DeleteCollection(alias)
err := wac.DeleteCollection(context.Background(), alias)
if err == nil {
t.Fatalf("Should not be able to delete collection %q unauthenticated.", alias)
}
Expand Down
5 changes: 3 additions & 2 deletions formatting.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package writeas

import (
"context"
"fmt"
"net/http"
)
Expand All @@ -11,7 +12,7 @@ type BodyResponse struct {

// Markdown takes raw Markdown and renders it into usable HTML. See
// https://developers.write.as/docs/api/#render-markdown.
func (c *Client) Markdown(body, collectionURL string) (string, error) {
func (c *Client) Markdown(ctx context.Context, body, collectionURL string) (string, error) {
p := &BodyResponse{}
data := struct {
RawBody string `json:"raw_body"`
Expand All @@ -21,7 +22,7 @@ func (c *Client) Markdown(body, collectionURL string) (string, error) {
CollectionURL: collectionURL,
}

env, err := c.post("/markdown", data, p)
env, err := c.post(ctx, "/markdown", data, p)
if err != nil {
return "", err
}
Expand Down
3 changes: 2 additions & 1 deletion formatting_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package writeas

import (
"context"
"testing"
)

Expand All @@ -11,7 +12,7 @@ func TestMarkdown(t *testing.T) {
out := `<p>This is <em>formatted</em> in <strong>Markdown</strong>.</p>
`

res, err := dwac.Markdown(in, "")
res, err := dwac.Markdown(context.Background(), in, "")
if err != nil {
t.Errorf("Unexpected fetch results: %+v, err: %v\n", res, err)
}
Expand Down
Loading