move struct to piwigo

This commit is contained in:
Celogeek 2021-12-21 11:31:48 +01:00
parent c302a10c75
commit 1e0c409dd0
Signed by: celogeek
GPG Key ID: E6B7BDCFC446233A
11 changed files with 108 additions and 87 deletions

View File

@ -0,0 +1,9 @@
package piwigo
type Derivatives map[string]Derivative
type Derivative struct {
Height int `json:"height"`
Width int `json:"width"`
Url string `json:"url"`
}

View File

@ -2,7 +2,10 @@ package piwigo
import ( import (
"encoding/json" "encoding/json"
"errors"
"fmt" "fmt"
"net/url"
"strings"
) )
func DumpResponse(v interface{}) (err error) { func DumpResponse(v interface{}) (err error) {
@ -12,3 +15,15 @@ func DumpResponse(v interface{}) (err error) {
} }
return return
} }
func ArgsToForm(args []string) (*url.Values, error) {
params := &url.Values{}
for _, arg := range args {
r := strings.SplitN(arg, "=", 2)
if len(r) != 2 {
return nil, errors.New("args should be key=value")
}
params.Add(r[0], r[1])
}
return params, nil
}

19
internal/piwigo/images.go Normal file
View File

@ -0,0 +1,19 @@
package piwigo
type ImagesDetails struct {
Id int `json:"id"`
Md5 string `json:"md5sum"`
Name string `json:"name"`
DateAvailable TimeResult `json:"date_available"`
DateCreation TimeResult `json:"date_creation"`
LastModified TimeResult `json:"lastmodified"`
Width int `json:"width"`
Height int `json:"height"`
Url string `json:"page_url"`
ImageUrl string `json:"element_url"`
Filename string `json:"file"`
Filesize int64 `json:"filesize"`
Categories Categories `json:"categories"`
Tags Tags `json:"tags"`
Derivatives Derivatives `json:"derivatives"`
}

8
internal/piwigo/info.go Normal file
View File

@ -0,0 +1,8 @@
package piwigo
type Infos []Info
type Info struct {
Name string `json:"name"`
Value interface{} `json:"value"`
}

View File

@ -0,0 +1,45 @@
package piwigo
import (
"encoding/json"
)
type Methods []string
type MethodParams []MethodParam
type MethodParam struct {
Name string `json:"name"`
Optional bool `json:"optional"`
Type string `json:"type"`
AcceptArray bool `json:"acceptArray"`
DefaultValue interface{} `json:"defaultValue"`
MaxValue interface{} `json:"maxValue"`
Info string `json:"info"`
}
type MethodOptions struct {
Admin bool `json:"admin_only"`
PostOnly bool `json:"post_only"`
}
func (j *MethodOptions) UnmarshalJSON(data []byte) error {
var r interface{}
if err := json.Unmarshal(data, &r); err != nil {
return err
}
switch r := r.(type) {
case map[string]interface{}:
j.Admin, _ = r["admin_only"].(bool)
j.PostOnly, _ = r["post_only"].(bool)
}
return nil
}
type MethodDetails struct {
Name string `json:"name"`
Description string `json:"description"`
Options MethodOptions `json:"options"`
Parameters MethodParams `json:"params"`
}

View File

@ -11,15 +11,8 @@ import (
type CategoriesListCommand struct { type CategoriesListCommand struct {
} }
type Category struct {
Id int `json:"id"`
Name string `json:"name"`
ImagesCount int `json:"nb_images"`
Url string `json:"url"`
}
type GetCategoriesListResponse struct { type GetCategoriesListResponse struct {
Categories []Category `json:"categories"` Categories piwigo.Categories `json:"categories"`
} }
func (c *CategoriesListCommand) Execute(args []string) error { func (c *CategoriesListCommand) Execute(args []string) error {

View File

@ -11,13 +11,8 @@ import (
type GetInfosCommand struct { type GetInfosCommand struct {
} }
type Info struct {
Name string `json:"name"`
Value interface{} `json:"value"`
}
type GetInfosResponse struct { type GetInfosResponse struct {
Infos []Info `json:"infos"` Infos piwigo.Infos `json:"infos"`
} }
var getInfosCommand GetInfosCommand var getInfosCommand GetInfosCommand

View File

@ -14,29 +14,7 @@ type ImagesDetailsCommand struct {
Id string `short:"i" long:"id" description:"ID of the images" required:"true"` Id string `short:"i" long:"id" description:"ID of the images" required:"true"`
} }
type Derivative struct { type GetImagesDetailsResponse piwigo.ImagesDetails
Height int `json:"height"`
Width int `json:"width"`
Url string `json:"url"`
}
type GetImagesDetailsResponse struct {
Id int `json:"id"`
Md5 string `json:"md5sum"`
Name string `json:"name"`
DateAvailable piwigo.TimeResult `json:"date_available"`
DateCreation piwigo.TimeResult `json:"date_creation"`
LastModified piwigo.TimeResult `json:"lastmodified"`
Width int `json:"width"`
Height int `json:"height"`
Url string `json:"page_url"`
ImageUrl string `json:"element_url"`
Filename string `json:"file"`
Filesize int64 `json:"filesize"`
Categories piwigo.Categories `json:"categories"`
Tags piwigo.Tags `json:"tags"`
Derivatives map[string]Derivative `json:"derivatives"`
}
func (c *ImagesDetailsCommand) Execute(args []string) error { func (c *ImagesDetailsCommand) Execute(args []string) error {
p := piwigo.Piwigo{} p := piwigo.Piwigo{}

View File

@ -1,7 +1,6 @@
package piwigocli package piwigocli
import ( import (
"encoding/json"
"fmt" "fmt"
"net/url" "net/url"
"os" "os"
@ -15,41 +14,7 @@ type MethodDetailsCommand struct {
MethodName string `short:"m" long:"method-name" description:"Method name to details"` MethodName string `short:"m" long:"method-name" description:"Method name to details"`
} }
type MethodDetailsParams struct { type MethodDetailsResult piwigo.MethodDetails
Name string `json:"name"`
Optional bool `json:"optional"`
Type string `json:"type"`
AcceptArray bool `json:"acceptArray"`
DefaultValue interface{} `json:"defaultValue"`
MaxValue interface{} `json:"maxValue"`
Info string `json:"info"`
}
type MethodDetailsOptions struct {
Admin bool `json:"admin_only"`
PostOnly bool `json:"post_only"`
}
type MethodDetailsResult struct {
Name string `json:"name"`
Description string `json:"description"`
Options MethodDetailsOptions `json:"options"`
Parameters []MethodDetailsParams `json:"params"`
}
func (j *MethodDetailsOptions) UnmarshalJSON(data []byte) error {
var r interface{}
if err := json.Unmarshal(data, &r); err != nil {
return err
}
switch r := r.(type) {
case map[string]interface{}:
j.Admin, _ = r["admin_only"].(bool)
j.PostOnly, _ = r["post_only"].(bool)
}
return nil
}
func (c *MethodDetailsCommand) Execute(args []string) error { func (c *MethodDetailsCommand) Execute(args []string) error {
p := piwigo.Piwigo{} p := piwigo.Piwigo{}

View File

@ -10,7 +10,7 @@ import (
type MethodListCommand struct{} type MethodListCommand struct{}
type MethodListResult struct { type MethodListResult struct {
Methods []string `json:"methods"` Methods piwigo.Methods `json:"methods"`
} }
func (c *MethodListCommand) Execute(args []string) error { func (c *MethodListCommand) Execute(args []string) error {

View File

@ -1,10 +1,6 @@
package piwigocli package piwigocli
import ( import (
"errors"
"net/url"
"strings"
"github.com/celogeek/piwigo-cli/internal/piwigo" "github.com/celogeek/piwigo-cli/internal/piwigo"
) )
@ -24,13 +20,9 @@ func (c *MethodTryCommand) Execute(args []string) error {
} }
var result interface{} var result interface{}
params := &url.Values{} params, err := piwigo.ArgsToForm(args)
for _, arg := range args { if err != nil {
r := strings.SplitN(arg, "=", 2) return err
if len(r) != 2 {
return errors.New("args should be key=value")
}
params.Add(r[0], r[1])
} }
if err := p.Post(c.MethodName, params, &result); err != nil { if err := p.Post(c.MethodName, params, &result); err != nil {
@ -38,7 +30,9 @@ func (c *MethodTryCommand) Execute(args []string) error {
return err return err
} }
piwigo.DumpResponse(result) piwigo.DumpResponse(map[string]interface{}{
piwigo.DumpResponse(params) "params": params,
"result": result,
})
return nil return nil
} }