Go client for the Write.as API https://developers.write.as
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

163 lines
4.7 KiB

  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://developer.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://developer.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://developer.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. // GetUserCollections retrieves the authenticated user's collections.
  102. // See https://developers.write.as/docs/api/#retrieve-user-39-s-collections
  103. func (c *Client) GetUserCollections() (*[]Collection, error) {
  104. colls := &[]Collection{}
  105. env, err := c.get("/me/collections", colls)
  106. if err != nil {
  107. return nil, err
  108. }
  109. var ok bool
  110. if colls, ok = env.Data.(*[]Collection); !ok {
  111. return nil, fmt.Errorf("Wrong data returned from API.")
  112. }
  113. status := env.Code
  114. if status != http.StatusOK {
  115. if c.isNotLoggedIn(status) {
  116. return nil, fmt.Errorf("Not authenticated.")
  117. }
  118. return nil, fmt.Errorf("Problem getting collections: %d. %v\n", status, err)
  119. }
  120. return colls, nil
  121. }
  122. // DeleteCollection permanently deletes a collection and makes any posts on it
  123. // anonymous.
  124. //
  125. // See https://developers.write.as/docs/api/#delete-a-collection.
  126. func (c *Client) DeleteCollection(alias string) error {
  127. endpoint := "/collections/" + alias
  128. env, err := c.delete(endpoint, nil /* data */)
  129. if err != nil {
  130. return err
  131. }
  132. status := env.Code
  133. switch status {
  134. case http.StatusNoContent:
  135. return nil
  136. case http.StatusUnauthorized:
  137. return fmt.Errorf("Not authenticated.")
  138. case http.StatusBadRequest:
  139. return fmt.Errorf("Bad request: %s", env.ErrorMessage)
  140. default:
  141. return fmt.Errorf("Problem deleting collection: %d. %s\n", status, env.ErrorMessage)
  142. }
  143. }