diff --git a/post.go b/post.go index 7102a85..5ebb575 100644 --- a/post.go +++ b/post.go @@ -1,138 +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) + 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 } 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) } diff --git a/writeas.go b/writeas.go index 74ab889..a5443f6 100644 --- a/writeas.go +++ b/writeas.go @@ -1,119 +1,125 @@ 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) put(path string, data, r interface{}) (*impart.Envelope, error) { + b := new(bytes.Buffer) + json.NewEncoder(b).Encode(data) + return c.request("PUT", 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) { 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 } } 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) } }