Go client for the Write.as API https://developers.write.as

collection.go 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. package writeas
  2. import (
  3. "fmt"
  4. "net/http"
  5. )
  6. type (
  7. // Collection represents a collection of posts. Blogs are a type of collection
  8. // on Write.as.
  9. Collection struct {
  10. Alias string `json:"alias"`
  11. Title string `json:"title"`
  12. Description string `json:"description"`
  13. StyleSheet string `json:"style_sheet"`
  14. Private bool `json:"private"`
  15. Views int64 `json:"views"`
  16. Domain string `json:"domain,omitempty"`
  17. Email string `json:"email,omitempty"`
  18. URL string `json:"url,omitempty"`
  19. TotalPosts int `json:"total_posts"`
  20. Posts *[]Post `json:"posts,omitempty"`
  21. }
  22. // CollectionParams holds values for creating a collection.
  23. CollectionParams struct {
  24. Alias string `json:"alias"`
  25. Title string `json:"title"`
  26. Description string `json:"description,omitempty"`
  27. }
  28. )
  29. // CreateCollection creates a new collection, returning a user-friendly error
  30. // if one comes up. Requires a Write.as subscription. See
  31. // https://developers.write.as/docs/api/#create-a-collection
  32. func (c *Client) CreateCollection(sp *CollectionParams) (*Collection, error) {
  33. p := &Collection{}
  34. env, err := c.post("/collections", sp, p)
  35. if err != nil {
  36. return nil, err
  37. }
  38. var ok bool
  39. if p, ok = env.Data.(*Collection); !ok {
  40. return nil, fmt.Errorf("Wrong data returned from API.")
  41. }
  42. status := env.Code
  43. if status != http.StatusCreated {
  44. if status == http.StatusBadRequest {
  45. return nil, fmt.Errorf("Bad request: %s", env.ErrorMessage)
  46. } else if status == http.StatusForbidden {
  47. return nil, fmt.Errorf("Casual or Pro user required.")
  48. } else if status == http.StatusConflict {
  49. return nil, fmt.Errorf("Collection name is already taken.")
  50. } else if status == http.StatusPreconditionFailed {
  51. return nil, fmt.Errorf("Reached max collection quota.")
  52. }
  53. return nil, fmt.Errorf("Problem getting post: %d. %v\n", status, err)
  54. }
  55. return p, nil
  56. }
  57. // GetCollection retrieves a collection, returning the Collection and any error
  58. // (in user-friendly form) that occurs. See
  59. // https://developers.write.as/docs/api/#retrieve-a-collection
  60. func (c *Client) GetCollection(alias string) (*Collection, error) {
  61. coll := &Collection{}
  62. env, err := c.get(fmt.Sprintf("/collections/%s", alias), coll)
  63. if err != nil {
  64. return nil, err
  65. }
  66. var ok bool
  67. if coll, ok = env.Data.(*Collection); !ok {
  68. return nil, fmt.Errorf("Wrong data returned from API.")
  69. }
  70. status := env.Code
  71. if status == http.StatusOK {
  72. return coll, nil
  73. } else if status == http.StatusNotFound {
  74. return nil, fmt.Errorf("Collection not found.")
  75. } else {
  76. return nil, fmt.Errorf("Problem getting collection: %d. %v\n", status, err)
  77. }
  78. }
  79. // GetCollectionPosts retrieves a collection's posts, returning the Posts
  80. // and any error (in user-friendly form) that occurs. See
  81. // https://developers.write.as/docs/api/#retrieve-collection-posts
  82. func (c *Client) GetCollectionPosts(alias string) (*[]Post, error) {
  83. coll := &Collection{}
  84. env, err := c.get(fmt.Sprintf("/collections/%s/posts", alias), coll)
  85. if err != nil {
  86. return nil, err
  87. }
  88. var ok bool
  89. if coll, ok = env.Data.(*Collection); !ok {
  90. return nil, fmt.Errorf("Wrong data returned from API.")
  91. }
  92. status := env.Code
  93. if status == http.StatusOK {
  94. return coll.Posts, nil
  95. } else if status == http.StatusNotFound {
  96. return nil, fmt.Errorf("Collection not found.")
  97. } else {
  98. return nil, fmt.Errorf("Problem getting collection: %d. %v\n", status, err)
  99. }
  100. }
  101. // GetCollectionPost retrieves a post from a collection
  102. // and any error (in user-friendly form) that occurs). See
  103. // https://developers.write.as/docs/api/#retrieve-a-collection-post
  104. func (c *Client) GetCollectionPost(alias, slug string) (*Post, error) {
  105. post := Post{}
  106. env, err := c.get(fmt.Sprintf("/collections/%s/posts/%s", alias, slug), &post)
  107. if err != nil {
  108. return nil, err
  109. }
  110. if _, ok := env.Data.(*Post); !ok {
  111. return nil, fmt.Errorf("Wrong data returned from API.")
  112. }
  113. if env.Code == http.StatusOK {
  114. return &post, nil
  115. } else if env.Code == http.StatusNotFound {
  116. return nil, fmt.Errorf("Post %s not found in collection %s", slug, alias)
  117. }
  118. return nil, fmt.Errorf("Problem getting post %s from collection %s: %d. %v\n", slug, alias, env.Code, err)
  119. }
  120. // GetUserCollections retrieves the authenticated user's collections.
  121. // See https://developers.write.as/docs/api/#retrieve-user-39-s-collections
  122. func (c *Client) GetUserCollections() (*[]Collection, error) {
  123. colls := &[]Collection{}
  124. env, err := c.get("/me/collections", colls)
  125. if err != nil {
  126. return nil, err
  127. }
  128. var ok bool
  129. if colls, ok = env.Data.(*[]Collection); !ok {
  130. return nil, fmt.Errorf("Wrong data returned from API.")
  131. }
  132. status := env.Code
  133. if status != http.StatusOK {
  134. if c.isNotLoggedIn(status) {
  135. return nil, fmt.Errorf("Not authenticated.")
  136. }
  137. return nil, fmt.Errorf("Problem getting collections: %d. %v\n", status, err)
  138. }
  139. return colls, nil
  140. }
  141. // DeleteCollection permanently deletes a collection and makes any posts on it
  142. // anonymous.
  143. //
  144. // See https://developers.write.as/docs/api/#delete-a-collection.
  145. func (c *Client) DeleteCollection(alias string) error {
  146. endpoint := "/collections/" + alias
  147. env, err := c.delete(endpoint, nil /* data */)
  148. if err != nil {
  149. return err
  150. }
  151. status := env.Code
  152. switch status {
  153. case http.StatusNoContent:
  154. return nil
  155. case http.StatusUnauthorized:
  156. return fmt.Errorf("Not authenticated.")
  157. case http.StatusBadRequest:
  158. return fmt.Errorf("Bad request: %s", env.ErrorMessage)
  159. default:
  160. return fmt.Errorf("Problem deleting collection: %d. %s\n", status, env.ErrorMessage)
  161. }
  162. }