From 5e659105622277ba33d3041f9ac23f79a43539bc Mon Sep 17 00:00:00 2001 From: Oliver Eggert Date: Mon, 21 Nov 2022 21:11:06 -0800 Subject: [PATCH] Add code samples for markers and pagination. --- .../markers-and-pagination/README.md | 5 +++ .../js/pagination-with-markers.js | 40 +++++++++++++++++++ .../py/pagination-with-markers.py | 23 +++++++++++ 3 files changed, 68 insertions(+) create mode 100644 content/_code-samples/markers-and-pagination/README.md create mode 100644 content/_code-samples/markers-and-pagination/js/pagination-with-markers.js create mode 100644 content/_code-samples/markers-and-pagination/py/pagination-with-markers.py diff --git a/content/_code-samples/markers-and-pagination/README.md b/content/_code-samples/markers-and-pagination/README.md new file mode 100644 index 0000000000..63a2f5ec31 --- /dev/null +++ b/content/_code-samples/markers-and-pagination/README.md @@ -0,0 +1,5 @@ +# Markers and Pagination + +Iterate over a `ledger_data` method request that requires multiple calls. + +Examples from the [Markers and Pagination page](https://xrpl.org/markers-and-pagination.html#markers-and-pagination). \ No newline at end of file diff --git a/content/_code-samples/markers-and-pagination/js/pagination-with-markers.js b/content/_code-samples/markers-and-pagination/js/pagination-with-markers.js new file mode 100644 index 0000000000..4830ecaaa6 --- /dev/null +++ b/content/_code-samples/markers-and-pagination/js/pagination-with-markers.js @@ -0,0 +1,40 @@ +const xrpl = require("xrpl") + +async function main() { + + // Create client and connect to network. + const client = new xrpl.Client("wss://xrplcluster.com/") + await client.connect() + + // Query ledger data. + let ledger = await client.request({ + "command": "ledger_data", + "ledger_index": 500000, + }) + + // Create function to loop through API calls. + function code(){ + console.log(ledger["result"]) + } + + // Run code at least once before checking for markers. + do { + code() + + if (ledger["result"]["marker"] == null) { + break + } + + const ledger_marker = await client.request({ + "command": "ledger_data", + "ledger_index": 500000, + "marker": ledger["result"]["marker"] + }) + ledger = ledger_marker + + } while (true) + + client.disconnect() +} + +main() \ No newline at end of file diff --git a/content/_code-samples/markers-and-pagination/py/pagination-with-markers.py b/content/_code-samples/markers-and-pagination/py/pagination-with-markers.py new file mode 100644 index 0000000000..c633ca222c --- /dev/null +++ b/content/_code-samples/markers-and-pagination/py/pagination-with-markers.py @@ -0,0 +1,23 @@ +from xrpl.clients import JsonRpcClient +from xrpl.models.requests import LedgerData + +# Create a client to connect to the main network. +client = JsonRpcClient("https://xrplcluster.com/") + +# Specify ledger to query and request data. +ledger = LedgerData(ledger_index=500000) +ledger_data = client.request(ledger).result + +# Code to run on each call. +def code(): + print(ledger_data) + +#Execute code at least once before checking for markers. +while True: + code() + if "marker" not in ledger_data: + break + + # Specify ledger and marker to continue querying. + ledger_marker = LedgerData(ledger_index=500000, marker=ledger_data["marker"]) + ledger_data = client.request(ledger_marker).result \ No newline at end of file