From 1e0c409dd079bb54397d3d60b439e28a606d29ba Mon Sep 17 00:00:00 2001 From: celogeek <65178+celogeek@users.noreply.github.com> Date: Tue, 21 Dec 2021 11:31:48 +0100 Subject: [PATCH] move struct to piwigo --- internal/piwigo/derivatives.go | 9 ++++++ internal/piwigo/helper.go | 15 +++++++++ internal/piwigo/images.go | 19 +++++++++++ internal/piwigo/info.go | 8 +++++ internal/piwigo/methods.go | 45 +++++++++++++++++++++++++++ internal/piwigocli/categories_list.go | 9 +----- internal/piwigocli/general.go | 7 +---- internal/piwigocli/images_details.go | 24 +------------- internal/piwigocli/method_details.go | 37 +--------------------- internal/piwigocli/method_list.go | 2 +- internal/piwigocli/method_try.go | 20 +++++------- 11 files changed, 108 insertions(+), 87 deletions(-) create mode 100644 internal/piwigo/derivatives.go create mode 100644 internal/piwigo/images.go create mode 100644 internal/piwigo/info.go create mode 100644 internal/piwigo/methods.go diff --git a/internal/piwigo/derivatives.go b/internal/piwigo/derivatives.go new file mode 100644 index 0000000..dfe3257 --- /dev/null +++ b/internal/piwigo/derivatives.go @@ -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"` +} diff --git a/internal/piwigo/helper.go b/internal/piwigo/helper.go index 5a24b1d..94dda4d 100644 --- a/internal/piwigo/helper.go +++ b/internal/piwigo/helper.go @@ -2,7 +2,10 @@ package piwigo import ( "encoding/json" + "errors" "fmt" + "net/url" + "strings" ) func DumpResponse(v interface{}) (err error) { @@ -12,3 +15,15 @@ func DumpResponse(v interface{}) (err error) { } 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 +} diff --git a/internal/piwigo/images.go b/internal/piwigo/images.go new file mode 100644 index 0000000..3066b97 --- /dev/null +++ b/internal/piwigo/images.go @@ -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"` +} diff --git a/internal/piwigo/info.go b/internal/piwigo/info.go new file mode 100644 index 0000000..a38f023 --- /dev/null +++ b/internal/piwigo/info.go @@ -0,0 +1,8 @@ +package piwigo + +type Infos []Info + +type Info struct { + Name string `json:"name"` + Value interface{} `json:"value"` +} diff --git a/internal/piwigo/methods.go b/internal/piwigo/methods.go new file mode 100644 index 0000000..1702807 --- /dev/null +++ b/internal/piwigo/methods.go @@ -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"` +} diff --git a/internal/piwigocli/categories_list.go b/internal/piwigocli/categories_list.go index 404dbe8..de27231 100644 --- a/internal/piwigocli/categories_list.go +++ b/internal/piwigocli/categories_list.go @@ -11,15 +11,8 @@ import ( 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 { - Categories []Category `json:"categories"` + Categories piwigo.Categories `json:"categories"` } func (c *CategoriesListCommand) Execute(args []string) error { diff --git a/internal/piwigocli/general.go b/internal/piwigocli/general.go index 598234e..f7e1c63 100644 --- a/internal/piwigocli/general.go +++ b/internal/piwigocli/general.go @@ -11,13 +11,8 @@ import ( type GetInfosCommand struct { } -type Info struct { - Name string `json:"name"` - Value interface{} `json:"value"` -} - type GetInfosResponse struct { - Infos []Info `json:"infos"` + Infos piwigo.Infos `json:"infos"` } var getInfosCommand GetInfosCommand diff --git a/internal/piwigocli/images_details.go b/internal/piwigocli/images_details.go index 6bf2298..1fe2bd8 100644 --- a/internal/piwigocli/images_details.go +++ b/internal/piwigocli/images_details.go @@ -14,29 +14,7 @@ type ImagesDetailsCommand struct { Id string `short:"i" long:"id" description:"ID of the images" required:"true"` } -type Derivative struct { - 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"` -} +type GetImagesDetailsResponse piwigo.ImagesDetails func (c *ImagesDetailsCommand) Execute(args []string) error { p := piwigo.Piwigo{} diff --git a/internal/piwigocli/method_details.go b/internal/piwigocli/method_details.go index 8679e41..7ba96a2 100644 --- a/internal/piwigocli/method_details.go +++ b/internal/piwigocli/method_details.go @@ -1,7 +1,6 @@ package piwigocli import ( - "encoding/json" "fmt" "net/url" "os" @@ -15,41 +14,7 @@ type MethodDetailsCommand struct { MethodName string `short:"m" long:"method-name" description:"Method name to details"` } -type MethodDetailsParams 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 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 -} +type MethodDetailsResult piwigo.MethodDetails func (c *MethodDetailsCommand) Execute(args []string) error { p := piwigo.Piwigo{} diff --git a/internal/piwigocli/method_list.go b/internal/piwigocli/method_list.go index 81a5985..cee5695 100644 --- a/internal/piwigocli/method_list.go +++ b/internal/piwigocli/method_list.go @@ -10,7 +10,7 @@ import ( type MethodListCommand struct{} type MethodListResult struct { - Methods []string `json:"methods"` + Methods piwigo.Methods `json:"methods"` } func (c *MethodListCommand) Execute(args []string) error { diff --git a/internal/piwigocli/method_try.go b/internal/piwigocli/method_try.go index 02fea3a..57aa082 100644 --- a/internal/piwigocli/method_try.go +++ b/internal/piwigocli/method_try.go @@ -1,10 +1,6 @@ package piwigocli import ( - "errors" - "net/url" - "strings" - "github.com/celogeek/piwigo-cli/internal/piwigo" ) @@ -24,13 +20,9 @@ func (c *MethodTryCommand) Execute(args []string) error { } var result interface{} - params := &url.Values{} - for _, arg := range args { - r := strings.SplitN(arg, "=", 2) - if len(r) != 2 { - return errors.New("args should be key=value") - } - params.Add(r[0], r[1]) + params, err := piwigo.ArgsToForm(args) + if err != nil { + return err } if err := p.Post(c.MethodName, params, &result); err != nil { @@ -38,7 +30,9 @@ func (c *MethodTryCommand) Execute(args []string) error { return err } - piwigo.DumpResponse(result) - piwigo.DumpResponse(params) + piwigo.DumpResponse(map[string]interface{}{ + "params": params, + "result": result, + }) return nil }