JohnPaton / airbase

🌬 An easy downloader for the AirBase air quality data.
https://airbase.readthedocs.io
MIT License
8 stars 4 forks source link

Implement functional API for dowloading #29

Open JohnPaton opened 2 years ago

JohnPaton commented 2 years ago

Like requests, create a new api that basically bypasses the client (by handling it internally) so that users can jump directly to download_*ing the data.

avaldebe commented 2 years ago

What do you have in mind? I think that the functionality from AirbaseClient and AirbaseRequestcould be neatly combined into a context manager class that works something like:

from airbase import AirbaseSession

with AirbaseSession(country=["NL", "DE"], pollutant="NO3", year_from=2015) as session:
    session.download_to_directory(dir="data", skip_existing=True)
Generating CSV download links...
100%|██████████| 2/2 [00:03<00:00,  2.03s/it]
Generated 12 CSV links ready for downloading
Downloading CSVs to data...
100%|██████████| 12/12 [00:01<00:00,  8.44it/s]

The context manager will help with clean up the asyncio loop of unfinished tasks after receiving an KeyboardInterrupt.

JohnPaton commented 2 years ago

Ah I hadn't thought about the unfinished tasks now that it's async. What I had in mind was basically just providing top-level functions corresponding to the methods of the Client, and instantiating a Client as needed in the background (now that it doesn't take any arguments anyway), so that users don't need to bother with the Client at all. They would just get Request objects directly that they can download however they would like. This is because scientific users often prefer the minimum of code/complexity and just want to get things done :)

I think given that, the ContextManager doesn't really make it clearer what's going on. And tbh having one line instantiating the client isn't so much of a burden anyway, so maybe it's best to just leave it as is?