~thrrgilag/woodstock

a066b79ca8515e63592c3070adea1930b803913e — Morgan McMillian 2 years ago c10a50e
change post to json and support raw objects

resolves issue #8
3 files changed, 65 insertions(+), 19 deletions(-)

M objects.go
M posts.go
M posts_test.go
M objects.go => objects.go +5 -0
@@ 258,3 258,8 @@ type Marker struct {
	Version    string `json:"version"`
	Name       string `json:"name"`
}

type Raw struct {
	Type  string            `json:"type"`
	Value map[string]string `json:"value"`
}

M posts.go => posts.go +18 -4
@@ 1,6 1,7 @@
package woodstock

import (
	"encoding/json"
	"net/url"
	"strconv"
	"strings"


@@ 24,6 25,11 @@ type ActionsResult struct {
	Data []Action `json:"data"`
}

type NewPost struct {
	Text string `json:"text"`
	Raw  []Raw  `json:"raw"`
}

// GetPost retrieves a post
// https://pnut.io/docs/api/resources/posts/lookup#get-posts-id
func (c *Client) GetPost(id string) (result PostResult, err error) {


@@ 52,17 58,25 @@ func (c *Client) GetPostRevisions(id string) (result PostsResult, err error) {

// Post creates a post
// https://pnut.io/docs/api/resources/posts/lifecycle#post-posts
func (c *Client) Post(v url.Values) (result PostResult, err error) {
func (c *Client) Post(params NewPost) (result PostResult, err error) {
	json, err := json.Marshal(params)
	if err != nil {
		return
	}
	responseCh := make(chan response)
	c.queryQueue <- query{url: PostAPI, form: v, data: &result, method: "POST", responseCh: responseCh}
	c.queryQueue <- query{url: PostAPI, data: &result, method: "POST", responseCh: responseCh, json: string(json)}
	return result, (<-responseCh).err
}

// RevisePost updates an existing post
// https://pnut.io/docs/api/resources/posts/lifecycle#put-posts-id
func (c *Client) RevisePost(id string, v url.Values) (result PostResult, err error) {
func (c *Client) RevisePost(id string, params NewPost) (result PostResult, err error) {
	json, err := json.Marshal(params)
	if err != nil {
		return
	}
	responseCh := make(chan response)
	c.queryQueue <- query{url: PostAPI + "/" + id, form: v, data: &result, method: "PUT", responseCh: responseCh}
	c.queryQueue <- query{url: PostAPI + "/" + id, data: &result, method: "PUT", responseCh: responseCh, json: string(json)}
	return result, (<-responseCh).err
}


M posts_test.go => posts_test.go +42 -15
@@ 1,7 1,7 @@
package woodstock

import (
	"net/url"
	"strconv"
	"testing"
	"time"
)


@@ 14,9 14,38 @@ func TestPost(t *testing.T) {
	text := "Hello pnut.io"
	client := NewClient(config.ClientID, "")
	client.SetAccessToken(config.AccessToken)
	v := url.Values{}
	v.Set("text", text)
	post, err := client.Post(v)
	language := map[string]string{"language": "en"}
	var raw []Raw
	raw = append(raw, Raw{Type: "io.pnut.core.language", Value: language})
	newpost := NewPost{Text: text, Raw: raw}
	post, err := client.Post(newpost)
	if err != nil {
		t.Error(err)
	}
	if post.Data.Content.Text != text {
		t.Errorf("Post appears incorrect, got: %s, want: %s", post.Data.Content.Text, text)
	}
	time.Sleep(Delay)
}

func TestLongPost(t *testing.T) {
	config, err := GetConfig()
	if err != nil {
		t.Error(err)
	}
	text := "Hello pnut.io"
	client := NewClient(config.ClientID, "")
	client.SetAccessToken(config.AccessToken)
	language := map[string]string{"language": "en"}
	longpost := map[string]string{}
	longpost["body"] = text
	longpost["title"] = "longpost test"
	longpost["tstamp"] = strconv.FormatInt(time.Now().UnixNano()/int64(time.Microsecond), 10)
	var raw []Raw
	raw = append(raw, Raw{Type: "io.pnut.core.language", Value: language})
	raw = append(raw, Raw{Type: "nl.chimpnut.blog.post", Value: longpost})
	newpost := NewPost{Text: text, Raw: raw}
	post, err := client.Post(newpost)
	if err != nil {
		t.Error(err)
	}


@@ 97,22 126,21 @@ func TestRevisePost(t *testing.T) {
		t.Error(err)
	}
	text := "Hello pnut.io."
	new := "Hello pnut.io golang test"
	newtext := "Hello pnut.io golang test"
	client := NewClient(config.ClientID, "")
	client.SetAccessToken(config.AccessToken)
	v := url.Values{}
	v.Set("text", text)
	post, err := client.Post(v)
	newpost := NewPost{Text: text}
	post, err := client.Post(newpost)
	if err != nil {
		t.Error(err)
	}
	v.Set("text", new)
	rpost, rerr := client.RevisePost(post.Data.ID, v)
	newpost = NewPost{Text: newtext}
	rpost, rerr := client.RevisePost(post.Data.ID, newpost)
	if rerr != nil {
		t.Error(rerr)
	}
	if rpost.Data.Content.Text != new {
		t.Errorf("Revised post appears incorrect, got: %s, want: %s", post.Data.Content.Text, new)
	if rpost.Data.Content.Text != newtext {
		t.Errorf("Revised post appears incorrect, got: %s, want: %s", post.Data.Content.Text, newtext)
	}
	time.Sleep(Delay)
}


@@ 125,9 153,8 @@ func TestDeletePost(t *testing.T) {
	text := "Hello pnut.io_"
	client := NewClient(config.ClientID, "")
	client.SetAccessToken(config.AccessToken)
	v := url.Values{}
	v.Set("text", text)
	post, err := client.Post(v)
	newpost := NewPost{Text: text}
	post, err := client.Post(newpost)
	if err != nil {
		t.Error(err)
	}