|
| 1 | +""" |
| 2 | +The async client can be used as a replacement for the synchronous |
| 3 | +IndicoClient to make concurrent calls to the platform |
| 4 | +""" |
| 5 | + |
| 6 | +import asyncio |
| 7 | +from typing import AsyncIterator, List |
| 8 | + |
| 9 | +from indico import AsyncIndicoClient, IndicoConfig |
| 10 | +from indico.queries import CreateDataset |
| 11 | +from indico.queries.datasets import GetDataset, ListDatasets |
| 12 | +from indico.types.dataset import Dataset |
| 13 | +from indico.types.submission import Submission |
| 14 | + |
| 15 | +""" |
| 16 | +Example illustrating how to use the client in typed contexts |
| 17 | +""" |
| 18 | + |
| 19 | +config = IndicoConfig(host="try.indico.io") |
| 20 | + |
| 21 | + |
| 22 | +async def main(): |
| 23 | + async with AsyncIndicoClient(config=config) as client: |
| 24 | + ipa_version: str = await client.get_ipa_version() |
| 25 | + print(ipa_version) |
| 26 | + |
| 27 | + filename: str = "my_file_for_all_datasets.pdf" |
| 28 | + |
| 29 | + # CreateDataset is typed to return a Dataset, so multiple concurrent calls |
| 30 | + # via asyncio.gather should, and does, return List[Dataset] |
| 31 | + datasets: List[Dataset] = await asyncio.gather( |
| 32 | + *( |
| 33 | + client.call(CreateDataset(name=f"My Dataset {i}", files=[filename])) |
| 34 | + for i in range(1, 4) |
| 35 | + ) |
| 36 | + ) |
| 37 | + assert len(datasets) == 3 |
| 38 | + |
| 39 | + # paginated calls are also properly typed |
| 40 | + pages: AsyncIterator[List[Dataset]] = client.paginate(ListDatasets()) |
| 41 | + async for datasets in pages: |
| 42 | + for d in datasets: |
| 43 | + print(d.id) |
| 44 | + |
| 45 | + # incorrect typing will throw mypy / ide linting errors when using those tools. |
| 46 | + # here, Pyright correctly reports '"Dataset" is not the same as "Submission"' |
| 47 | + not_a_submission: Submission = await client.call(GetDataset(datasets[0].id)) |
| 48 | + |
| 49 | + |
| 50 | +if __name__ == "__main__": |
| 51 | + # How to run a Python script using async |
| 52 | + asyncio.run(main()) |
0 commit comments