mirror of
https://github.com/writeas/go-writeas.git
synced 2025-07-27 21:18:19 +00:00
Support updating posts
Plus change Post's ModifyToken to Token.
This commit is contained in:
parent
0376852c2f
commit
8a9ac652f2
12
auth.go
Normal file
12
auth.go
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
package writeas
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (c *Client) isNotLoggedIn(code int) bool {
|
||||||
|
if c.token == "" {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return code == http.StatusUnauthorized
|
||||||
|
}
|
70
post.go
70
post.go
@ -10,33 +10,38 @@ type (
|
|||||||
// Post represents a published Write.as post, whether anonymous, owned by a
|
// Post represents a published Write.as post, whether anonymous, owned by a
|
||||||
// user, or part of a collection.
|
// user, or part of a collection.
|
||||||
Post struct {
|
Post struct {
|
||||||
ID string `json:"id"`
|
ID string `json:"id"`
|
||||||
Slug string `json:"slug"`
|
Slug string `json:"slug"`
|
||||||
ModifyToken string `json:"token"`
|
Token string `json:"token"`
|
||||||
Font string `json:"appearance"`
|
Font string `json:"appearance"`
|
||||||
Language *string `json:"language"`
|
Language *string `json:"language"`
|
||||||
RTL *bool `json:"rtl"`
|
RTL *bool `json:"rtl"`
|
||||||
Listed bool `json:"listed"`
|
Listed bool `json:"listed"`
|
||||||
Created time.Time `json:"created"`
|
Created time.Time `json:"created"`
|
||||||
Title string `json:"title"`
|
Title string `json:"title"`
|
||||||
Content string `json:"body"`
|
Content string `json:"body"`
|
||||||
Views int64 `json:"views"`
|
Views int64 `json:"views"`
|
||||||
Tags []string `json:"tags"`
|
Tags []string `json:"tags"`
|
||||||
Images []string `json:"images"`
|
Images []string `json:"images"`
|
||||||
OwnerName string `json:"owner,omitempty"`
|
OwnerName string `json:"owner,omitempty"`
|
||||||
|
|
||||||
Collection *Collection `json:"collection,omitempty"`
|
Collection *Collection `json:"collection,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// PostParams holds values for creating or updating a post.
|
// PostParams holds values for creating or updating a post.
|
||||||
PostParams struct {
|
PostParams struct {
|
||||||
Title string `json:"title"`
|
// Parameters only for creating
|
||||||
Content string `json:"body"`
|
ID string `json:"-"`
|
||||||
Font string `json:"font"`
|
Token string `json:"token,omitempty"`
|
||||||
IsRTL *bool `json:"rtl"`
|
|
||||||
Language *string `json:"lang"`
|
|
||||||
|
|
||||||
Crosspost []map[string]string `json:"crosspost"`
|
// 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"`
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -87,3 +92,28 @@ func (c *Client) CreatePost(sp *PostParams) (*Post, error) {
|
|||||||
}
|
}
|
||||||
return p, nil
|
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
|
||||||
|
}
|
||||||
|
16
post_test.go
16
post_test.go
@ -15,9 +15,21 @@ func TestCreatePost(t *testing.T) {
|
|||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Post create failed: %v", err)
|
t.Errorf("Post create failed: %v", err)
|
||||||
} else {
|
return
|
||||||
t.Logf("Post created: %+v", p)
|
|
||||||
}
|
}
|
||||||
|
t.Logf("Post created: %+v", p)
|
||||||
|
|
||||||
|
// Update post
|
||||||
|
p, err = wac.UpdatePost(&PostParams{
|
||||||
|
ID: p.ID,
|
||||||
|
Token: p.Token,
|
||||||
|
Content: "Now it's been updated!",
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Post update failed: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
t.Logf("Post updated: %+v", p)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGetPost(t *testing.T) {
|
func TestGetPost(t *testing.T) {
|
||||||
|
Loading…
Reference in New Issue
Block a user