Kaynağa Gözat

adds GetCollectionPost

this adds a client method to retrieve a collection post along with basic
tests - phabricator task T588
pull/17/head
Rob Loranger 4 yıl önce
ebeveyn
işleme
a853522f69
Veri tabanında bu imza için bilinen anahtar bulunamadı GPG Anahtar Kimliği: D6F1633A4F0903B8
2 değiştirilmiş dosya ile 41 ekleme ve 0 silme
  1. +24
    -0
      collection.go
  2. +17
    -0
      collection_test.go

+ 24
- 0
collection.go Dosyayı Görüntüle

@@ -113,6 +113,30 @@ func (c *Client) GetCollectionPosts(alias string) (*[]Post, error) {
}
}

// GetCollectionPost retrieves a post from a collection
// and any error (in user-friendly form) that occurs). See
// https://developers.write.as/docs/api/#retrieve-a-collection-post
func (c *Client) GetCollectionPost(alias, slug string) (*Post, error) {
post := Post{}

env, err := c.get(fmt.Sprintf("/collections/%s/posts/%s", alias, slug), &post)
if err != nil {
return nil, err
}

if _, ok := env.Data.(*Post); !ok {
return nil, fmt.Errorf("Wrong data returned from API.")
}

if env.Code == http.StatusOK {
return &post, nil
} else if env.Code == http.StatusNotFound {
return nil, fmt.Errorf("Post %s not found in collection %s", slug, alias)
}

return nil, fmt.Errorf("Problem getting post %s from collection %s: %d. %v\n", slug, alias, env.Code, err)
}

// GetUserCollections retrieves the authenticated user's collections.
// See https://developers.write.as/docs/api/#retrieve-user-39-s-collections
func (c *Client) GetUserCollections() (*[]Collection, error) {


+ 17
- 0
collection_test.go Dosyayı Görüntüle

@@ -34,6 +34,23 @@ func TestGetCollectionPosts(t *testing.T) {
}
}

func TestGetCollectionPost(t *testing.T) {
wac := NewClient()

res, err := wac.GetCollectionPost("blog", "extending-write-as")
if err != nil {
t.Errorf("Unexpected fetch results: %+v, err: %v\n", res, err)
}

if res == nil {
t.Errorf("No post returned!")
}

if len(res.Content) == 0 {
t.Errorf("Post content is empty!")
}
}

func TestGetUserCollections(t *testing.T) {
wac := NewDevClient()
_, err := wac.LogIn("demo", "demo")


Yükleniyor…
İptal
Kaydet