add login/register forms. Restructure pixelapi

This commit is contained in:
2018-06-20 23:47:47 +02:00
parent 75197310bf
commit ffa9fb3395
18 changed files with 292 additions and 228 deletions

View File

@@ -1,10 +1,7 @@
package pixelapi
import (
"encoding/json"
"io"
"github.com/Fornaxian/log"
)
// GetFile makes a file download request and returns a readcloser. Don't forget
@@ -29,18 +26,11 @@ type FileInfo struct {
}
// GetFileInfo gets the FileInfo from the pixeldrain API
func (p *PixelAPI) GetFileInfo(id string) *FileInfo {
body, err := getString(p.apiEndpoint + "/file/" + id + "/info")
func (p *PixelAPI) GetFileInfo(id string) (resp *FileInfo, err *Error) {
resp = &FileInfo{}
err = getJSON(p.apiEndpoint+"/file/"+id+"/info", resp)
if err != nil {
log.Error("req failed: %v", err)
return nil
return nil, err
}
var fileInfo FileInfo
err = json.Unmarshal([]byte(body), &fileInfo)
if err != nil {
log.Error("unmarshal failed: %v. json: %s", err, body)
return nil
}
return &fileInfo
return resp, nil
}

View File

@@ -1,9 +1,5 @@
package pixelapi
import (
"encoding/json"
)
// API error constants
const (
ListNotFoundError = "list_not_found"
@@ -11,7 +7,6 @@ const (
// List information object from the pixeldrain API
type List struct {
Error *ErrorResponse
Success bool `json:"success"`
ID string `json:"id"`
Title string `json:"title"`
@@ -32,22 +27,11 @@ 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) *List {
var list = &List{}
body, err := getString(p.apiEndpoint + "/list/" + id)
func (p *PixelAPI) GetList(id string) (resp *List, err *Error) {
resp = &List{}
err = getJSON(p.apiEndpoint+"/list/"+id, resp)
if err != nil {
list.Error = errorResponseFromError(err)
return list
return nil, err
}
err = json.Unmarshal([]byte(body), list)
if err != nil {
list.Error = errorResponseFromError(err)
return list
}
if !list.Success {
list.Error = errorResponseFromJSON(body)
}
return list
return resp, nil
}

13
pixelapi/misc.go Normal file
View File

@@ -0,0 +1,13 @@
package pixelapi
type Recaptcha struct {
SiteKey string `json:"site_key"`
}
func (p *PixelAPI) GetRecaptcha() (resp *Recaptcha, err *Error) {
err = getJSON(p.apiEndpoint+"/misc/recpatcha", resp)
if err != nil {
return nil, err
}
return resp, nil
}

View File

@@ -1,9 +1,141 @@
package pixelapi
import (
"encoding/json"
"io"
"io/ioutil"
"net/http"
"github.com/Fornaxian/log"
)
// PixelAPI is the Pixeldrain API client
type PixelAPI struct {
apiEndpoint string
}
// New creates a new Pixeldrain API client to query the Pixeldrain API with
func New(apiEndpoint string) *PixelAPI {
return &PixelAPI{apiEndpoint}
}
type Error struct {
ReqError bool
Success bool `json:"success"`
Value string `json:"value"`
Message string `json:"message"`
}
func (e Error) Error() string { return e.Value }
func errorResponseFromJSON(j string) *Error {
var r = &Error{}
var err = json.Unmarshal([]byte(j), r)
if err != nil {
r.Success = false
r.ReqError = true
r.Value = err.Error()
}
return r
}
func errorResponseFromError(e error) *Error {
var r = &Error{}
r.Success = false
r.ReqError = true
r.Value = e.Error()
return r
}
func getJSON(url string, target interface{}) *Error {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return &Error{
ReqError: true,
Success: false,
Value: err.Error(),
Message: err.Error(),
}
}
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return &Error{
ReqError: true,
Success: false,
Value: err.Error(),
Message: err.Error(),
}
}
defer resp.Body.Close()
var jdec = json.NewDecoder(resp.Body)
// Test for client side and server side errors
if resp.StatusCode >= 400 {
var errResp = &Error{
ReqError: false,
}
err = jdec.Decode(&errResp)
if err != nil {
log.Error("Can't decode this: %v", err)
return &Error{
ReqError: true,
Success: false,
Value: err.Error(),
Message: err.Error(),
}
}
return errResp
}
err = jdec.Decode(target)
if 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 nil
}
func getString(url string) (string, error) {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return "", err
}
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
bodyBytes, err := ioutil.ReadAll(resp.Body)
return string(bodyBytes), err
}
func getRaw(url string) (io.ReadCloser, error) {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
return resp.Body, err
}

View File

@@ -1,70 +0,0 @@
package pixelapi
import (
"encoding/json"
"io"
"io/ioutil"
"net/http"
)
type ErrorResponse struct {
ReqError bool
Success bool `json:"success"`
Value string `json:"value"`
Message *string `json:"message"`
ID *string `json:"id"`
}
func errorResponseFromJSON(j string) *ErrorResponse {
var r = &ErrorResponse{}
var err = json.Unmarshal([]byte(j), r)
if err != nil {
r.Success = false
r.ReqError = true
r.Value = err.Error()
}
return r
}
func errorResponseFromError(e error) *ErrorResponse {
var r = &ErrorResponse{}
r.Success = false
r.ReqError = true
r.Value = e.Error()
return r
}
func getString(url string) (string, error) {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return "", err
}
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
bodyBytes, err := ioutil.ReadAll(resp.Body)
return string(bodyBytes), err
}
func getRaw(url string) (io.ReadCloser, error) {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
return resp.Body, err
}