Browse Source

adds GetCollectionPost

this adds a client method to retrieve a collection post along with basic
tests - phabricator task T588
Rob Loranger 6 months ago
parent
commit
a853522f69
No account linked to committer's email
2 changed files with 41 additions and 0 deletions
  1. 24
    0
      collection.go
  2. 17
    0
      collection_test.go

+ 24
- 0
collection.go View File

@@ -113,6 +113,30 @@ func (c *Client) GetCollectionPosts(alias string) (*[]Post, error) {
113 113
 	}
114 114
 }
115 115
 
116
+// GetCollectionPost retrieves a post from a collection
117
+// and any error (in user-friendly form) that occurs). See
118
+// https://developers.write.as/docs/api/#retrieve-a-collection-post
119
+func (c *Client) GetCollectionPost(alias, slug string) (*Post, error) {
120
+	post := Post{}
121
+
122
+	env, err := c.get(fmt.Sprintf("/collections/%s/posts/%s", alias, slug), &post)
123
+	if err != nil {
124
+		return nil, err
125
+	}
126
+
127
+	if _, ok := env.Data.(*Post); !ok {
128
+		return nil, fmt.Errorf("Wrong data returned from API.")
129
+	}
130
+
131
+	if env.Code == http.StatusOK {
132
+		return &post, nil
133
+	} else if env.Code == http.StatusNotFound {
134
+		return nil, fmt.Errorf("Post %s not found in collection %s", slug, alias)
135
+	}
136
+
137
+	return nil, fmt.Errorf("Problem getting post %s from collection %s: %d. %v\n", slug, alias, env.Code, err)
138
+}
139
+
116 140
 // GetUserCollections retrieves the authenticated user's collections.
117 141
 // See https://developers.write.as/docs/api/#retrieve-user-39-s-collections
118 142
 func (c *Client) GetUserCollections() (*[]Collection, error) {

+ 17
- 0
collection_test.go View File

@@ -34,6 +34,23 @@ func TestGetCollectionPosts(t *testing.T) {
34 34
 	}
35 35
 }
36 36
 
37
+func TestGetCollectionPost(t *testing.T) {
38
+	wac := NewClient()
39
+
40
+	res, err := wac.GetCollectionPost("blog", "extending-write-as")
41
+	if err != nil {
42
+		t.Errorf("Unexpected fetch results: %+v, err: %v\n", res, err)
43
+	}
44
+
45
+	if res == nil {
46
+		t.Errorf("No post returned!")
47
+	}
48
+
49
+	if len(res.Content) == 0 {
50
+		t.Errorf("Post content is empty!")
51
+	}
52
+}
53
+
37 54
 func TestGetUserCollections(t *testing.T) {
38 55
 	wac := NewDevClient()
39 56
 	_, err := wac.LogIn("demo", "demo")