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.

186 lines
5.3 KiB

  1. package writeas
  2. import (
  3. "fmt"
  4. "net/http"
  5. "time"
  6. )
  7. type (
  8. // Post represents a published Write.as post, whether anonymous, owned by a
  9. // user, or part of a collection.
  10. Post struct {
  11. ID string `json:"id"`
  12. Slug string `json:"slug"`
  13. Token string `json:"token"`
  14. Font string `json:"appearance"`
  15. Language *string `json:"language"`
  16. RTL *bool `json:"rtl"`
  17. Listed bool `json:"listed"`
  18. Created time.Time `json:"created"`
  19. Title string `json:"title"`
  20. Content string `json:"body"`
  21. Views int64 `json:"views"`
  22. Tags []string `json:"tags"`
  23. Images []string `json:"images"`
  24. OwnerName string `json:"owner,omitempty"`
  25. Collection *Collection `json:"collection,omitempty"`
  26. }
  27. // OwnedPostParams are, together, fields only the original post author knows.
  28. OwnedPostParams struct {
  29. ID string `json:"-"`
  30. Token string `json:"token,omitempty"`
  31. }
  32. // PostParams holds values for creating or updating a post.
  33. PostParams struct {
  34. // Parameters only for updating
  35. OwnedPostParams
  36. // Parameters for creating or updating
  37. Title string `json:"title,omitempty"`
  38. Content string `json:"body,omitempty"`
  39. Font string `json:"font,omitempty"`
  40. IsRTL *bool `json:"rtl,omitempty"`
  41. Language *string `json:"lang,omitempty"`
  42. Crosspost []map[string]string `json:"crosspost,omitempty"`
  43. }
  44. // ClaimPostResult contains the post-specific result for a request to
  45. // associate a post to an account.
  46. ClaimPostResult struct {
  47. ID string `json:"id,omitempty"`
  48. Code int `json:"code,omitempty"`
  49. ErrorMessage string `json:"error_msg,omitempty"`
  50. Post *Post `json:"post,omitempty"`
  51. }
  52. )
  53. // GetPost retrieves a published post, returning the Post and any error (in
  54. // user-friendly form) that occurs. See
  55. // https://developer.write.as/docs/api/#retrieve-a-post.
  56. func (c *Client) GetPost(id string) (*Post, error) {
  57. p := &Post{}
  58. env, err := c.get(fmt.Sprintf("/posts/%s", id), p)
  59. if err != nil {
  60. return nil, err
  61. }
  62. var ok bool
  63. if p, ok = env.Data.(*Post); !ok {
  64. return nil, fmt.Errorf("Wrong data returned from API.")
  65. }
  66. status := env.Code
  67. if status == http.StatusOK {
  68. return p, nil
  69. } else if status == http.StatusNotFound {
  70. return nil, fmt.Errorf("Post not found.")
  71. } else if status == http.StatusGone {
  72. return nil, fmt.Errorf("Post unpublished.")
  73. }
  74. return nil, fmt.Errorf("Problem getting post: %s. %v\n", status, err)
  75. }
  76. // CreatePost publishes a new post, returning a user-friendly error if one comes
  77. // up. See https://developer.write.as/docs/api/#publish-a-post.
  78. func (c *Client) CreatePost(sp *PostParams) (*Post, error) {
  79. p := &Post{}
  80. env, err := c.post("/posts", sp, p)
  81. if err != nil {
  82. return nil, err
  83. }
  84. var ok bool
  85. if p, ok = env.Data.(*Post); !ok {
  86. return nil, fmt.Errorf("Wrong data returned from API.")
  87. }
  88. status := env.Code
  89. if status == http.StatusCreated {
  90. return p, nil
  91. } else if status == http.StatusBadRequest {
  92. return nil, fmt.Errorf("Bad request: %s", env.ErrorMessage)
  93. } else {
  94. return nil, fmt.Errorf("Problem getting post: %s. %v\n", status, err)
  95. }
  96. return p, nil
  97. }
  98. // UpdatePost updates a published post with the given PostParams. See
  99. // https://developer.write.as/docs/api/#update-a-post.
  100. func (c *Client) UpdatePost(sp *PostParams) (*Post, error) {
  101. p := &Post{}
  102. env, err := c.put(fmt.Sprintf("/posts/%s", sp.ID), sp, p)
  103. if err != nil {
  104. return nil, err
  105. }
  106. var ok bool
  107. if p, ok = env.Data.(*Post); !ok {
  108. return nil, fmt.Errorf("Wrong data returned from API.")
  109. }
  110. status := env.Code
  111. if status == http.StatusOK {
  112. return p, nil
  113. } else if c.isNotLoggedIn(status) {
  114. return nil, fmt.Errorf("Not authenticated.")
  115. } else if status == http.StatusBadRequest {
  116. return nil, fmt.Errorf("Bad request: %s", env.ErrorMessage)
  117. }
  118. return nil, fmt.Errorf("Problem getting post: %s. %v\n", status, err)
  119. }
  120. // DeletePost permanently deletes a published post. See
  121. // https://developer.write.as/docs/api/#delete-a-post.
  122. func (c *Client) DeletePost(sp *PostParams) error {
  123. env, err := c.delete(fmt.Sprintf("/posts/%s", sp.ID), map[string]string{
  124. "token": sp.Token,
  125. })
  126. if err != nil {
  127. return err
  128. }
  129. status := env.Code
  130. if status == http.StatusNoContent {
  131. return nil
  132. } else if c.isNotLoggedIn(status) {
  133. return fmt.Errorf("Not authenticated.")
  134. } else if status == http.StatusBadRequest {
  135. return fmt.Errorf("Bad request: %s", env.ErrorMessage)
  136. }
  137. return fmt.Errorf("Problem getting post: %s. %v\n", status, err)
  138. }
  139. // ClaimPosts associates anonymous posts with a user / account.
  140. // https://developer.write.as/docs/api/#claim-posts.
  141. func (c *Client) ClaimPosts(sp *[]OwnedPostParams) (*[]ClaimPostResult, error) {
  142. p := &[]ClaimPostResult{}
  143. env, err := c.put("/posts/claim", sp, p)
  144. if err != nil {
  145. return nil, err
  146. }
  147. var ok bool
  148. if p, ok = env.Data.(*[]ClaimPostResult); !ok {
  149. return nil, fmt.Errorf("Wrong data returned from API.")
  150. }
  151. status := env.Code
  152. if status == http.StatusOK {
  153. return p, nil
  154. } else if c.isNotLoggedIn(status) {
  155. return nil, fmt.Errorf("Not authenticated.")
  156. } else if status == http.StatusBadRequest {
  157. return nil, fmt.Errorf("Bad request: %s", env.ErrorMessage)
  158. } else {
  159. return nil, fmt.Errorf("Problem getting post: %s. %v\n", status, err)
  160. }
  161. // TODO: does this also happen with moving posts?
  162. return p, nil
  163. }