Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(c8y api): support common query parameters for GET requests #223

Merged
merged 3 commits into from
Mar 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion pkg/cmd/api/api.manual.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -238,6 +241,16 @@ func (n *CmdAPI) RunE(cmd *cobra.Command, args []string) error {
if err != nil {
return nil
}

// 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)
}

queryValue, err := query.GetQueryUnescape(true)

if err != nil {
Expand Down
8 changes: 8 additions & 0 deletions tests/manual/api/api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down