From 4d72725990dd072d867947b32611f196ae7aab03 Mon Sep 17 00:00:00 2001 From: Oleg Yurchik Date: Sun, 17 Dec 2023 22:18:05 +0300 Subject: [PATCH] Add protocol for cursor pagination --- soul_diary/ui/app/backend/base.py | 10 +++++++--- soul_diary/ui/app/backend/local.py | 6 +++++- soul_diary/ui/app/backend/soul.py | 10 ++++++++-- 3 files changed, 20 insertions(+), 6 deletions(-) diff --git a/soul_diary/ui/app/backend/base.py b/soul_diary/ui/app/backend/base.py index c5f64a4..9b89f7b 100644 --- a/soul_diary/ui/app/backend/base.py +++ b/soul_diary/ui/app/backend/base.py @@ -110,8 +110,8 @@ class BaseBackend: def is_auth(self) -> bool: return all((self._token, self._encryption_key)) - async def get_sense_list(self) -> SenseList: - encrypted_sense_list = await self.fetch_sense_list() + async def get_sense_list(self, cursor: str | None = None, limit: int = 10) -> SenseList: + encrypted_sense_list = await self.fetch_sense_list(cursor=cursor, limit=limit) senses = [ self.convert_encrypted_sense_to_sense(encrypted_sense) for encrypted_sense in encrypted_sense_list.senses @@ -176,7 +176,11 @@ class BaseBackend: async def get_options(self) -> Options: raise NotImplementedError - async def fetch_sense_list(self) -> EncryptedSenseList: + async def fetch_sense_list( + self, + cursor: str | None = None, + limit: int = 10, + ) -> EncryptedSenseList: raise NotImplementedError async def fetch_sense(self, sense_id: uuid.UUID) -> EncryptedSense: diff --git a/soul_diary/ui/app/backend/local.py b/soul_diary/ui/app/backend/local.py index 1722714..58be7af 100644 --- a/soul_diary/ui/app/backend/local.py +++ b/soul_diary/ui/app/backend/local.py @@ -60,7 +60,11 @@ class LocalBackend(BaseBackend): async def get_options(self) -> Options: return Options(registration_enabled=True) - async def fetch_sense_list(self) -> EncryptedSenseList: + async def fetch_sense_list( + self, + cursor: str | None = None, + limit: int = 10, + ) -> EncryptedSenseList: if not self.is_auth: raise NonAuthenticatedException() diff --git a/soul_diary/ui/app/backend/soul.py b/soul_diary/ui/app/backend/soul.py index a1b907d..d599dde 100644 --- a/soul_diary/ui/app/backend/soul.py +++ b/soul_diary/ui/app/backend/soul.py @@ -117,10 +117,16 @@ class SoulBackend(BaseBackend): return Options.model_validate(response) - async def fetch_sense_list(self) -> EncryptedSenseList: + async def fetch_sense_list( + self, + cursor: str | None = None, + limit: int = 10, + ) -> EncryptedSenseList: path = "/senses/" + params = {"limit": limit, "cursor": cursor} + params = {key: value for key, value in params.items() if value is not None} - response = await self.request(method="GET", path=path) + response = await self.request(method="GET", path=path, params=params) senses = [EncryptedSense.model_validate(sense) for sense in response["data"]] return EncryptedSenseList(senses=senses)