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.

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