Go client for the Write.as API https://developers.write.as
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

188 рядки
5.2 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 struct {
  45. ID string `json:"id,omitempty"`
  46. Code int `json:"code,omitempty"`
  47. ErrorMessage string `json:"error_msg,omitempty"`
  48. Post *Post `json:"post,omitempty"`
  49. }
  50. )
  51. // GetPost retrieves a published post, returning the Post and any error (in
  52. // user-friendly form) that occurs. See
  53. // https://writeas.github.io/docs/#retrieve-a-post.
  54. func (c *Client) GetPost(id string) (*Post, error) {
  55. p := &Post{}
  56. env, err := c.get(fmt.Sprintf("/posts/%s", id), p)
  57. if err != nil {
  58. return nil, err
  59. }
  60. var ok bool
  61. if p, ok = env.Data.(*Post); !ok {
  62. return nil, fmt.Errorf("Wrong data returned from API.")
  63. }
  64. status := env.Code
  65. if status == http.StatusOK {
  66. return p, nil
  67. } else if status == http.StatusNotFound {
  68. return nil, fmt.Errorf("Post not found.")
  69. } else if status == http.StatusGone {
  70. return nil, fmt.Errorf("Post unpublished.")
  71. } else {
  72. return nil, fmt.Errorf("Problem getting post: %s. %v\n", status, err)
  73. }
  74. return p, nil
  75. }
  76. // CreatePost publishes a new post, returning a user-friendly error if one comes
  77. // up. See https://writeas.github.io/docs/#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://writeas.github.io/docs/#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. } else {
  118. return nil, fmt.Errorf("Problem getting post: %s. %v\n", status, err)
  119. }
  120. return p, nil
  121. }
  122. // DeletePost permanently deletes a published post. See
  123. // https://writeas.github.io/docs/#delete-a-post.
  124. func (c *Client) DeletePost(sp *PostParams) error {
  125. env, err := c.delete(fmt.Sprintf("/posts/%s", sp.ID), map[string]string{
  126. "token": sp.Token,
  127. })
  128. if err != nil {
  129. return err
  130. }
  131. status := env.Code
  132. if status == http.StatusNoContent {
  133. return nil
  134. } else if c.isNotLoggedIn(status) {
  135. return fmt.Errorf("Not authenticated.")
  136. } else if status == http.StatusBadRequest {
  137. return fmt.Errorf("Bad request: %s", env.ErrorMessage)
  138. }
  139. return fmt.Errorf("Problem getting post: %s. %v\n", status, err)
  140. }
  141. // ClaimPosts associates anonymous posts with a user / account.
  142. // https://writeas.github.io/docs/#claim-posts.
  143. func (c *Client) ClaimPosts(sp *[]OwnedPostParams) (*[]ClaimPostResult, error) {
  144. p := &[]ClaimPostResult{}
  145. env, err := c.put("/posts/claim", sp, p)
  146. if err != nil {
  147. return nil, err
  148. }
  149. var ok bool
  150. if p, ok = env.Data.(*[]ClaimPostResult); !ok {
  151. return nil, fmt.Errorf("Wrong data returned from API.")
  152. }
  153. status := env.Code
  154. if status == http.StatusOK {
  155. return p, nil
  156. } else if c.isNotLoggedIn(status) {
  157. return nil, fmt.Errorf("Not authenticated.")
  158. } else if status == http.StatusBadRequest {
  159. return nil, fmt.Errorf("Bad request: %s", env.ErrorMessage)
  160. } else {
  161. return nil, fmt.Errorf("Problem getting post: %s. %v\n", status, err)
  162. }
  163. // TODO: does this also happen with moving posts?
  164. return p, nil
  165. }