diff options
author | Jhon Honce <jhonce@redhat.com> | 2020-01-23 16:13:47 -0700 |
---|---|---|
committer | Jhon Honce <jhonce@redhat.com> | 2020-01-23 16:32:00 -0700 |
commit | 9634e7eef77abbba2584b8e78daf9c76cfdcedd9 (patch) | |
tree | 61731b20c17e12b2c342ec910e28072029d2ecae /pkg/api/handlers/events.go | |
parent | 8beeb067aac857deb29e91562cf4b6f068fe0328 (diff) | |
download | podman-9634e7eef77abbba2584b8e78daf9c76cfdcedd9.tar.gz podman-9634e7eef77abbba2584b8e78daf9c76cfdcedd9.tar.bz2 podman-9634e7eef77abbba2584b8e78daf9c76cfdcedd9.zip |
Add query parameter converters for complex types
* Add converter for URL query parameters of type map[string][]string
* Add converter for URL query parameters of type time.Time
* Added function to allocate and configure schema.Decoder for API use
* Updated API handlers to leverage new converters, and correct handler
code for filter type
An encoding example for a client using filters:
v := map[string][]string{
"dangling": {"true"},
}
payload, err := jsoniter.MarshalToString(v)
if err != nil {
panic(err)
}
payload = "?filters=" + url.QueryEscape(payload)
Signed-off-by: Jhon Honce <jhonce@redhat.com>
Diffstat (limited to 'pkg/api/handlers/events.go')
-rw-r--r-- | pkg/api/handlers/events.go | 25 |
1 files changed, 10 insertions, 15 deletions
diff --git a/pkg/api/handlers/events.go b/pkg/api/handlers/events.go index 900efa3da..44bf35254 100644 --- a/pkg/api/handlers/events.go +++ b/pkg/api/handlers/events.go @@ -1,9 +1,10 @@ package handlers import ( - "encoding/json" "fmt" "net/http" + "strings" + "time" "github.com/containers/libpod/pkg/api/handlers/utils" "github.com/pkg/errors" @@ -11,30 +12,24 @@ import ( func GetEvents(w http.ResponseWriter, r *http.Request) { query := struct { - Since string `json:"since"` - Until string `json:"until"` - Filters string `json:"filters"` + Since time.Time `schema:"since"` + Until time.Time `schema:"until"` + Filters map[string][]string `schema:"filters"` }{} if err := decodeQuery(r, &query); err != nil { utils.Error(w, "Failed to parse parameters", http.StatusBadRequest, errors.Wrapf(err, "Failed to parse parameters for %s", r.URL.String())) } - var filters = map[string][]string{} - if found := hasVar(r, "filters"); found { - if err := json.Unmarshal([]byte(query.Filters), &filters); err != nil { - utils.BadRequest(w, "filters", query.Filters, err) - return + var libpodFilters = []string{} + if _, found := r.URL.Query()["filters"]; found { + for k, v := range query.Filters { + libpodFilters = append(libpodFilters, fmt.Sprintf("%s=%s", k, v[0])) } } - var libpodFilters = make([]string, len(filters)) - for k, v := range filters { - libpodFilters = append(libpodFilters, fmt.Sprintf("%s=%s", k, v[0])) - } - libpodEvents, err := getRuntime(r).GetEvents(libpodFilters) if err != nil { - utils.BadRequest(w, "filters", query.Filters, err) + utils.BadRequest(w, "filters", strings.Join(r.URL.Query()["filters"], ", "), err) return } |