diff --git a/post.go b/post.go index 922ee99..c29e3fa 100644 --- a/post.go +++ b/post.go @@ -1,119 +1,138 @@ 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"` } // PostParams holds values for creating or updating a post. PostParams struct { // Parameters only for creating ID string `json:"-"` Token string `json:"token,omitempty"` // 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 } + +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.StatusOK { + 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) +} diff --git a/post_test.go b/post_test.go index d70be40..104a9f6 100644 --- a/post_test.go +++ b/post_test.go @@ -1,56 +1,69 @@ 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) return } t.Logf("Post created: %+v", p) + token := p.Token + // Update post p, err = wac.UpdatePost(&PostParams{ ID: p.ID, - Token: p.Token, + Token: token, Content: "Now it's been updated!", }) if err != nil { t.Errorf("Post update failed: %v", err) return } t.Logf("Post updated: %+v", p) + + // Delete post + err = wac.DeletePost(&PostParams{ + ID: p.ID, + Token: token, + }) + if err != nil { + t.Errorf("Post delete failed: %v", err) + return + } + t.Logf("Post deleted!") } 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) } } } diff --git a/writeas.go b/writeas.go index 97bfbb8..c6ee586 100644 --- a/writeas.go +++ b/writeas.go @@ -1,91 +1,120 @@ package writeas import ( "bytes" "encoding/json" "errors" "fmt" "github.com/writeas/impart" "io" "net/http" "time" ) const ( apiURL = "https://write.as/api" ) type Client struct { baseURL string // Access token for the user making requests. token string // Client making requests to the API client *http.Client } // defaultHTTPTimeout is the default http.Client timeout. const defaultHTTPTimeout = 10 * time.Second func NewClient() *Client { return &Client{ client: &http.Client{Timeout: defaultHTTPTimeout}, baseURL: apiURL, } } func (c *Client) SetToken(token string) { c.token = token } func (c *Client) get(path string, r interface{}) (*impart.Envelope, error) { method := "GET" if method != "GET" && method != "HEAD" { return nil, errors.New(fmt.Sprintf("Method %s not currently supported by library (only HEAD and GET).\n", method)) } return c.request(method, path, nil, r) } func (c *Client) post(path string, data, r interface{}) (*impart.Envelope, error) { b := new(bytes.Buffer) json.NewEncoder(b).Encode(data) return c.request("POST", path, b, r) } +func (c *Client) delete(path string, data map[string]string) (*impart.Envelope, error) { + r, err := c.buildRequest("DELETE", path, nil) + if err != nil { + return nil, err + } + + q := r.URL.Query() + for k, v := range data { + q.Add(k, v) + } + r.URL.RawQuery = q.Encode() + + return c.doRequest(r, nil) +} + func (c *Client) request(method, path string, data io.Reader, result interface{}) (*impart.Envelope, error) { + r, err := c.buildRequest(method, path, data) + if err != nil { + return nil, err + } + + return c.doRequest(r, result) +} + +func (c *Client) buildRequest(method, path string, data io.Reader) (*http.Request, error) { url := fmt.Sprintf("%s%s", c.baseURL, path) r, err := http.NewRequest(method, url, data) if err != nil { return nil, fmt.Errorf("Create request: %v", err) } - c.prepareRequest(r) + + return r, nil +} + +func (c *Client) doRequest(r *http.Request, result interface{}) (*impart.Envelope, error) { + fmt.Printf("%sing %s\n", "SOMETH", r.URL) resp, err := c.client.Do(r) if err != nil { return nil, fmt.Errorf("Request: %v", err) } defer resp.Body.Close() env := &impart.Envelope{ Code: resp.StatusCode, } if result != nil { env.Data = result - } - err = json.NewDecoder(resp.Body).Decode(&env) - if err != nil { - return nil, err + err = json.NewDecoder(resp.Body).Decode(&env) + if err != nil { + return nil, err + } } return env, nil } func (c *Client) prepareRequest(r *http.Request) { r.Header.Add("User-Agent", "go-writeas v1") r.Header.Add("Content-Type", "application/json") if c.token != "" { r.Header.Add("Authorization", "Token "+c.token) } }