Transfer code from API repo to own repo

This commit is contained in:
2021-03-09 18:11:23 +01:00
parent 0e5d737209
commit 5c74f774ff
12 changed files with 473 additions and 1 deletions

135
pixelapi/user.go Normal file
View File

@@ -0,0 +1,135 @@
package pixelapi
import (
"net/url"
"strconv"
"fornaxian.tech/pixeldrain_server/api/restapi/apitype"
)
// UserRegister registers a new user on the Pixeldrain server. username and
// password are always required. email is optional, but without it you will not
// be able to reset your password in case you forget it. captcha depends on
// whether reCaptcha is enabled on the Pixeldrain server, this can be checked
// through the GetRecaptcha function.
//
// The register API can return multiple errors, which will be stored in the
// Errors array. Check for len(Errors) == 0 to see if an error occurred. If err
// != nil it means a connection error occurred
func (p *PixelAPI) UserRegister(username, email, password, captcha string) (err error) {
return p.form(
"POST", "user/register",
url.Values{
"username": {username},
"email": {email},
"password": {password},
"recaptcha_response": {captcha},
},
nil,
)
}
// PostUserLogin logs a user in with the provided credentials. The response will
// contain the returned API key. If saveKey is true the API key will also be
// saved in the client and following requests with this client will be
// autenticated
func (p *PixelAPI) PostUserLogin(username, password string) (resp apitype.UserSession, err error) {
return resp, p.form(
"POST", "user/login",
url.Values{"username": {username}, "password": {password}},
&resp,
)
}
// GetUser returns information about the logged in user. Requires an API key
func (p *PixelAPI) GetUser() (resp apitype.UserInfo, err error) {
return resp, p.jsonRequest("GET", "user", &resp)
}
// PostUserSession creates a new user sessions
func (p *PixelAPI) PostUserSession() (resp apitype.UserSession, err error) {
return resp, p.jsonRequest("POST", "user/session", &resp)
}
// GetUserSession lists all active user sessions
func (p *PixelAPI) GetUserSession() (resp []apitype.UserSession, err error) {
return resp, p.jsonRequest("GET", "user/session", &resp)
}
// DeleteUserSession destroys an API key so it can no longer be used to perform
// actions
func (p *PixelAPI) DeleteUserSession(key string) (err error) {
return p.jsonRequest("DELETE", "user/session", nil)
}
// GetUserFiles gets files uploaded by a user
func (p *PixelAPI) GetUserFiles() (resp apitype.FileInfoSlice, err error) {
return resp, p.jsonRequest("GET", "user/files", &resp)
}
// GetUserLists gets lists created by a user
func (p *PixelAPI) GetUserLists() (resp apitype.ListInfoSlice, err error) {
return resp, p.jsonRequest("GET", "user/lists", &resp)
}
// PutUserPassword changes the user's password
func (p *PixelAPI) PutUserPassword(oldPW, newPW string) (err error) {
return p.form(
"PUT", "user/password",
url.Values{"old_password": {oldPW}, "new_password": {newPW}},
nil,
)
}
// PutUserEmailReset starts the e-mail change process. An email will be sent to
// the new address to verify that it's real. Once the link in the e-mail is
// clicked the key it contains can be sent to the API with UserEmailResetConfirm
// and the change will be applied
func (p *PixelAPI) PutUserEmailReset(email string, delete bool) (err error) {
return p.form(
"PUT", "user/email_reset",
url.Values{"new_email": {email}, "delete": {strconv.FormatBool(delete)}},
nil,
)
}
// PutUserEmailResetConfirm finishes process of changing a user's e-mail address
func (p *PixelAPI) PutUserEmailResetConfirm(key string) (err error) {
return p.form(
"PUT", "user/email_reset_confirm",
url.Values{"key": {key}},
nil,
)
}
// PutUserPasswordReset starts the password reset process. An email will be sent
// the user to verify that it really wanted to reset the password. Once the link
// in the e-mail is clicked the key it contains can be sent to the API with
// UserPasswordResetConfirm and a new password can be set
func (p *PixelAPI) PutUserPasswordReset(email string, recaptchaResponse string) (err error) {
return p.form(
"PUT", "user/password_reset",
url.Values{"email": {email}, "recaptcha_response": {recaptchaResponse}},
nil,
)
}
// PutUserPasswordResetConfirm finishes process of resetting a user's password.
// If the key is valid the new_password parameter will be saved as the new
// password
func (p *PixelAPI) PutUserPasswordResetConfirm(key string, newPassword string) (err error) {
return p.form(
"PUT", "user/password_reset_confirm",
url.Values{"key": {key}, "new_password": {newPassword}},
nil,
)
}
// PutUserUsername changes the user's username.
func (p *PixelAPI) PutUserUsername(username string) (err error) {
return p.form(
"PUT", "user/username",
url.Values{"new_username": {username}},
nil,
)
}