Working download photo method
Signed-off-by: Kris Nóva <kris@nivenly.com>
This commit is contained in:
parent
c1a45bf8e3
commit
b7fd487a2e
18 changed files with 918 additions and 170 deletions
|
@ -16,18 +16,20 @@ const (
|
|||
)
|
||||
|
||||
type V1Client struct {
|
||||
token string
|
||||
apihost *url.URL
|
||||
client http.Client
|
||||
downloadToken string
|
||||
token string
|
||||
apihost *url.URL
|
||||
client http.Client
|
||||
}
|
||||
|
||||
// New will only accept a url.URL so that we know
|
||||
// all errors have been handled up until this point
|
||||
func New(connURL *url.URL, token string) *V1Client {
|
||||
func New(connURL *url.URL, token, downloadToken string) *V1Client {
|
||||
return &V1Client{
|
||||
client: http.Client{},
|
||||
apihost: connURL,
|
||||
token: token,
|
||||
client: http.Client{},
|
||||
apihost: connURL,
|
||||
token: token,
|
||||
downloadToken: downloadToken,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -196,6 +198,60 @@ func (v1 *V1Client) PUT(payload interface{}, endpointFormat string, a ...interfa
|
|||
return response
|
||||
}
|
||||
|
||||
// DELETE is the V1 POST function. By design it will check globally for all non 200
|
||||
// responses and return an error if a non 200 is encountered.
|
||||
// DELETE will accept a payload.
|
||||
//
|
||||
// Error Codes:
|
||||
// -1 Unable to create request
|
||||
// -2 Unable to write payload
|
||||
// -3 Unable to JSON Marshal
|
||||
func (v1 *V1Client) DELETE(payload interface{}, endpointFormat string, a ...interface{}) *V1Response {
|
||||
url := v1.Endpoint(fmt.Sprintf(endpointFormat, a...))
|
||||
//logger.Debug("POST [%s]", url)
|
||||
response := &V1Response{}
|
||||
jBytes, err := json.Marshal(&payload)
|
||||
if err != nil {
|
||||
response.StatusCode = -3
|
||||
response.Error = fmt.Errorf("unable to marshal JSON: %v", err)
|
||||
return response
|
||||
}
|
||||
buffer := &bytes.Buffer{}
|
||||
_, err = buffer.Write(jBytes)
|
||||
if err != nil {
|
||||
response.StatusCode = -2
|
||||
response.Error = fmt.Errorf("unable to write payload: %v", err)
|
||||
return response
|
||||
}
|
||||
|
||||
req, err := http.NewRequest("DELETE", url, buffer)
|
||||
if err != nil {
|
||||
response.StatusCode = -1
|
||||
response.Error = fmt.Errorf("unable to create new request: %v", err)
|
||||
return response
|
||||
}
|
||||
req.Header.Set("Content-Type", DefaultContentType)
|
||||
req.Header.Set("X-Session-Id", v1.token)
|
||||
resp, err := v1.client.Do(req)
|
||||
if err != nil {
|
||||
response.Error = fmt.Errorf("error while executing request: %v", err)
|
||||
return response
|
||||
}
|
||||
response.StatusCode = resp.StatusCode
|
||||
response.HTTPResponse = resp
|
||||
body, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
response.Error = fmt.Errorf("unable to read body: %v", err)
|
||||
return response
|
||||
}
|
||||
response.Body = body
|
||||
if resp.StatusCode != 200 {
|
||||
response.Error = fmt.Errorf("[%d]: %s", resp.StatusCode, body)
|
||||
return response
|
||||
}
|
||||
return response
|
||||
}
|
||||
|
||||
// Endpoint supports "/api/v1" and "api/v1" like strings
|
||||
// to generate the string type of a given endpoint based on
|
||||
// a client
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
|
@ -51,14 +50,14 @@ type Photo struct {
|
|||
CameraSerial string `gorm:"type:VARBINARY(255);" json:"CameraSerial" yaml:"CameraSerial,omitempty"`
|
||||
CameraSrc string `gorm:"type:VARBINARY(8);" json:"CameraSrc" yaml:"-"`
|
||||
LensID uint `gorm:"index:idx_photos_camera_lens;default:1" json:"LensID" yaml:"-"`
|
||||
//Details *Details `gorm:"association_autoupdate:false;association_autocreate:false;association_save_reference:false" json:"Details" yaml:"Details"`
|
||||
//Details *Details `gorm:"association_autoupdate:false;association_autocreate:false;association_save_reference:false" json:"Details" yaml:"Details"`
|
||||
//Camera *Camera `gorm:"association_autoupdate:false;association_autocreate:false;association_save_reference:false" json:"Camera" yaml:"-"`
|
||||
//Lens *Lens `gorm:"association_autoupdate:false;association_autocreate:false;association_save_reference:false" json:"Lens" yaml:"-"`
|
||||
//Cell *Cell `gorm:"association_autoupdate:false;association_autocreate:false;association_save_reference:false" json:"Cell" yaml:"-"`
|
||||
//Place *Place `gorm:"association_autoupdate:false;association_autocreate:false;association_save_reference:false" json:"Place" yaml:"-"`
|
||||
//Keywords []Keyword `json:"-" yaml:"-"`
|
||||
//Albums []Album `json:"-" yaml:"-"`
|
||||
//Files []File `yaml:"-"`
|
||||
Files []File `yaml:"-"`
|
||||
//Labels []PhotoLabel `yaml:"-"`
|
||||
CreatedAt time.Time `yaml:"CreatedAt,omitempty"`
|
||||
UpdatedAt time.Time `yaml:"UpdatedAt,omitempty"`
|
||||
|
@ -80,6 +79,9 @@ func (v1 *V1Client) GetPhoto(uuid string) (Photo, error) {
|
|||
}
|
||||
|
||||
// PUT /api/v1/photos/:uid
|
||||
//
|
||||
// Parameters:
|
||||
// uuid: string PhotoUUID as returned by the API
|
||||
func (v1 *V1Client) UpdatePhoto(object Photo) (Photo, error) {
|
||||
err := v1.PUT(&object, "/api/v1/photos/%s", object.UUID).JSON(&object)
|
||||
return object, err
|
||||
|
@ -89,36 +91,27 @@ func (v1 *V1Client) UpdatePhoto(object Photo) (Photo, error) {
|
|||
//
|
||||
// Parameters:
|
||||
// uuid: string PhotoUUID as returned by the API
|
||||
func (v1 *V1Client) GetPhotoDownload(uuid string) (*File, error) {
|
||||
if uuid == "" {
|
||||
return nil, fmt.Errorf("missing uuid for GetPhotoDownload [GET /api/v1/photos/:uuid/dl]")
|
||||
}
|
||||
file := &File{}
|
||||
return file, nil
|
||||
func (v1 *V1Client) GetPhotoDownload(uuid string) ([]byte, error) {
|
||||
resp := v1.GET("/api/v1/photos/%s/dl?t=%s", uuid, v1.downloadToken)
|
||||
return resp.Body, resp.Error
|
||||
}
|
||||
|
||||
// GET /api/v1/photos/:uuid/yaml
|
||||
//
|
||||
// Parameters:
|
||||
// uuid: string PhotoUUID as returned by the API
|
||||
func (v1 *V1Client) GetPhotoYaml(uuid string) (*Photo, error) {
|
||||
if uuid == "" {
|
||||
return nil, fmt.Errorf("missing uuid for GetPhotoYAML [GET /api/v1/photos/:uuid/yaml]")
|
||||
}
|
||||
photo := &Photo{}
|
||||
return photo, nil
|
||||
func (v1 *V1Client) GetPhotoYaml(uuid string) ([]byte, error) {
|
||||
resp := v1.GET("/api/v1/photos/%s/yaml", uuid)
|
||||
return resp.Body, resp.Error
|
||||
}
|
||||
|
||||
// POST /api/v1/photos/:uuid/approve
|
||||
//
|
||||
// Parameters:
|
||||
// uuid: string PhotoUUID as returned by the API
|
||||
func (v1 *V1Client) ApprovePhoto(uuid string) (*Photo, error) {
|
||||
if uuid == "" {
|
||||
return nil, fmt.Errorf("missing uuid for ApprovePhoto [POST /api/v1/photos/:uuid/approve]")
|
||||
}
|
||||
photo := &Photo{}
|
||||
return photo, nil
|
||||
func (v1 *V1Client) ApprovePhoto(uuid string) error {
|
||||
resp := v1.POST(nil, "/api/v1/photos/%s/approve", uuid)
|
||||
return resp.Error
|
||||
}
|
||||
|
||||
// POST /api/v1/photos/:uid/like
|
||||
|
@ -126,10 +119,8 @@ func (v1 *V1Client) ApprovePhoto(uuid string) (*Photo, error) {
|
|||
// Parameters:
|
||||
// uid: string PhotoUID as returned by the API
|
||||
func (v1 *V1Client) LikePhoto(uuid string) error {
|
||||
if uuid == "" {
|
||||
return fmt.Errorf("missing uuid for LikePhoto [POST /api/v1/photos/:uid/like]")
|
||||
}
|
||||
return nil
|
||||
resp := v1.POST(nil, "/api/v1/photos/%s/like", uuid)
|
||||
return resp.Error
|
||||
}
|
||||
|
||||
// DELETE /api/v1/photos/:uuid/like
|
||||
|
@ -137,10 +128,8 @@ func (v1 *V1Client) LikePhoto(uuid string) error {
|
|||
// Parameters:
|
||||
// uuid: string PhotoUUID as returned by the API
|
||||
func (v1 *V1Client) DislikePhoto(uuid string) error {
|
||||
if uuid == "" {
|
||||
return fmt.Errorf("missing uuid for DislikePhoto [DELETE /api/v1/photos/:uuid/like]")
|
||||
}
|
||||
return nil
|
||||
resp := v1.DELETE(nil, "/api/v1/photos/%s/approve", uuid)
|
||||
return resp.Error
|
||||
}
|
||||
|
||||
// POST /api/v1/photos/:uid/files/:file_uid/primary
|
||||
|
@ -149,48 +138,6 @@ func (v1 *V1Client) DislikePhoto(uuid string) error {
|
|||
// uid: string PhotoUID as returned by the API
|
||||
// file_uid: string File UID as returned by the API
|
||||
func (v1 *V1Client) PhotoPrimary(uuid, fileuuid string) error {
|
||||
if uuid == "" {
|
||||
return fmt.Errorf("missing uuid for PhotoPrimary [POST /api/v1/photos/:uid/files/:file_uid/primary]")
|
||||
}
|
||||
if fileuuid == "" {
|
||||
return fmt.Errorf("missing fileuuid for PhotoPrimary [POST /api/v1/photos/:uid/files/:file_uid/primary]")
|
||||
}
|
||||
return nil
|
||||
resp := v1.POST(nil, "/api/v1/photos/%s/files/%s/primary", uuid, fileuuid)
|
||||
return resp.Error
|
||||
}
|
||||
|
||||
// -----
|
||||
// Dump from Chrome
|
||||
//
|
||||
//Request URL: http://localhost:8080/api/v1/photos/pqnzigq156lndozm
|
||||
//Request Method: PUT
|
||||
//Status Code: 200 OK
|
||||
//Remote Address: 127.0.0.1:8080
|
||||
//Referrer Policy: strict-origin-when-cross-origin
|
||||
|
||||
// [RESPONSE HEADERS]
|
||||
//Content-Type: application/json; charset=utf-8
|
||||
//Date: Thu, 04 Feb 2021 04:27:16 GMT
|
||||
//Transfer-Encoding: chunked
|
||||
|
||||
// [REQUEST HEADERS]
|
||||
//Accept: application/json, text/plain, */*
|
||||
//Accept-Encoding: gzip, deflate, br
|
||||
//Accept-Language: en-US,en;q=0.9
|
||||
//Connection: keep-alive
|
||||
//Content-Length: 41
|
||||
//Content-Type: application/json;charset=UTF-8
|
||||
//Host: localhost:8080
|
||||
//Origin: http://localhost:8080
|
||||
//Referer: http://localhost:8080/albums/aqnzih81icziiyae/february-2021
|
||||
//Sec-Fetch-Dest: empty
|
||||
//Sec-Fetch-Mode: cors
|
||||
//Sec-Fetch-Site: same-origin
|
||||
//User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.96 Safari/537.36
|
||||
//X-Client-Hash: 2607a5a5
|
||||
//X-Client-Version: 210121-07e559df-Linux-x86_64
|
||||
//X-Session-ID: d92837cb1c41e37b9993d25e282efb3b337b6ae609a687d9
|
||||
|
||||
// [REQUEST PAYLOAD]
|
||||
//{Title: "Test Nova", TitleSrc: "manual"}
|
||||
//Title: "Test Nova"
|
||||
//TitleSrc: "manual"
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue