-
Notifications
You must be signed in to change notification settings - Fork 503
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
/exp/lighthorizon: new endpoints for tx and ops paged listing by acco…
…unt id (#4453)
- Loading branch information
Showing
16 changed files
with
979 additions
and
774 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
package actions | ||
|
||
import ( | ||
"net/http" | ||
"strconv" | ||
|
||
"github.com/stellar/go/support/log" | ||
|
||
"github.com/stellar/go/exp/lighthorizon/adapters" | ||
"github.com/stellar/go/exp/lighthorizon/services" | ||
hProtocol "github.com/stellar/go/protocols/horizon" | ||
"github.com/stellar/go/protocols/horizon/operations" | ||
"github.com/stellar/go/support/render/hal" | ||
"github.com/stellar/go/toid" | ||
) | ||
|
||
const ( | ||
urlAccountId = "account_id" | ||
) | ||
|
||
func accountRequestParams(w http.ResponseWriter, r *http.Request) (string, pagination) { | ||
var accountId string | ||
var accountErr bool | ||
|
||
if accountId, accountErr = getURLParam(r, urlAccountId); !accountErr { | ||
sendErrorResponse(w, http.StatusBadRequest, "") | ||
return "", pagination{} | ||
} | ||
|
||
paginate, err := paging(r) | ||
if err != nil { | ||
sendErrorResponse(w, http.StatusBadRequest, string(invalidPagingParameters)) | ||
return "", pagination{} | ||
} | ||
|
||
if paginate.Cursor < 1 { | ||
paginate.Cursor = toid.New(1, 1, 1).ToInt64() | ||
} | ||
|
||
if paginate.Limit == 0 { | ||
paginate.Limit = 10 | ||
} | ||
|
||
return accountId, paginate | ||
} | ||
|
||
func NewTXByAccountHandler(lightHorizon services.LightHorizon) func(http.ResponseWriter, *http.Request) { | ||
return func(w http.ResponseWriter, r *http.Request) { | ||
ctx := r.Context() | ||
var accountId string | ||
var paginate pagination | ||
|
||
if accountId, paginate = accountRequestParams(w, r); accountId == "" { | ||
return | ||
} | ||
|
||
page := hal.Page{ | ||
Cursor: strconv.FormatInt(paginate.Cursor, 10), | ||
Order: string(paginate.Order), | ||
Limit: uint64(paginate.Limit), | ||
} | ||
page.Init() | ||
page.FullURL = r.URL | ||
|
||
txns, err := lightHorizon.Transactions.GetTransactionsByAccount(ctx, paginate.Cursor, paginate.Limit, accountId) | ||
if err != nil { | ||
log.Error(err) | ||
sendErrorResponse(w, http.StatusInternalServerError, err.Error()) | ||
return | ||
} | ||
|
||
for _, txn := range txns { | ||
var response hProtocol.Transaction | ||
response, err = adapters.PopulateTransaction(r, &txn) | ||
if err != nil { | ||
log.Error(err) | ||
sendErrorResponse(w, http.StatusInternalServerError, err.Error()) | ||
return | ||
} | ||
|
||
page.Add(response) | ||
} | ||
|
||
page.PopulateLinks() | ||
sendPageResponse(w, page) | ||
} | ||
} | ||
|
||
func NewOpsByAccountHandler(lightHorizon services.LightHorizon) func(http.ResponseWriter, *http.Request) { | ||
return func(w http.ResponseWriter, r *http.Request) { | ||
ctx := r.Context() | ||
var accountId string | ||
var paginate pagination | ||
|
||
if accountId, paginate = accountRequestParams(w, r); accountId == "" { | ||
return | ||
} | ||
|
||
page := hal.Page{ | ||
Cursor: strconv.FormatInt(paginate.Cursor, 10), | ||
Order: string(paginate.Order), | ||
Limit: uint64(paginate.Limit), | ||
} | ||
page.Init() | ||
page.FullURL = r.URL | ||
|
||
ops, err := lightHorizon.Operations.GetOperationsByAccount(ctx, paginate.Cursor, paginate.Limit, accountId) | ||
if err != nil { | ||
log.Error(err) | ||
sendErrorResponse(w, http.StatusInternalServerError, err.Error()) | ||
return | ||
} | ||
|
||
for _, op := range ops { | ||
var response operations.Operation | ||
response, err = adapters.PopulateOperation(r, &op) | ||
if err != nil { | ||
log.Error(err) | ||
sendErrorResponse(w, http.StatusInternalServerError, err.Error()) | ||
return | ||
} | ||
|
||
page.Add(response) | ||
} | ||
|
||
page.PopulateLinks() | ||
sendPageResponse(w, page) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.