2016-09-04 23:31:57 +00:00
|
|
|
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 {
|
2016-09-05 00:33:38 +00:00
|
|
|
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"`
|
2016-09-04 23:31:57 +00:00
|
|
|
|
|
|
|
Collection *Collection `json:"collection,omitempty"`
|
|
|
|
}
|
|
|
|
|
2016-09-05 00:11:37 +00:00
|
|
|
// PostParams holds values for creating or updating a post.
|
|
|
|
PostParams struct {
|
2016-09-05 00:33:38 +00:00
|
|
|
// Parameters only for creating
|
|
|
|
ID string `json:"-"`
|
|
|
|
Token string `json:"token,omitempty"`
|
2016-09-04 23:31:57 +00:00
|
|
|
|
2016-09-05 00:33:38 +00:00
|
|
|
// 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"`
|
2016-09-04 23:31:57 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2016-09-05 00:11:37 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2016-09-05 00:33:38 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|