Skip to content

Commit

Permalink
api: hotfix legacy endpoints, return an empty list like they always d…
Browse files Browse the repository at this point in the history
…id (WIP)
  • Loading branch information
altergui committed Aug 21, 2024
1 parent cd62600 commit c314648
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 23 deletions.
22 changes: 20 additions & 2 deletions api/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,16 @@ func (a *API) accountElectionsListByPageHandler(_ *apirest.APIdata, ctx *httprou
return ErrMissingParameter
}

return a.sendElectionList(ctx, params)
list, err := a.electionList(params)
if err != nil {
// keep legacy behaviour of sending an empty list rather than a 404
if errors.Is(err, ErrPageNotFound) {
return marshalAndSend(ctx, emptyElectionsList())
}
return err
}

return marshalAndSend(ctx, list)
}

// accountElectionsListByStatusAndPageHandler
Expand Down Expand Up @@ -398,7 +407,16 @@ func (a *API) accountElectionsListByStatusAndPageHandler(_ *apirest.APIdata, ctx
return ErrMissingParameter
}

return a.sendElectionList(ctx, params)
list, err := a.electionList(params)
if err != nil {
// keep legacy behaviour of sending an empty list rather than a 404
if errors.Is(err, ErrPageNotFound) {
return marshalAndSend(ctx, emptyElectionsList())
}
return err
}

return marshalAndSend(ctx, list)
}

// accountElectionsCountHandler
Expand Down
70 changes: 56 additions & 14 deletions api/elections.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,16 @@ func (a *API) electionListByFilterAndPageHandler(msg *apirest.APIdata, ctx *http
return ErrMissingParameter
}

return a.sendElectionList(ctx, params)
list, err := a.electionList(params)
if err != nil {
// keep legacy behaviour of sending an empty list rather than a 404
if errors.Is(err, ErrPageNotFound) {
return marshalAndSend(ctx, emptyElectionsList())
}
return err
}

return marshalAndSend(ctx, list)
}

// electionListByFilterHandler
Expand All @@ -191,7 +200,16 @@ func (a *API) electionListByFilterHandler(msg *apirest.APIdata, ctx *httprouter.
return ErrCantParseDataAsJSON.WithErr(err)
}

return a.sendElectionList(ctx, params)
list, err := a.electionList(params)
if err != nil {
// keep legacy behaviour of sending an empty list rather than a 404
if errors.Is(err, ErrPageNotFound) {
return marshalAndSend(ctx, emptyElectionsList())
}
return err
}

return marshalAndSend(ctx, list)
}

// electionListByPageHandler
Expand All @@ -213,7 +231,17 @@ func (a *API) electionListByPageHandler(_ *apirest.APIdata, ctx *httprouter.HTTP
if err != nil {
return err
}
return a.sendElectionList(ctx, params)

list, err := a.electionList(params)
if err != nil {
// keep legacy behaviour of sending an empty list rather than a 404
if errors.Is(err, ErrPageNotFound) {
return marshalAndSend(ctx, emptyElectionsList())
}
return err
}

return marshalAndSend(ctx, list)
}

// electionListHandler
Expand Down Expand Up @@ -251,21 +279,26 @@ func (a *API) electionListHandler(_ *apirest.APIdata, ctx *httprouter.HTTPContex
if err != nil {
return err
}
return a.sendElectionList(ctx, params)

list, err := a.electionList(params)
if err != nil {
return err
}

return marshalAndSend(ctx, list)
}

// sendElectionList produces a filtered, paginated ElectionsList,
// and sends it marshalled over ctx.Send
// electionList produces a filtered, paginated ElectionsList.
//
// Errors returned are always of type APIerror.
func (a *API) sendElectionList(ctx *httprouter.HTTPContext, params *ElectionParams) error {
func (a *API) electionList(params *ElectionParams) (*ElectionsList, error) {
if params.OrganizationID != "" && !a.indexer.EntityExists(params.OrganizationID) {
return ErrOrgNotFound
return nil, ErrOrgNotFound
}

status, err := parseStatus(params.Status)
if err != nil {
return err
return nil, err
}

eids, total, err := a.indexer.ProcessList(
Expand All @@ -285,12 +318,12 @@ func (a *API) sendElectionList(ctx *httprouter.HTTPContext, params *ElectionPara
params.EndDateBefore,
)
if err != nil {
return ErrIndexerQueryFailed.WithErr(err)
return nil, ErrIndexerQueryFailed.WithErr(err)
}

pagination, err := calculatePagination(params.Page, params.Limit, total)
if err != nil {
return err
return nil, err
}

list := &ElectionsList{
Expand All @@ -300,11 +333,11 @@ func (a *API) sendElectionList(ctx *httprouter.HTTPContext, params *ElectionPara
for _, eid := range eids {
e, err := a.indexer.ProcessInfo(eid)
if err != nil {
return ErrCantFetchElection.Withf("(%x): %v", eid, err)
return nil, ErrCantFetchElection.Withf("(%x): %v", eid, err)
}
list.Elections = append(list.Elections, a.electionSummary(e))
}
return marshalAndSend(ctx, list)
return list, nil
}

// electionHandler
Expand Down Expand Up @@ -483,7 +516,16 @@ func (a *API) electionVotesListByPageHandler(_ *apirest.APIdata, ctx *httprouter
if err != nil {
return err
}
return a.sendVotesList(ctx, params)
list, err := a.votesList(params)
if err != nil {
// keep legacy behaviour of sending an empty list rather than a 404
if errors.Is(err, ErrPageNotFound) {
return marshalAndSend(ctx, emptyVotesList())
}
return err
}

return marshalAndSend(ctx, list)
}

// electionScrutinyHandler
Expand Down
61 changes: 61 additions & 0 deletions api/legacy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package api

//
// Legacy lists used to return an empty list (instead of null or an error)
//

func emptyElectionsList() any {
return struct {
List []any `json:"elections"`
}{
List: []any{},
}
}

func emptyOrganizationsList() any {

Check failure on line 15 in api/legacy.go

View workflow job for this annotation

GitHub Actions / job_go_checks

func emptyOrganizationsList is unused (U1000)
return struct {
List []any `json:"organizations"`
}{
List: []any{},
}
}

func emptyVotesList() any {
return struct {
List []any `json:"votes"`
}{
List: []any{},
}
}

func emptyTransactionsList() any {

Check failure on line 31 in api/legacy.go

View workflow job for this annotation

GitHub Actions / job_go_checks

func emptyTransactionsList is unused (U1000)
return struct {
List []any `json:"transactions"`
}{
List: []any{},
}
}

func emptyFeesList() any {

Check failure on line 39 in api/legacy.go

View workflow job for this annotation

GitHub Actions / job_go_checks

func emptyFeesList is unused (U1000)
return struct {
List []any `json:"fees"`
}{
List: []any{},
}
}

func emptyTransfersList() any {

Check failure on line 47 in api/legacy.go

View workflow job for this annotation

GitHub Actions / job_go_checks

func emptyTransfersList is unused (U1000)
return struct {
List []any `json:"transfers"`
}{
List: []any{},
}
}

func emptyAccountsList() any {

Check failure on line 55 in api/legacy.go

View workflow job for this annotation

GitHub Actions / job_go_checks

func emptyAccountsList is unused (U1000)
return struct {
List []any `json:"accounts"`
}{
List: []any{},
}
}
20 changes: 13 additions & 7 deletions api/vote.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,16 +218,22 @@ func (a *API) votesListHandler(_ *apirest.APIdata, ctx *httprouter.HTTPContext)
if err != nil {
return err
}
return a.sendVotesList(ctx, params)

list, err := a.votesList(params)
if err != nil {
return err
}

return marshalAndSend(ctx, list)
}

// sendVotesList produces a filtered, paginated VotesList,
// votesList produces a filtered, paginated VotesList,
// and sends it marshalled over ctx.Send
//
// Errors returned are always of type APIerror.
func (a *API) sendVotesList(ctx *httprouter.HTTPContext, params *VoteParams) error {
func (a *API) votesList(params *VoteParams) (*VotesList, error) {
if params.ElectionID != "" && !a.indexer.ProcessExists(params.ElectionID) {
return ErrElectionNotFound
return nil, ErrElectionNotFound
}

votes, total, err := a.indexer.VoteList(
Expand All @@ -237,12 +243,12 @@ func (a *API) sendVotesList(ctx *httprouter.HTTPContext, params *VoteParams) err
"",
)
if err != nil {
return ErrIndexerQueryFailed.WithErr(err)
return nil, ErrIndexerQueryFailed.WithErr(err)
}

pagination, err := calculatePagination(params.Page, params.Limit, total)
if err != nil {
return err
return nil, err
}

list := &VotesList{
Expand All @@ -259,7 +265,7 @@ func (a *API) sendVotesList(ctx *httprouter.HTTPContext, params *VoteParams) err
TransactionIndex: &vote.TxIndex,
})
}
return marshalAndSend(ctx, list)
return list, nil
}

// parseVoteParams returns an VoteParams filled with the passed params
Expand Down

0 comments on commit c314648

Please sign in to comment.