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.

69 line
1.6 KiB

  1. package writeas
  2. import (
  3. "fmt"
  4. "net/http"
  5. "time"
  6. )
  7. type (
  8. // AuthUser represents a just-authenticated user. It contains information
  9. // that'll only be returned once (now) per user session.
  10. AuthUser struct {
  11. AccessToken string `json:"access_token,omitempty"`
  12. Password string `json:"password,omitempty"`
  13. User *User `json:"user"`
  14. }
  15. // User represents a registered Write.as user.
  16. User struct {
  17. Username string `json:"username"`
  18. Email string `json:"email"`
  19. Created time.Time `json:"created"`
  20. // Optional properties
  21. Subscription *UserSubscription `json:"subscription"`
  22. }
  23. // UserSubscription contains information about a user's Write.as
  24. // subscription.
  25. UserSubscription struct {
  26. Name string `json:"name"`
  27. Begin time.Time `json:"begin"`
  28. End time.Time `json:"end"`
  29. AutoRenew bool `json:"auto_renew"`
  30. Active bool `json:"is_active"`
  31. Delinquent bool `json:"is_delinquent"`
  32. }
  33. )
  34. // GetMe retrieves the authenticated User's information.
  35. // See: https://developers.write.as/docs/api/#retrieve-authenticated-user
  36. func (c *Client) GetMe(verbose bool) (*User, error) {
  37. if c.Token() == "" {
  38. return nil, fmt.Errorf("Unable to get user; no access token given.")
  39. }
  40. params := ""
  41. if verbose {
  42. params = "?verbose=true"
  43. }
  44. env, err := c.get("/me"+params, nil)
  45. if err != nil {
  46. return nil, err
  47. }
  48. status := env.Code
  49. if status == http.StatusUnauthorized {
  50. return nil, fmt.Errorf("invalid or expired token")
  51. }
  52. var u *User
  53. var ok bool
  54. if u, ok = env.Data.(*User); !ok {
  55. return nil, fmt.Errorf("Wrong data returned from API.")
  56. }
  57. return u, nil
  58. }