Add protocol for cursor pagination

This commit is contained in:
2023-12-17 22:18:05 +03:00
parent 3b9d0d2ba6
commit 4d72725990
3 changed files with 20 additions and 6 deletions

View File

@@ -110,8 +110,8 @@ class BaseBackend:
def is_auth(self) -> bool: def is_auth(self) -> bool:
return all((self._token, self._encryption_key)) return all((self._token, self._encryption_key))
async def get_sense_list(self) -> SenseList: async def get_sense_list(self, cursor: str | None = None, limit: int = 10) -> SenseList:
encrypted_sense_list = await self.fetch_sense_list() encrypted_sense_list = await self.fetch_sense_list(cursor=cursor, limit=limit)
senses = [ senses = [
self.convert_encrypted_sense_to_sense(encrypted_sense) self.convert_encrypted_sense_to_sense(encrypted_sense)
for encrypted_sense in encrypted_sense_list.senses for encrypted_sense in encrypted_sense_list.senses
@@ -176,7 +176,11 @@ class BaseBackend:
async def get_options(self) -> Options: async def get_options(self) -> Options:
raise NotImplementedError 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 raise NotImplementedError
async def fetch_sense(self, sense_id: uuid.UUID) -> EncryptedSense: async def fetch_sense(self, sense_id: uuid.UUID) -> EncryptedSense:

View File

@@ -60,7 +60,11 @@ class LocalBackend(BaseBackend):
async def get_options(self) -> Options: async def get_options(self) -> Options:
return Options(registration_enabled=True) 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: if not self.is_auth:
raise NonAuthenticatedException() raise NonAuthenticatedException()

View File

@@ -117,10 +117,16 @@ class SoulBackend(BaseBackend):
return Options.model_validate(response) 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/" 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"]] senses = [EncryptedSense.model_validate(sense) for sense in response["data"]]
return EncryptedSenseList(senses=senses) return EncryptedSenseList(senses=senses)