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.

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