Skip to content

Commit

Permalink
Merge pull request #333 from InjectiveLabs/fix/pagination_next_key
Browse files Browse the repository at this point in the history
Fix/pagination next key
  • Loading branch information
aarmoa authored Jun 12, 2024
2 parents ece0283 + eadfc3d commit a93aab1
Show file tree
Hide file tree
Showing 9 changed files with 564 additions and 488 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

All notable changes to this project will be documented in this file.

## [1.5.3] - 2024-06-12
### Changed
- Changed parameter `key` from the PaginationOption class.
- Fixed an error when using the next page key in PaginationOption, causing incorrect pagination responses.

## [1.5.2] - 2024-05-10
### Changed
- Updated `protobuf` dependency version to ">=5.26.1"
Expand Down
41 changes: 41 additions & 0 deletions examples/chain_client/9_PaginatedRequestExample.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import asyncio

from pyinjective.async_client import AsyncClient
from pyinjective.client.model.pagination import PaginationOption
from pyinjective.core.network import Network


async def main() -> None:
network = Network.testnet()
client = AsyncClient(network)
pagination = PaginationOption(
limit=10,
count_total=True,
)
first_result = await client.fetch_total_supply(
pagination=pagination,
)
print(f"First result:\n{first_result}")

next_page_key = first_result["pagination"]["nextKey"]

for i in range(5):
if next_page_key == "":
break

next_request_pagination = PaginationOption(
limit=pagination.limit,
count_total=pagination.count_total,
encoded_page_key=next_page_key,
)

next_result = await client.fetch_total_supply(
pagination=next_request_pagination,
)
print(f"Page {i+2} result:\n{next_result}")

next_page_key = next_result["pagination"]["nextKey"]


if __name__ == "__main__":
asyncio.get_event_loop().run_until_complete(main())
951 changes: 473 additions & 478 deletions poetry.lock

Large diffs are not rendered by default.

6 changes: 2 additions & 4 deletions pyinjective/async_client.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import asyncio
import base64
import time
from copy import deepcopy
from decimal import Decimal
Expand Down Expand Up @@ -3064,11 +3063,10 @@ async def initialize_tokens_from_chain_denoms(self):
next_key = query_result.get("pagination", {}).get("nextKey", "")

while next_key != "":
query_result = await self.fetch_denoms_metadata(pagination=PaginationOption(key=next_key))
query_result = await self.fetch_denoms_metadata(pagination=PaginationOption(encoded_page_key=next_key))

all_denoms_metadata.extend(query_result.get("metadatas", []))
result_next_key = query_result.get("pagination", {}).get("nextKey", "")
next_key = base64.b64decode(result_next_key).decode()
next_key = query_result.get("pagination", {}).get("nextKey", "")

for token_metadata in all_denoms_metadata:
symbol = token_metadata["symbol"]
Expand Down
10 changes: 6 additions & 4 deletions pyinjective/client/model/pagination.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import base64
from typing import Optional

from pyinjective.proto.cosmos.base.query.v1beta1 import pagination_pb2 as pagination_pb
Expand All @@ -6,7 +7,7 @@
class PaginationOption:
def __init__(
self,
key: Optional[str] = None,
encoded_page_key: Optional[str] = None,
skip: Optional[int] = None,
limit: Optional[int] = None,
start_time: Optional[int] = None,
Expand All @@ -17,7 +18,7 @@ def __init__(
to_number: Optional[int] = None,
):
super().__init__()
self.key = key
self.encoded_page_key = encoded_page_key
self.skip = skip
self.limit = limit
self.start_time = start_time
Expand All @@ -30,8 +31,9 @@ def __init__(
def create_pagination_request(self) -> pagination_pb.PageRequest:
page_request = pagination_pb.PageRequest()

if self.key is not None:
page_request.key = self.key.encode()
if self.encoded_page_key is not None and self.encoded_page_key != "":
page_key = base64.b64decode(self.encoded_page_key)
page_request.key = page_key
if self.skip is not None:
page_request.offset = self.skip
if self.limit is not None:
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "injective-py"
version = "1.5.2"
version = "1.5.3"
description = "Injective Python SDK, with Exchange API Client"
authors = ["Injective Labs <[email protected]>"]
license = "Apache-2.0"
Expand Down
2 changes: 1 addition & 1 deletion tests/client/chain/grpc/test_chain_grpc_auth_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ async def test_fetch_accounts(
api._stub = auth_servicer

pagination_option = PaginationOption(
key="011ab4075a94245dff7338e3042db5b7cc3f42e1",
encoded_page_key="011ab4075a94245dff7338e3042db5b7cc3f42e1",
skip=10,
limit=30,
reverse=False,
Expand Down
Empty file added tests/client/model/__init__.py
Empty file.
35 changes: 35 additions & 0 deletions tests/client/model/test_pagination.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import base64

from pyinjective.client.model.pagination import PaginationOption


class TestPaginationOption:
def test_create_pagination_request(self):
next_page_key = b"next page key"
encoded_next_page_key = base64.b64encode(next_page_key).decode()
pagination_option = PaginationOption(
encoded_page_key=encoded_next_page_key,
skip=5,
limit=10,
start_time=3,
end_time=4,
reverse=False,
count_total=True,
from_number=105,
to_number=135,
)

page_request = pagination_option.create_pagination_request()
assert page_request.key == next_page_key
assert page_request.offset == pagination_option.skip
assert page_request.limit == pagination_option.limit
assert not page_request.reverse
assert page_request.count_total

def test_next_key_is_none_for_empty_encoded_page_key(self):
pagination_option = PaginationOption(
encoded_page_key="",
)
page_request = pagination_option.create_pagination_request()

assert page_request.key == b""

0 comments on commit a93aab1

Please sign in to comment.