diff --git a/README.md b/README.md index a29aec1..a360d85 100644 --- a/README.md +++ b/README.md @@ -1,69 +1,69 @@ # go-writeas [![godoc](https://godoc.org/github.com/writeas/go-writeas?status.svg)](https://godoc.org/github.com/writeas/go-writeas) Official Write.as Go client library. ## Installation ```bash go get github.com/writeas/go-writeas ``` ## Documentation -See all functionality and usages in the [API documentation](https://writeas.github.io/docs/). +See all functionality and usages in the [API documentation](https://developer.write.as/docs/api/). ### Example usage ```go import "github.com/writeas/go-writeas" func main() { // Create the client c := writeas.NewClient() // Publish a post p, err := c.CreatePost(&PostParams{ Title: "Title!", Content: "This is a post.", Font: "sans", }) if err != nil { // Perhaps show err.Error() } // Update a published post p, err := c.UpdatePost(&PostParams{ ID: "3psnxyhqxy3hq", Token: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", Content: "Now it's been updated!", }) if err != nil { // handle } // Get a published post p, err := c.GetPost("3psnxyhqxy3hq") if err != nil { // handle } // Delete a post err := c.DeletePost(&PostParams{ ID: "3psnxyhqxy3hq", Token: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", }) } ``` ## Contributing The library covers our usage, but might not be comprehensive of the API. So we always welcome contributions and improvements from the community. Before sending pull requests, make sure you've done the following: * Run `go fmt` on all updated .go files. * Document all structs and funcs. ## License MIT diff --git a/auth.go b/auth.go index 6face6a..ed9e336 100644 --- a/auth.go +++ b/auth.go @@ -1,52 +1,52 @@ package writeas import ( "fmt" "net/http" ) // LogIn authenticates a user with Write.as. -// See https://writeas.github.io/docs/#authenticate-a-user +// See https://developer.write.as/docs/api/#authenticate-a-user func (c *Client) LogIn(username, pass string) (*AuthUser, error) { u := &AuthUser{} up := struct { Alias string `json:"alias"` Pass string `json:"pass"` }{ Alias: username, Pass: pass, } env, err := c.post("/auth/login", up, u) if err != nil { return nil, err } var ok bool if u, ok = env.Data.(*AuthUser); !ok { return nil, fmt.Errorf("Wrong data returned from API.") } status := env.Code if status == http.StatusOK { return u, nil } else if status == http.StatusBadRequest { return nil, fmt.Errorf("Bad request: %s", env.ErrorMessage) } else if status == http.StatusUnauthorized { return nil, fmt.Errorf("Incorrect password.") } else if status == http.StatusNotFound { return nil, fmt.Errorf("User does not exist.") } else if status == http.StatusTooManyRequests { return nil, fmt.Errorf("Stop repeatedly trying to log in.") } else { return nil, fmt.Errorf("Problem authenticating: %s. %v\n", status, err) } return u, nil } func (c *Client) isNotLoggedIn(code int) bool { if c.token == "" { return false } return code == http.StatusUnauthorized } diff --git a/collection.go b/collection.go index e9102b0..dc808b4 100644 --- a/collection.go +++ b/collection.go @@ -1,87 +1,87 @@ package writeas import ( "fmt" "net/http" ) type ( // Collection represents a collection of posts. Blogs are a type of collection // on Write.as. Collection struct { Alias string `json:"alias"` Title string `json:"title"` Description string `json:"description"` StyleSheet string `json:"style_sheet"` Private bool `json:"private"` Views int64 `json:"views"` Domain string `json:"domain,omitempty"` Email string `json:"email,omitempty"` TotalPosts int `json:"total_posts"` } // CollectionParams holds values for creating a collection. CollectionParams struct { Alias string `json:"alias"` Title string `json:"title"` } ) // GetCollection retrieves a collection, returning the Collection and any error // (in user-friendly form) that occurs. See -// https://writeas.github.io/docs/#retrieve-a-collection +// https://developer.write.as/docs/api/#retrieve-a-collection func (c *Client) GetCollection(alias string) (*Collection, error) { coll := &Collection{} env, err := c.get(fmt.Sprintf("/collections/%s", alias), coll) if err != nil { return nil, err } var ok bool if coll, ok = env.Data.(*Collection); !ok { return nil, fmt.Errorf("Wrong data returned from API.") } status := env.Code if status == http.StatusOK { return coll, nil } else if status == http.StatusNotFound { return nil, fmt.Errorf("Collection not found.") } else { return nil, fmt.Errorf("Problem getting collection: %s. %v\n", status, err) } return coll, nil } // CreateCollection creates a new collection, returning a user-friendly error // if one comes up. Requires a Write.as subscription. See -// https://writeas.github.io/docs/#create-a-collection +// https://developer.write.as/docs/api/#create-a-collection func (c *Client) CreateCollection(sp *CollectionParams) (*Collection, error) { p := &Collection{} env, err := c.post("/collections", sp, p) if err != nil { return nil, err } var ok bool if p, ok = env.Data.(*Collection); !ok { return nil, fmt.Errorf("Wrong data returned from API.") } status := env.Code if status == http.StatusCreated { return p, nil } else if status == http.StatusBadRequest { return nil, fmt.Errorf("Bad request: %s", env.ErrorMessage) } else if status == http.StatusForbidden { return nil, fmt.Errorf("Casual or Pro user required.") } else if status == http.StatusConflict { return nil, fmt.Errorf("Collection name is already taken.") } else if status == http.StatusPreconditionFailed { return nil, fmt.Errorf("Reached max collection quota.") } else { return nil, fmt.Errorf("Problem getting post: %s. %v\n", status, err) } return p, nil } diff --git a/post.go b/post.go index ed65de9..7f361ae 100644 --- a/post.go +++ b/post.go @@ -1,189 +1,189 @@ package writeas import ( "fmt" "net/http" "time" ) type ( // Post represents a published Write.as post, whether anonymous, owned by a // user, or part of a collection. Post struct { ID string `json:"id"` Slug string `json:"slug"` Token string `json:"token"` Font string `json:"appearance"` Language *string `json:"language"` RTL *bool `json:"rtl"` Listed bool `json:"listed"` Created time.Time `json:"created"` Title string `json:"title"` Content string `json:"body"` Views int64 `json:"views"` Tags []string `json:"tags"` Images []string `json:"images"` OwnerName string `json:"owner,omitempty"` Collection *Collection `json:"collection,omitempty"` } // OwnedPostParams are, together, fields only the original post author knows. OwnedPostParams struct { ID string `json:"-"` Token string `json:"token,omitempty"` } // PostParams holds values for creating or updating a post. PostParams struct { // Parameters only for updating OwnedPostParams // Parameters for creating or updating Title string `json:"title,omitempty"` Content string `json:"body,omitempty"` Font string `json:"font,omitempty"` IsRTL *bool `json:"rtl,omitempty"` Language *string `json:"lang,omitempty"` Crosspost []map[string]string `json:"crosspost,omitempty"` } // ClaimPostResult contains the post-specific result for a request to // associate a post to an account. ClaimPostResult struct { ID string `json:"id,omitempty"` Code int `json:"code,omitempty"` ErrorMessage string `json:"error_msg,omitempty"` Post *Post `json:"post,omitempty"` } ) // GetPost retrieves a published post, returning the Post and any error (in // user-friendly form) that occurs. See -// https://writeas.github.io/docs/#retrieve-a-post. +// https://developer.write.as/docs/api/#retrieve-a-post. func (c *Client) GetPost(id string) (*Post, error) { p := &Post{} env, err := c.get(fmt.Sprintf("/posts/%s", id), p) if err != nil { return nil, err } var ok bool if p, ok = env.Data.(*Post); !ok { return nil, fmt.Errorf("Wrong data returned from API.") } status := env.Code if status == http.StatusOK { return p, nil } else if status == http.StatusNotFound { return nil, fmt.Errorf("Post not found.") } else if status == http.StatusGone { return nil, fmt.Errorf("Post unpublished.") } else { return nil, fmt.Errorf("Problem getting post: %s. %v\n", status, err) } return p, nil } // CreatePost publishes a new post, returning a user-friendly error if one comes -// up. See https://writeas.github.io/docs/#publish-a-post. +// up. See https://developer.write.as/docs/api/#publish-a-post. func (c *Client) CreatePost(sp *PostParams) (*Post, error) { p := &Post{} env, err := c.post("/posts", sp, p) if err != nil { return nil, err } var ok bool if p, ok = env.Data.(*Post); !ok { return nil, fmt.Errorf("Wrong data returned from API.") } status := env.Code if status == http.StatusCreated { return p, nil } else if status == http.StatusBadRequest { return nil, fmt.Errorf("Bad request: %s", env.ErrorMessage) } else { return nil, fmt.Errorf("Problem getting post: %s. %v\n", status, err) } return p, nil } // UpdatePost updates a published post with the given PostParams. See -// https://writeas.github.io/docs/#update-a-post. +// https://developer.write.as/docs/api/#update-a-post. func (c *Client) UpdatePost(sp *PostParams) (*Post, error) { p := &Post{} env, err := c.put(fmt.Sprintf("/posts/%s", sp.ID), sp, p) if err != nil { return nil, err } var ok bool if p, ok = env.Data.(*Post); !ok { return nil, fmt.Errorf("Wrong data returned from API.") } status := env.Code if status == http.StatusOK { return p, nil } else if c.isNotLoggedIn(status) { return nil, fmt.Errorf("Not authenticated.") } else if status == http.StatusBadRequest { return nil, fmt.Errorf("Bad request: %s", env.ErrorMessage) } else { return nil, fmt.Errorf("Problem getting post: %s. %v\n", status, err) } return p, nil } // DeletePost permanently deletes a published post. See -// https://writeas.github.io/docs/#delete-a-post. +// https://developer.write.as/docs/api/#delete-a-post. func (c *Client) DeletePost(sp *PostParams) error { env, err := c.delete(fmt.Sprintf("/posts/%s", sp.ID), map[string]string{ "token": sp.Token, }) if err != nil { return err } status := env.Code if status == http.StatusNoContent { return nil } else if c.isNotLoggedIn(status) { return fmt.Errorf("Not authenticated.") } else if status == http.StatusBadRequest { return fmt.Errorf("Bad request: %s", env.ErrorMessage) } return fmt.Errorf("Problem getting post: %s. %v\n", status, err) } // ClaimPosts associates anonymous posts with a user / account. -// https://writeas.github.io/docs/#claim-posts. +// https://developer.write.as/docs/api/#claim-posts. func (c *Client) ClaimPosts(sp *[]OwnedPostParams) (*[]ClaimPostResult, error) { p := &[]ClaimPostResult{} env, err := c.put("/posts/claim", sp, p) if err != nil { return nil, err } var ok bool if p, ok = env.Data.(*[]ClaimPostResult); !ok { return nil, fmt.Errorf("Wrong data returned from API.") } status := env.Code if status == http.StatusOK { return p, nil } else if c.isNotLoggedIn(status) { return nil, fmt.Errorf("Not authenticated.") } else if status == http.StatusBadRequest { return nil, fmt.Errorf("Bad request: %s", env.ErrorMessage) } else { return nil, fmt.Errorf("Problem getting post: %s. %v\n", status, err) } // TODO: does this also happen with moving posts? return p, nil }