mirror of
https://github.com/writeas/go-writeas.git
synced 2025-07-27 15:18:28 +00:00
Support creating a post
Plus fix Client post func and rename SubmittedPost to PostParams.
This commit is contained in:
parent
04c8b73524
commit
d2110756cd
27
post.go
27
post.go
@ -28,8 +28,8 @@ type (
|
||||
Collection *Collection `json:"collection,omitempty"`
|
||||
}
|
||||
|
||||
// SubmittedPost represents a new post for publishing or updating.
|
||||
SubmittedPost struct {
|
||||
// PostParams holds values for creating or updating a post.
|
||||
PostParams struct {
|
||||
Title string `json:"title"`
|
||||
Content string `json:"body"`
|
||||
Font string `json:"font"`
|
||||
@ -64,3 +64,26 @@ func (c *Client) GetPost(id string) (*Post, error) {
|
||||
}
|
||||
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
|
||||
}
|
||||
|
16
post_test.go
16
post_test.go
@ -6,7 +6,21 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
func TestGet(t *testing.T) {
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetPost(t *testing.T) {
|
||||
wac := NewClient("")
|
||||
|
||||
res, err := wac.GetPost("zekk5r9apum6p")
|
||||
|
@ -1,6 +1,7 @@
|
||||
package writeas
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
@ -43,8 +44,10 @@ func (c *Client) get(path string, r interface{}) (*impart.Envelope, error) {
|
||||
return c.request(method, path, nil, r)
|
||||
}
|
||||
|
||||
func (c *Client) post(path string, data io.Reader, r interface{}) (*impart.Envelope, error) {
|
||||
return c.request("POST", path, data, 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) request(method, path string, data io.Reader, result interface{}) (*impart.Envelope, error) {
|
||||
|
Loading…
Reference in New Issue
Block a user