Skip to content

Commit

Permalink
api: hotfix legacy endpoints, restoring their old behaviour
Browse files Browse the repository at this point in the history
rather than a "404 page not found" all the legacy endpoints had different behaviours

endpoints that return an empty list
  * GET /accounts/{accountId}/fees/page/{page}
  * GET /accounts/page/{page}
  * GET /chain/organizations/page/{page}
  * POST /chain/organizations/filter/page/{page}
  * GET /chain/transactions/page/{page}
  * GET /chain/blocks/{height}/transactions/page/{page}
  * GET /chain/fees/page/{page}
  * GET /chain/fees/reference/{reference}/page/{page}
  * GET /chain/fees/type/{type}/page/{page}
  * POST /elections/filter/page/{page}
  * POST /elections/filter
  * GET /elections/page/{page}
  * GET /elections/{electionId}/votes/page/{page}

odd endpoints that return {}
  * GET /accounts/{organizationId}/elections/page/{page}
  * GET /accounts/{organizationId}/elections/status/{status}/page/{page}

odd endpoint that returns {"transfers":{"received":[],"sent":[]}}
  * GET /accounts/{accountId}/transfers/page/{page}

the new endpoints are of course unaffected, they still return "page not found":
  * GET /accounts
  * GET /chain/organizations
  * GET /chain/transactions
  * GET /chain/fees
  * GET /chain/transfers
  * GET /elections
  * GET /votes
  • Loading branch information
altergui committed Aug 22, 2024
1 parent cd62600 commit 6f4642c
Show file tree
Hide file tree
Showing 6 changed files with 312 additions and 75 deletions.
75 changes: 63 additions & 12 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 the odd legacy behaviour of sending an empty json "{}"" rather than a 404
if errors.Is(err, ErrPageNotFound) {
return marshalAndSend(ctx, struct{}{})
}
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 the odd legacy behaviour of sending an empty json "{}"" rather than a 404
if errors.Is(err, ErrPageNotFound) {
return marshalAndSend(ctx, struct{}{})
}
return err
}

return marshalAndSend(ctx, list)
}

// accountElectionsCountHandler
Expand Down Expand Up @@ -458,7 +476,16 @@ func (a *API) tokenTransfersListHandler(_ *apirest.APIdata, ctx *httprouter.HTTP
return err
}

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

return marshalAndSend(ctx, list)
}

// tokenFeesHandler
Expand Down Expand Up @@ -490,7 +517,16 @@ func (a *API) tokenFeesHandler(_ *apirest.APIdata, ctx *httprouter.HTTPContext)
return ErrMissingParameter
}

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

return marshalAndSend(ctx, list)
}

// tokenTransfersCountHandler
Expand Down Expand Up @@ -546,7 +582,17 @@ func (a *API) accountListByPageHandler(_ *apirest.APIdata, ctx *httprouter.HTTPC
if err != nil {
return err
}
return a.sendAccountList(ctx, params)

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

return marshalAndSend(ctx, list)
}

// accountListHandler
Expand All @@ -570,33 +616,38 @@ func (a *API) accountListHandler(_ *apirest.APIdata, ctx *httprouter.HTTPContext
if err != nil {
return err
}
return a.sendAccountList(ctx, params)

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

return marshalAndSend(ctx, list)
}

// sendAccountList produces a paginated AccountsList,
// and sends it marshalled over ctx.Send
// accountList produces a paginated AccountsList.
//
// Errors returned are always of type APIerror.
func (a *API) sendAccountList(ctx *httprouter.HTTPContext, params *AccountParams) error {
func (a *API) accountList(params *AccountParams) (*AccountsList, error) {
accounts, total, err := a.indexer.AccountList(
params.Limit,
params.Page*params.Limit,
params.AccountID,
)
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 := &AccountsList{
Accounts: accounts,
Pagination: pagination,
}
return marshalAndSend(ctx, list)
return list, nil
}

// parseAccountParams returns an AccountParams filled with the passed params
Expand Down
Loading

0 comments on commit 6f4642c

Please sign in to comment.