bunch of API changes

This commit is contained in:
2020-05-05 22:03:34 +02:00
parent e89db822cc
commit e811d0a54f
22 changed files with 12289 additions and 352 deletions

View File

@@ -10,7 +10,7 @@ type IsAdmin struct {
// UserIsAdmin returns if the logged in user is an admin user
func (p *PixelAPI) UserIsAdmin() (resp IsAdmin, err error) {
err = p.jsonRequest("GET", p.apiEndpoint+"/admin/is_admin", &resp)
err = p.jsonRequest("GET", p.apiEndpoint+"/admin/is_admin", &resp, false)
if err != nil {
return resp, err
}
@@ -31,16 +31,16 @@ type AdminGlobals struct {
// AdminGetGlobals returns if the logged in user is an admin user
func (p *PixelAPI) AdminGetGlobals() (resp AdminGlobals, err error) {
if err = p.jsonRequest("GET", p.apiEndpoint+"/admin/globals", &resp); err != nil {
if err = p.jsonRequest("GET", p.apiEndpoint+"/admin/globals", &resp, false); err != nil {
return resp, err
}
return resp, nil
}
// AdminSetGlobals returns if the logged in user is an admin user
func (p *PixelAPI) AdminSetGlobals(key, value string) (resp SuccessResponse, err error) {
func (p *PixelAPI) AdminSetGlobals(key, value string) (err error) {
var form = url.Values{}
form.Add("key", key)
form.Add("value", value)
return resp, p.form("POST", p.apiEndpoint+"/admin/globals", form, &resp, true)
return p.form("POST", p.apiEndpoint+"/admin/globals", form, nil, true)
}

View File

@@ -35,9 +35,10 @@ type FileInfo struct {
// GetFileInfo gets the FileInfo from the pixeldrain API
func (p *PixelAPI) GetFileInfo(id string) (resp FileInfo, err error) {
return resp, p.jsonRequest("GET", p.apiEndpoint+"/file/"+id+"/info", &resp)
return resp, p.jsonRequest("GET", p.apiEndpoint+"/file/"+id+"/info", &resp, false)
}
// PostFileView adds a view to a file
func (p *PixelAPI) PostFileView(id, viewtoken string) (err error) {
vals := url.Values{}
vals.Set("token", viewtoken)

View File

@@ -27,5 +27,5 @@ type ListFile struct {
// GetList get a List from the pixeldrain API. Errors will be available through
// List.Error. Standard error checks apply.
func (p *PixelAPI) GetList(id string) (resp List, err error) {
return resp, p.jsonRequest("GET", p.apiEndpoint+"/list/"+id, &resp)
return resp, p.jsonRequest("GET", p.apiEndpoint+"/list/"+id, &resp, false)
}

View File

@@ -8,12 +8,12 @@ type Recaptcha struct {
// GetRecaptcha gets the reCaptcha site key from the pixelapi server. If
// reCaptcha is disabled the key will be empty
func (p *PixelAPI) GetRecaptcha() (resp Recaptcha, err error) {
return resp, p.jsonRequest("GET", p.apiEndpoint+"/misc/recaptcha", &resp)
return resp, p.jsonRequest("GET", p.apiEndpoint+"/misc/recaptcha", &resp, false)
}
// GetMiscViewToken requests a viewtoken from the server. The viewtoken is valid
// for a limited amount of time and can be used to add views to a file.
// Viewtokens can only be requested from localhost
func (p *PixelAPI) GetMiscViewToken() (resp string, err error) {
return resp, p.jsonRequest("GET", p.apiEndpoint+"/misc/viewtoken", &resp)
return resp, p.jsonRequest("GET", p.apiEndpoint+"/misc/viewtoken", &resp, false)
}

View File

@@ -26,59 +26,49 @@ func New(apiEndpoint string) *PixelAPI {
return &PixelAPI{apiEndpoint: apiEndpoint}
}
// Error is either an error that occurred during the API request
// (ReqError = true), or an error that was returned from the Pixeldrain API
// (ReqError = false). The Success field is also returned by the Pixeldrain API,
// but since this is struct represents an Error it will usually be false.
//
// When ReqError is true the Value field will contain a Go error message. When
// it's false it will contain a Pixeldrain API error code.
// Standard response types
// Error is an error returned by the pixeldrain API. If the request failed
// before it could reach the API the error will be on a different type
type Error struct {
ReqError bool
Success bool `json:"success"`
Value string `json:"value"`
Message string `json:"message"`
Extra interface{} `json:"extra,omitempty"`
Status int `json:"-"` // One of the http.Status types
Success bool `json:"success"`
StatusCode string `json:"value"`
Message string `json:"message"`
// In case of the multiple_errors code this array will be populated with
// more errors
Errors []Error `json:"errors,omitempty"`
// Metadata regarding the error
Extra map[string]interface{} `json:"extra,omitempty"`
}
func (e Error) Error() string { return e.Value }
func (e Error) Error() string { return e.StatusCode }
// SuccessResponse is a generic response the API returns when the action was
// successful and there is nothing interesting to report
type SuccessResponse struct {
Success bool `json:"success"`
Message string `json:"message"`
}
func (p *PixelAPI) jsonRequest(method, url string, target interface{}) error {
req, err := http.NewRequest(method, url, nil)
if err != nil {
return Error{
ReqError: true,
Success: false,
Value: err.Error(),
Message: err.Error(),
}
}
func (p *PixelAPI) do(r *http.Request) (*http.Response, error) {
if p.APIKey != "" {
req.SetBasicAuth("", p.APIKey)
r.SetBasicAuth("", p.APIKey)
}
if p.RealIP != "" {
req.Header.Set("X-Real-IP", p.RealIP)
r.Header.Set("X-Real-IP", p.RealIP)
}
resp, err := client.Do(req)
return client.Do(r)
}
func (p *PixelAPI) jsonRequest(method, url string, target interface{}, multiErr bool) error {
req, err := http.NewRequest(method, url, nil)
if err != nil {
return Error{
ReqError: true,
Success: false,
Value: err.Error(),
Message: err.Error(),
}
return err
}
resp, err := p.do(req)
if err != nil {
return err
}
defer resp.Body.Close()
return parseJSONResponse(resp, target, true)
return parseJSONResponse(resp, target, multiErr)
}
func (p *PixelAPI) getString(url string) (string, error) {
@@ -86,14 +76,7 @@ func (p *PixelAPI) getString(url string) (string, error) {
if err != nil {
return "", err
}
if p.APIKey != "" {
req.SetBasicAuth("", p.APIKey)
}
if p.RealIP != "" {
req.Header.Set("X-Real-IP", p.RealIP)
}
resp, err := client.Do(req)
resp, err := p.do(req)
if err != nil {
return "", err
}
@@ -110,14 +93,7 @@ func (p *PixelAPI) getRaw(url string) (io.ReadCloser, error) {
if err != nil {
return nil, err
}
if p.APIKey != "" {
req.SetBasicAuth("", p.APIKey)
}
if p.RealIP != "" {
req.Header.Set("X-Real-IP", p.RealIP)
}
resp, err := client.Do(req)
resp, err := p.do(req)
if err != nil {
return nil, err
}
@@ -130,56 +106,29 @@ func (p *PixelAPI) form(
url string,
vals url.Values,
target interface{},
catchErrors bool,
multiErr bool,
) error {
req, err := http.NewRequest(method, url, strings.NewReader(vals.Encode()))
if err != nil {
return Error{
ReqError: true,
Success: false,
Value: err.Error(),
Message: err.Error(),
}
return err
}
if p.APIKey != "" {
req.SetBasicAuth("", p.APIKey)
}
if p.RealIP != "" {
req.Header.Set("X-Real-IP", p.RealIP)
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
resp, err := client.Do(req)
resp, err := p.do(req)
if err != nil {
return Error{
ReqError: true,
Success: false,
Value: err.Error(),
Message: err.Error(),
}
return err
}
defer resp.Body.Close()
return parseJSONResponse(resp, target, catchErrors)
return parseJSONResponse(resp, target, multiErr)
}
func parseJSONResponse(resp *http.Response, target interface{}, catchErrors bool) error {
var err error
func parseJSONResponse(resp *http.Response, target interface{}, multiErr bool) (err error) {
// Test for client side and server side errors
if catchErrors && resp.StatusCode >= 400 {
var errResp = Error{
ReqError: false,
}
if resp.StatusCode >= 400 {
errResp := Error{Status: resp.StatusCode}
if err = json.NewDecoder(resp.Body).Decode(&errResp); err != nil {
log.Error("Can't decode this: %v", err)
return Error{
ReqError: true,
Success: false,
Value: err.Error(),
Message: err.Error(),
}
return err
}
return errResp
}
@@ -191,12 +140,8 @@ func parseJSONResponse(resp *http.Response, target interface{}, catchErrors bool
if err = json.NewDecoder(resp.Body).Decode(target); err != nil {
r, _ := ioutil.ReadAll(resp.Body)
log.Error("Can't decode this: %v. %s", err, r)
return Error{
ReqError: true,
Success: false,
Value: err.Error(),
Message: err.Error(),
}
return err
}
return nil
}

View File

@@ -6,15 +6,6 @@ import (
"strconv"
)
// Registration is the response to the UserRegister API. 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
type Registration struct {
Success bool `json:"success"`
Message string `json:"message,omitempty"`
Errors []Error `json:"errors,omitempty"`
}
// 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
@@ -24,18 +15,17 @@ type Registration struct {
// 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) (resp *Registration, err error) {
resp = &Registration{}
var form = url.Values{}
form.Add("username", username)
form.Add("email", email)
form.Add("password", password)
form.Add("recaptcha_response", captcha)
err = p.form("POST", p.apiEndpoint+"/user/register", form, resp, false)
if err != nil {
return nil, err
}
return resp, nil
func (p *PixelAPI) UserRegister(username, email, password, captcha string) (err error) {
return p.form(
"POST", p.apiEndpoint+"/user/register",
url.Values{
"username": {username},
"email": {email},
"password": {password},
"recaptcha_response": {captcha},
},
nil, true,
)
}
// Login is the success response to the `user/login` API
@@ -48,14 +38,12 @@ type Login struct {
// 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) UserLogin(username, password string, saveKey bool) (resp *Login, err error) {
resp = &Login{}
func (p *PixelAPI) UserLogin(username, password string, saveKey bool) (resp Login, err error) {
var form = url.Values{}
form.Add("username", username)
form.Add("password", password)
err = p.form("POST", p.apiEndpoint+"/user/login", form, resp, true)
if err != nil {
return nil, err
if err = p.form("POST", p.apiEndpoint+"/user/login", form, &resp, true); err != nil {
return resp, err
}
if saveKey {
p.APIKey = resp.APIKey
@@ -71,61 +59,48 @@ type UserInfo struct {
}
// UserInfo returns information about the logged in user. Requires an API key
func (p *PixelAPI) UserInfo() (resp *UserInfo, err error) {
resp = &UserInfo{}
err = p.jsonRequest("GET", p.apiEndpoint+"/user", resp)
if err != nil {
return nil, err
}
return resp, nil
func (p *PixelAPI) UserInfo() (resp UserInfo, err error) {
return resp, p.jsonRequest("GET", p.apiEndpoint+"/user", &resp, false)
}
// UserSessionDestroy destroys an API key so it can no longer be used to perform
// actions
func (p *PixelAPI) UserSessionDestroy(key string) (err error) {
return p.jsonRequest("DELETE", p.apiEndpoint+"/user/session", nil)
return p.jsonRequest("DELETE", p.apiEndpoint+"/user/session", nil, false)
}
// UserFiles is a list of files uploaded by a user
type UserFiles struct {
Success bool `json:"success"`
Files []FileInfo `json:"files"`
}
func (p *PixelAPI) UserFiles(page, limit int) (resp *UserFiles, err error) {
resp = &UserFiles{Files: make([]FileInfo, 0)}
err = p.jsonRequest(
"GET",
fmt.Sprintf("%s/user/files?page=%d&limit=%d", p.apiEndpoint, page, limit),
resp,
// UserFiles gets files uploaded by a user
func (p *PixelAPI) UserFiles(page, limit int) (resp UserFiles, err error) {
return resp, p.jsonRequest(
"GET", fmt.Sprintf("%s/user/files?page=%d&limit=%d", p.apiEndpoint, page, limit), &resp, false,
)
if err != nil {
return nil, err
}
return resp, nil
}
// UserLists is a list of lists created by a user
type UserLists struct {
Success bool `json:"success"`
Lists []List `json:"lists"`
}
func (p *PixelAPI) UserLists(page, limit int) (resp *UserLists, err error) {
resp = &UserLists{Lists: make([]List, 0)}
if err = p.jsonRequest(
"GET",
fmt.Sprintf("%s/user/lists?page=%d&limit=%d", p.apiEndpoint, page, limit),
resp,
); err != nil {
return nil, err
}
return resp, nil
// UserLists gets lists created by a user
func (p *PixelAPI) UserLists(page, limit int) (resp UserLists, err error) {
return resp, p.jsonRequest(
"GET", fmt.Sprintf("%s/user/lists?page=%d&limit=%d", p.apiEndpoint, page, limit), &resp, false,
)
}
// UserPasswordSet changes the user's password
func (p *PixelAPI) UserPasswordSet(oldPW, newPW string) (err error) {
var form = url.Values{}
form.Add("old_password", oldPW)
form.Add("new_password", newPW)
return p.form("PUT", p.apiEndpoint+"/user/password", form, nil, true)
return p.form(
"PUT", p.apiEndpoint+"/user/password",
url.Values{"old_password": {oldPW}, "new_password": {newPW}}, nil, true,
)
}
// UserEmailReset starts the e-mail change process. An email will be sent to the
@@ -141,9 +116,10 @@ func (p *PixelAPI) UserEmailReset(email string, delete bool) (err error) {
// UserEmailResetConfirm finishes process of changing a user's e-mail address
func (p *PixelAPI) UserEmailResetConfirm(key string) (err error) {
var form = url.Values{}
form.Add("key", key)
return p.form("PUT", p.apiEndpoint+"/user/email_reset_cofirm", form, nil, true)
return p.form(
"PUT", p.apiEndpoint+"/user/email_reset_confirm",
url.Values{"key": {key}}, nil, true,
)
}
// UserPasswordReset starts the password reset process. An email will be sent