Go client for the Write.as API https://developers.write.as
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

63 行
1.5 KiB

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