Files
podman/pkg/api/handlers/events.go
Jhon Honce 9634e7eef7 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>
2020-01-23 16:32:00 -07:00

42 lines
1.1 KiB
Go

package handlers
import (
"fmt"
"net/http"
"strings"
"time"
"github.com/containers/libpod/pkg/api/handlers/utils"
"github.com/pkg/errors"
)
func GetEvents(w http.ResponseWriter, r *http.Request) {
query := struct {
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 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]))
}
}
libpodEvents, err := getRuntime(r).GetEvents(libpodFilters)
if err != nil {
utils.BadRequest(w, "filters", strings.Join(r.URL.Query()["filters"], ", "), err)
return
}
var apiEvents = make([]*Event, len(libpodEvents))
for _, v := range libpodEvents {
apiEvents = append(apiEvents, EventToApiEvent(v))
}
utils.WriteJSON(w, http.StatusOK, apiEvents)
}