Bye bye generators.

This commit is contained in:
retoor 2024-12-03 00:57:59 +01:00
parent 16fd773435
commit fd2c47da45
2 changed files with 8 additions and 20 deletions

View File

@ -111,8 +111,7 @@ class Api:
obj = await response.json()
if not obj.get("success"):
return
for result in obj.get("results", []):
yield result
return obj.get("results", [])
async def get_rant(self, id):
response = None
@ -133,8 +132,8 @@ class Api:
obj = await response.json()
if not obj.get("success"):
return
for rant in obj.get("rants", []):
yield rant
return obj.get("rants", [])
async def get_user_id(self, username):
response = None
@ -150,9 +149,7 @@ class Api:
@property
async def mentions(self):
async for notif in self.notifs:
if notif["type"] == "comment_mention":
yield notif
return [notif for notif in (await self.notifs) if notif['type'] == "comment_mention"]
@property
async def notifs(self):
@ -163,5 +160,5 @@ class Api:
response = await session.get(
url=self.patch_url("users/me/notif-feed"), params=self.patch_auth()
)
for item in (await response.json()).get("data", {}).get("items", []):
yield item
return (await response.json()).get("data", {}).get("items", [])

View File

@ -8,20 +8,11 @@ class ApiTestCase(unittest.IsolatedAsyncioTestCase):
def setUp(self):
self.api = Api()
async def async_list(self, generator):
list_ = []
async for v in generator:
list_.append(v)
return list_
async def async_len(self, generator):
return len(await self.async_list(generator))
async def test_get_rants(self):
self.assertTrue(await self.async_len(self.api.get_rants()))
self.assertTrue(len(await self.api.get_rants()))
async def test_search(self):
self.assertTrue(await self.async_len(self.api.search("retoor")))
self.assertTrue(len(await self.api.search("retoor")))
async def test_get_user_id(self):
self.assertTrue(await self.api.get_user_id("retoor"))