-
Notifications
You must be signed in to change notification settings - Fork 0
/
post.go
93 lines (74 loc) · 1.97 KB
/
post.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package subclub
import (
"fmt"
)
type (
// Post represents a post result on the sub.club API.
Post struct {
Success bool `json:"success"`
PostID string `json:"postId"`
Post string `json:"post"`
URI string `json:"uri"`
URL string `json:"url"`
}
// PostParams holds the parameters for creating a new sub.club post.
PostParams struct {
Content string `json:"content"`
}
// postUpdateParams is an internal struct for editing a sub.club post.
postUpdateParams struct {
PostID string `json:"postId"`
Content string `json:"content"`
}
// PostDeleteParams has parameters for deleting a sub.club post.
PostDeleteParams struct {
PostID string `json:"postId"`
}
// PostDeleteResult represents an API response after deleting a sub.club post.
PostDeleteResult struct {
Deleted bool `json:"deleted"`
}
)
// Post creates a new post on sub.club with the given parameters.
func (c *Client) Post(pp *PostParams) (*Post, error) {
p := &Post{}
env, err := c.post("/post", pp, p)
if err != nil {
return nil, err
}
var ok bool
if p, ok = env.(*Post); !ok {
return nil, fmt.Errorf("wrong data returned from API")
}
return p, nil
}
// UpdatePost edits the given post with the supplied PostParams.
func (c *Client) UpdatePost(postID string, pp *PostParams) (*Post, error) {
p := &Post{}
pup := &postUpdateParams{
PostID: postID,
Content: pp.Content,
}
env, err := c.post("/post/edit", pup, p)
if err != nil {
return nil, err
}
var ok bool
if p, ok = env.(*Post); !ok {
return nil, fmt.Errorf("wrong data returned from API")
}
return p, nil
}
// DeletePost deletes a post with the given postID.
func (c *Client) DeletePost(postID string) (*PostDeleteResult, error) {
res := &PostDeleteResult{}
env, err := c.post("/post/delete", &PostDeleteParams{PostID: postID}, res)
if err != nil {
return nil, err
}
var ok bool
if res, ok = env.(*PostDeleteResult); !ok {
return nil, fmt.Errorf("wrong data returned from API")
}
return res, nil
}