Page MenuHomeMusing Studio

No OneTemporary

diff --git a/post.go b/post.go
index 450084b..8839ba8 100644
--- a/post.go
+++ b/post.go
@@ -1,147 +1,187 @@
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
- ID string `json:"-"`
- Token string `json:"token,omitempty"`
+ 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 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.
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.
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.
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.
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.
+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
+}

File Metadata

Mime Type
text/x-diff
Expires
Sat, Apr 26, 8:42 PM (3 h, 31 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
3216689

Event Timeline