From 5c08e299ebfbd276419d91999b691e8fe55b9392 Mon Sep 17 00:00:00 2001 From: reubenmiller Date: Sun, 5 Mar 2023 17:27:56 +0100 Subject: [PATCH 1/3] feat(c8y api): support common query parameters --- pkg/cmd/api/api.manual.go | 5 +++++ tests/manual/api/api.yaml | 8 ++++++++ 2 files changed, 13 insertions(+) diff --git a/pkg/cmd/api/api.manual.go b/pkg/cmd/api/api.manual.go index 40df1a844..d891172c9 100644 --- a/pkg/cmd/api/api.manual.go +++ b/pkg/cmd/api/api.manual.go @@ -238,6 +238,11 @@ func (n *CmdAPI) RunE(cmd *cobra.Command, args []string) error { if err != nil { return nil } + commonOptions, err := cfg.GetOutputCommonOptions(cmd) + if err != nil { + return cmderrors.NewUserError(fmt.Sprintf("Failed to get common options. err=%s", err)) + } + commonOptions.AddQueryParameters(query) queryValue, err := query.GetQueryUnescape(true) if err != nil { diff --git a/tests/manual/api/api.yaml b/tests/manual/api/api.yaml index 51fba44c0..ca6ffd0d2 100644 --- a/tests/manual/api/api.yaml +++ b/tests/manual/api/api.yaml @@ -130,6 +130,14 @@ tests: path: /alarm/alarms query: pageSize=10&status=ACTIVE + It supports the pageSize flag: + command: | + c8y api /inventory/managedObjects --pageSize 10 --dry + exit-code: 0 + stdout: + json: + query: pageSize=10 + It accepts positional arguments for path and defaults to GET (not using pipeline): command: | c8y api "/alarm/alarms" From 171ac0a5bbd6b832dc18d9a8ade5109b383da503 Mon Sep 17 00:00:00 2001 From: reubenmiller Date: Sun, 5 Mar 2023 17:34:16 +0100 Subject: [PATCH 2/3] only add paging query params on GET requests --- pkg/cmd/api/api.manual.go | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/pkg/cmd/api/api.manual.go b/pkg/cmd/api/api.manual.go index d891172c9..7cdf5fb99 100644 --- a/pkg/cmd/api/api.manual.go +++ b/pkg/cmd/api/api.manual.go @@ -238,11 +238,16 @@ func (n *CmdAPI) RunE(cmd *cobra.Command, args []string) error { if err != nil { return nil } - commonOptions, err := cfg.GetOutputCommonOptions(cmd) - if err != nil { - return cmderrors.NewUserError(fmt.Sprintf("Failed to get common options. err=%s", err)) + + // Only add common query parameter (for paging) to GET requests + if strings.EqualFold(method, http.MethodGet) { + commonOptions, err := cfg.GetOutputCommonOptions(cmd) + if err != nil { + return cmderrors.NewUserError(fmt.Sprintf("Failed to get common options. err=%s", err)) + } + commonOptions.AddQueryParameters(query) } - commonOptions.AddQueryParameters(query) + queryValue, err := query.GetQueryUnescape(true) if err != nil { From 2cf2cc4b46baee8e7f687883701f6dc412347782 Mon Sep 17 00:00:00 2001 From: reubenmiller Date: Sun, 5 Mar 2023 17:38:21 +0100 Subject: [PATCH 3/3] add examples --- pkg/cmd/api/api.manual.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/pkg/cmd/api/api.manual.go b/pkg/cmd/api/api.manual.go index 7cdf5fb99..a0e673350 100644 --- a/pkg/cmd/api/api.manual.go +++ b/pkg/cmd/api/api.manual.go @@ -54,9 +54,12 @@ func NewSubCommand(f *cmdutil.Factory) *CmdAPI { $ c8y api GET /alarm/alarms Get a list of alarms - $ c8y api GET "/alarm/alarms?pageSize=10&status=ACTIVE" + $ c8y api GET "/alarm/alarms&status=ACTIVE" --pageSize 10 Get a list of alarms with custom query parameters + $ c8y api GET "/alarm/alarms&status=ACTIVE" --pageSize 1 --withTotalPages + Get a total ACTIVE alarms + $ c8y api POST "alarm/alarms" --data "text=one,severity=MAJOR,type=test_Type,time=2019-01-01,source.id='12345'" --keepProperties Create a new alarm