diff --git a/mute_sync.py b/mute_sync.py index 9b88a40..5e43869 100644 --- a/mute_sync.py +++ b/mute_sync.py @@ -13,6 +13,31 @@ class BearerAuth(httpx.Auth): request.headers['Authorization'] = f'Bearer {self.token}' yield request +def paginated_get(client: httpx.Client, url: str, *args, **kwargs) -> list[dict]: + full_response = [] + + while True: + r = client.get(url, *args, **kwargs) + r.raise_for_status() + full_response.extend(r.json()) + + if 'Link' not in r.headers: + break + + relations = [ + ( + url.removeprefix('<').removesuffix('>'), + rel.removeprefix('rel="').removesuffix('"') + ) + for url, rel in + map(lambda p: p.split('; '), r.headers['Link'].split(', ')) + ] + rel_next = [url for url, rel in relations if rel == 'next'] + if len(rel_next) == 0: + break + url = rel_next[0] + + return full_response class UserAccount: def __init__(self, token: dict) -> None: @@ -29,10 +54,8 @@ class UserAccount: return r.json() @functools.cached_property - def mutes(self) -> dict: - r = self.client.get('/api/v1/mutes') - r.raise_for_status() - return r.json() + def mutes(self) -> list[dict]: + return paginated_get(self.client, '/api/v1/mutes') def __repr__(self) -> str: return f''