Page MenuHomeMusing Studio

No OneTemporary

diff --git a/post.go b/post.go
index 22b8630..c4d4996 100644
--- a/post.go
+++ b/post.go
@@ -1,66 +1,89 @@
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"`
ModifyToken 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"`
}
- // 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"`
IsRTL *bool `json:"rtl"`
Language *string `json:"lang"`
Crosspost []map[string]string `json:"crosspost"`
}
)
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
+}
diff --git a/post_test.go b/post_test.go
index 65ceee2..80a890c 100644
--- a/post_test.go
+++ b/post_test.go
@@ -1,30 +1,44 @@
package writeas
import (
"testing"
"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")
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 6da8793..e6f4f53 100644
--- a/writeas.go
+++ b/writeas.go
@@ -1,85 +1,88 @@
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(token string) *Client {
return &Client{
token: token,
client: &http.Client{Timeout: defaultHTTPTimeout},
baseURL: apiURL,
}
}
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 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) {
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)
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)
}
}

File Metadata

Mime Type
text/x-diff
Expires
Mon, Nov 25, 3:54 PM (1 d, 18 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
3106745

Event Timeline