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.

135 lines
4.6 KiB

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