Client for the pixeldrain API. Used by pixeldrain itself for tranferring data between the web UI and API server. And for rendering JSON responses
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
5.3 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. package pixelapi
  2. import (
  3. "net/url"
  4. "strconv"
  5. "time"
  6. "github.com/gocql/gocql"
  7. )
  8. // UserInfo contains information about the logged in user
  9. type UserInfo struct {
  10. Username string `json:"username"`
  11. Email string `json:"email"`
  12. Subscription SubscriptionType `json:"subscription"`
  13. IsAdmin bool `json:"is_admin"`
  14. }
  15. // UserSession is one user session
  16. type UserSession struct {
  17. AuthKey gocql.UUID `json:"auth_key"`
  18. CreationIP string `json:"creation_ip_address"`
  19. UserAgent string `json:"user_agent"`
  20. CreationTime time.Time `json:"creation_time"`
  21. LastUsedTime time.Time `json:"last_used_time"`
  22. }
  23. // UserRegister registers a new user on the Pixeldrain server. username and
  24. // password are always required. email is optional, but without it you will not
  25. // be able to reset your password in case you forget it. captcha depends on
  26. // whether reCaptcha is enabled on the Pixeldrain server, this can be checked
  27. // through the GetRecaptcha function.
  28. //
  29. // The register API can return multiple errors, which will be stored in the
  30. // Errors array. Check for len(Errors) == 0 to see if an error occurred. If err
  31. // != nil it means a connection error occurred
  32. func (p *PixelAPI) UserRegister(username, email, password, captcha string) (err error) {
  33. return p.form(
  34. "POST", "user/register",
  35. url.Values{
  36. "username": {username},
  37. "email": {email},
  38. "password": {password},
  39. "recaptcha_response": {captcha},
  40. },
  41. nil,
  42. )
  43. }
  44. // PostUserLogin logs a user in with the provided credentials. The response will
  45. // contain the returned API key. If saveKey is true the API key will also be
  46. // saved in the client and following requests with this client will be
  47. // autenticated
  48. func (p *PixelAPI) PostUserLogin(username, password string) (resp UserSession, err error) {
  49. return resp, p.form(
  50. "POST", "user/login",
  51. url.Values{"username": {username}, "password": {password}},
  52. &resp,
  53. )
  54. }
  55. // GetUser returns information about the logged in user. Requires an API key
  56. func (p *PixelAPI) GetUser() (resp UserInfo, err error) {
  57. return resp, p.jsonRequest("GET", "user", &resp)
  58. }
  59. // PostUserSession creates a new user sessions
  60. func (p *PixelAPI) PostUserSession() (resp UserSession, err error) {
  61. return resp, p.jsonRequest("POST", "user/session", &resp)
  62. }
  63. // GetUserSession lists all active user sessions
  64. func (p *PixelAPI) GetUserSession() (resp []UserSession, err error) {
  65. return resp, p.jsonRequest("GET", "user/session", &resp)
  66. }
  67. // DeleteUserSession destroys an API key so it can no longer be used to perform
  68. // actions
  69. func (p *PixelAPI) DeleteUserSession(key string) (err error) {
  70. return p.jsonRequest("DELETE", "user/session", nil)
  71. }
  72. // FileInfoSlice a collection of files which belong to a user
  73. type FileInfoSlice struct {
  74. Files []FileInfo `json:"files"`
  75. }
  76. // GetUserFiles gets files uploaded by a user
  77. func (p *PixelAPI) GetUserFiles() (resp FileInfoSlice, err error) {
  78. return resp, p.jsonRequest("GET", "user/files", &resp)
  79. }
  80. // ListInfoSlice is a collection of lists which belong to a user
  81. type ListInfoSlice struct {
  82. Lists []ListInfo `json:"lists"`
  83. }
  84. // GetUserLists gets lists created by a user
  85. func (p *PixelAPI) GetUserLists() (resp ListInfoSlice, err error) {
  86. return resp, p.jsonRequest("GET", "user/lists", &resp)
  87. }
  88. // PutUserPassword changes the user's password
  89. func (p *PixelAPI) PutUserPassword(oldPW, newPW string) (err error) {
  90. return p.form(
  91. "PUT", "user/password",
  92. url.Values{"old_password": {oldPW}, "new_password": {newPW}},
  93. nil,
  94. )
  95. }
  96. // PutUserEmailReset starts the e-mail change process. An email will be sent to
  97. // the new address to verify that it's real. Once the link in the e-mail is
  98. // clicked the key it contains can be sent to the API with UserEmailResetConfirm
  99. // and the change will be applied
  100. func (p *PixelAPI) PutUserEmailReset(email string, delete bool) (err error) {
  101. return p.form(
  102. "PUT", "user/email_reset",
  103. url.Values{"new_email": {email}, "delete": {strconv.FormatBool(delete)}},
  104. nil,
  105. )
  106. }
  107. // PutUserEmailResetConfirm finishes process of changing a user's e-mail address
  108. func (p *PixelAPI) PutUserEmailResetConfirm(key string) (err error) {
  109. return p.form(
  110. "PUT", "user/email_reset_confirm",
  111. url.Values{"key": {key}},
  112. nil,
  113. )
  114. }
  115. // PutUserPasswordReset starts the password reset process. An email will be sent
  116. // the user to verify that it really wanted to reset the password. Once the link
  117. // in the e-mail is clicked the key it contains can be sent to the API with
  118. // UserPasswordResetConfirm and a new password can be set
  119. func (p *PixelAPI) PutUserPasswordReset(email string, recaptchaResponse string) (err error) {
  120. return p.form(
  121. "PUT", "user/password_reset",
  122. url.Values{"email": {email}, "recaptcha_response": {recaptchaResponse}},
  123. nil,
  124. )
  125. }
  126. // PutUserPasswordResetConfirm finishes process of resetting a user's password.
  127. // If the key is valid the new_password parameter will be saved as the new
  128. // password
  129. func (p *PixelAPI) PutUserPasswordResetConfirm(key string, newPassword string) (err error) {
  130. return p.form(
  131. "PUT", "user/password_reset_confirm",
  132. url.Values{"key": {key}, "new_password": {newPassword}},
  133. nil,
  134. )
  135. }
  136. // PutUserUsername changes the user's username.
  137. func (p *PixelAPI) PutUserUsername(username string) (err error) {
  138. return p.form(
  139. "PUT", "user/username",
  140. url.Values{"new_username": {username}},
  141. nil,
  142. )
  143. }