diff --git a/api/elections.go b/api/elections.go index 99ca26f63..9a96e943b 100644 --- a/api/elections.go +++ b/api/elections.go @@ -29,9 +29,21 @@ const ( MaxOffchainFileSize = 1024 * 1024 * 1 // 1MB ) +const ( + ParamPage = "page" +) + func (a *API) enableElectionHandlers() error { if err := a.Endpoint.RegisterMethod( - "/elections/page/{page}", + "/elections/page/{page}", // legacy endpoint + "GET", + apirest.MethodAccessTypePublic, + a.electionFullListByPageHandler, + ); err != nil { + return err + } + if err := a.Endpoint.RegisterMethod( + "/elections", "GET", apirest.MethodAccessTypePublic, a.electionFullListHandler, @@ -124,9 +136,9 @@ func (a *API) enableElectionHandlers() error { return nil } -// electionFullListHandler +// electionFullListByPageHandler // -// @Summary List elections +// @Summary List elections (legacy params design) // @Description Get a list of elections summaries. // @Tags Elections // @Accept json @@ -134,7 +146,7 @@ func (a *API) enableElectionHandlers() error { // @Param page path number true "Page " // @Success 200 {object} ElectionSummary // @Router /elections/page/{page} [get] -func (a *API) electionFullListHandler(_ *apirest.APIdata, ctx *httprouter.HTTPContext) error { +func (a *API) electionFullListByPageHandler(_ *apirest.APIdata, ctx *httprouter.HTTPContext) error { page := 0 if ctx.URLParam("page") != "" { var err error @@ -143,6 +155,32 @@ func (a *API) electionFullListHandler(_ *apirest.APIdata, ctx *httprouter.HTTPCo return ErrCantParsePageNumber.With(ctx.URLParam("page")) } } + return a.electionFullListByPage(ctx, page) +} + +// electionFullListHandler +// +// @Summary List elections +// @Description Get a list of elections summaries. +// @Tags Elections +// @Accept json +// @Produce json +// @Param page query number false "Page " +// @Success 200 {object} ElectionSummary +// @Router /elections [get] +func (a *API) electionFullListHandler(_ *apirest.APIdata, ctx *httprouter.HTTPContext) error { + page := 0 + if p := ctx.QueryParam(ParamPage); p != "" { + var err error + page, err = strconv.Atoi(p) + if err != nil { + return ErrCantParsePageNumber.With(p) + } + } + return a.electionFullListByPage(ctx, page) +} + +func (a *API) electionFullListByPage(ctx *httprouter.HTTPContext, page int) error { elections, err := a.indexer.ProcessList(nil, page*MaxPageSize, MaxPageSize, "", 0, 0, "", false) if err != nil { return ErrCantFetchElectionList.WithErr(err) diff --git a/httprouter/message.go b/httprouter/message.go index c17a12fcd..b1723dd29 100644 --- a/httprouter/message.go +++ b/httprouter/message.go @@ -44,6 +44,12 @@ func (h *HTTPContext) URLParam(key string) string { return chi.URLParam(h.Request, key) } +// QueryParam is a wrapper around go-chi to get the value of a query string parameter (like "?key=value"). +// If key is not present, returns the empty string. +func (h *HTTPContext) QueryParam(key string) string { + return h.Request.URL.Query().Get(key) +} + // Send replies the request with the provided message. func (h *HTTPContext) Send(msg []byte, httpStatusCode int) error { defer func() {