diff --git a/auth.go b/auth.go new file mode 100644 index 0000000..cbe9a13 --- /dev/null +++ b/auth.go @@ -0,0 +1,12 @@ +package writeas + +import ( + "net/http" +) + +func (c *Client) isNotLoggedIn(code int) bool { + if c.token == "" { + return false + } + return code == http.StatusUnauthorized +} diff --git a/post.go b/post.go index c4d4996..922ee99 100644 --- a/post.go +++ b/post.go @@ -1,89 +1,119 @@ 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"` - ModifyToken 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"` + 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"` } // PostParams holds values for creating or updating a post. PostParams struct { - Title string `json:"title"` - Content string `json:"body"` - Font string `json:"font"` - IsRTL *bool `json:"rtl"` - Language *string `json:"lang"` + // Parameters only for creating + ID string `json:"-"` + Token string `json:"token,omitempty"` - Crosspost []map[string]string `json:"crosspost"` + // 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"` } ) 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 } 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 } + +func (c *Client) UpdatePost(sp *PostParams) (*Post, error) { + p := &Post{} + env, err := c.post(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 +} diff --git a/post_test.go b/post_test.go index 6c4ad14..d70be40 100644 --- a/post_test.go +++ b/post_test.go @@ -1,44 +1,56 @@ package writeas import ( "testing" "strings" ) func TestCreatePost(t *testing.T) { wac := NewClient() p, err := wac.CreatePost(&PostParams{ Title: "Title!", Content: "This is a post.", Font: "sans", }) if err != nil { t.Errorf("Post create failed: %v", err) - } else { - t.Logf("Post created: %+v", p) + return + } + t.Logf("Post created: %+v", p) + + // Update post + p, err = wac.UpdatePost(&PostParams{ + ID: p.ID, + Token: p.Token, + Content: "Now it's been updated!", + }) + if err != nil { + t.Errorf("Post update failed: %v", err) + return } + t.Logf("Post updated: %+v", p) } func TestGetPost(t *testing.T) { wac := NewClient() res, err := wac.GetPost("zekk5r9apum6p") if err != nil { t.Errorf("Unexpected fetch results: %+v, err: %v\n", res, err) } else { t.Logf("Post: %+v", res) if res.Content != "This is a post." { t.Errorf("Unexpected fetch results: %+v\n", res) } } res, err = wac.GetPost("3psnxyhqxy3hq") if err != nil { t.Errorf("Unexpected fetch results: %+v, err: %v\n", res, err) } else { if !strings.HasPrefix(res.Content, " Write.as Blog") { t.Errorf("Unexpected fetch results: %+v\n", res) } } }