25 lines
733 B
Python
25 lines
733 B
Python
"""Fetch records from an OData v4 JSON endpoint, following pagination."""
|
|
|
|
import httpx
|
|
|
|
|
|
async def fetch(
|
|
url: str,
|
|
username: str | None = None,
|
|
password: str | None = None,
|
|
) -> list[dict]:
|
|
auth = (username, password) if username and password else None
|
|
records: list[dict] = []
|
|
next_url: str | None = url
|
|
|
|
headers = {"Accept": "application/json"}
|
|
async with httpx.AsyncClient(timeout=30.0) as client:
|
|
while next_url:
|
|
response = await client.get(next_url, auth=auth, headers=headers)
|
|
response.raise_for_status()
|
|
body = response.json()
|
|
records.extend(body.get("value", []))
|
|
next_url = body.get("@odata.nextLink")
|
|
|
|
return records
|