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

collection.go 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. }
  27. )
  28. // CreateCollection creates a new collection, returning a user-friendly error
  29. // if one comes up. Requires a Write.as subscription. See
  30. // https://developer.write.as/docs/api/#create-a-collection
  31. func (c *Client) CreateCollection(sp *CollectionParams) (*Collection, error) {
  32. p := &Collection{}
  33. env, err := c.post("/collections", sp, p)
  34. if err != nil {
  35. return nil, err
  36. }
  37. var ok bool
  38. if p, ok = env.Data.(*Collection); !ok {
  39. return nil, fmt.Errorf("Wrong data returned from API.")
  40. }
  41. status := env.Code
  42. if status != http.StatusCreated {
  43. if status == http.StatusBadRequest {
  44. return nil, fmt.Errorf("Bad request: %s", env.ErrorMessage)
  45. } else if status == http.StatusForbidden {
  46. return nil, fmt.Errorf("Casual or Pro user required.")
  47. } else if status == http.StatusConflict {
  48. return nil, fmt.Errorf("Collection name is already taken.")
  49. } else if status == http.StatusPreconditionFailed {
  50. return nil, fmt.Errorf("Reached max collection quota.")
  51. }
  52. return nil, fmt.Errorf("Problem getting post: %d. %v\n", status, err)
  53. }
  54. return p, nil
  55. }
  56. // GetCollection retrieves a collection, returning the Collection and any error
  57. // (in user-friendly form) that occurs. See
  58. // https://developer.write.as/docs/api/#retrieve-a-collection
  59. func (c *Client) GetCollection(alias string) (*Collection, error) {
  60. coll := &Collection{}
  61. env, err := c.get(fmt.Sprintf("/collections/%s", alias), coll)
  62. if err != nil {
  63. return nil, err
  64. }
  65. var ok bool
  66. if coll, ok = env.Data.(*Collection); !ok {
  67. return nil, fmt.Errorf("Wrong data returned from API.")
  68. }
  69. status := env.Code
  70. if status == http.StatusOK {
  71. return coll, nil
  72. } else if status == http.StatusNotFound {
  73. return nil, fmt.Errorf("Collection not found.")
  74. } else {
  75. return nil, fmt.Errorf("Problem getting collection: %d. %v\n", status, err)
  76. }
  77. }
  78. // GetCollectionPosts retrieves a collection's posts, returning the Posts
  79. // and any error (in user-friendly form) that occurs. See
  80. // https://developer.write.as/docs/api/#retrieve-collection-posts
  81. func (c *Client) GetCollectionPosts(alias string) (*[]Post, error) {
  82. coll := &Collection{}
  83. env, err := c.get(fmt.Sprintf("/collections/%s/posts", alias), coll)
  84. if err != nil {
  85. return nil, err
  86. }
  87. var ok bool
  88. if coll, ok = env.Data.(*Collection); !ok {
  89. return nil, fmt.Errorf("Wrong data returned from API.")
  90. }
  91. status := env.Code
  92. if status == http.StatusOK {
  93. return coll.Posts, nil
  94. } else if status == http.StatusNotFound {
  95. return nil, fmt.Errorf("Collection not found.")
  96. } else {
  97. return nil, fmt.Errorf("Problem getting collection: %d. %v\n", status, err)
  98. }
  99. }
  100. // GetUserCollections retrieves the authenticated user's collections.
  101. // See https://developers.write.as/docs/api/#retrieve-user-39-s-collections
  102. func (c *Client) GetUserCollections() (*[]Collection, error) {
  103. colls := &[]Collection{}
  104. env, err := c.get("/me/collections", colls)
  105. if err != nil {
  106. return nil, err
  107. }
  108. var ok bool
  109. if colls, ok = env.Data.(*[]Collection); !ok {
  110. return nil, fmt.Errorf("Wrong data returned from API.")
  111. }
  112. status := env.Code
  113. if status != http.StatusOK {
  114. if c.isNotLoggedIn(status) {
  115. return nil, fmt.Errorf("Not authenticated.")
  116. }
  117. return nil, fmt.Errorf("Problem getting collections: %d. %v\n", status, err)
  118. }
  119. return colls, nil
  120. }
  121. // DeleteCollection permanently deletes a collection and makes any posts on it
  122. // anonymous.
  123. //
  124. // See https://developers.write.as/docs/api/#delete-a-collection.
  125. func (c *Client) DeleteCollection(alias string) error {
  126. endpoint := "/collections/" + alias
  127. env, err := c.delete(endpoint, nil /* data */)
  128. if err != nil {
  129. return err
  130. }
  131. status := env.Code
  132. switch status {
  133. case http.StatusNoContent:
  134. return nil
  135. case http.StatusUnauthorized:
  136. return fmt.Errorf("Not authenticated.")
  137. case http.StatusBadRequest:
  138. return fmt.Errorf("Bad request: %s", env.ErrorMessage)
  139. default:
  140. return fmt.Errorf("Problem deleting collection: %d. %s\n", status, env.ErrorMessage)
  141. }
  142. }