55from pathlib import Path
66from typing import TYPE_CHECKING , Any , cast
77
8- import aiohttp
9- import aiohttp .client_exceptions
108import backoff
9+ import curl_cffi
1110from aiofiles import tempfile
11+ from curl_cffi import requests
1212
1313from zipcode_coordinates_tz import constants
1414
1818logger = logging .getLogger (__name__ )
1919
2020
21- @backoff .on_exception (backoff .expo , aiohttp . ClientError , max_tries = constants .MAX_RETRIES , max_time = constants .MAX_RETRY_TIME )
21+ @backoff .on_exception (backoff .expo , requests . exceptions . RequestException , max_tries = constants .MAX_RETRIES , max_time = constants .MAX_RETRY_TIME )
2222@asynccontextmanager
23- async def get_json (session : aiohttp . ClientSession , url : str , params : dict [str , Any ] | None = None ) -> AsyncIterator [dict [str , Any ]]:
23+ async def get_json (session : requests . AsyncSession , url : str , params : dict [str , Any ] | None = None ) -> AsyncIterator [dict [str , Any ]]:
2424 """
2525 Executes a get request returning a json payload
2626
2727 Args:
28- session (aiohttp.ClientSession ): The Session.
28+ session (requests.AsyncSession ): The Session.
2929 url (str): The url to the file to download.
3030
3131 Returns:
3232 An Iterator that contains the json payload.
3333 """
34- async with session .get (url , params = params , raise_for_status = True ) as response :
35- yield await response .json ()
34+ response = await session .get (url )
35+ response .raise_for_status ()
36+ data : dict [str , Any ] = await response .json ()
37+ yield data
3638
3739
38- @backoff .on_exception (backoff .expo , aiohttp . ClientError , max_tries = constants .MAX_RETRIES , max_time = constants .MAX_RETRY_TIME )
40+ @backoff .on_exception (backoff .expo , requests . exceptions . RequestException , max_tries = constants .MAX_RETRIES , max_time = constants .MAX_RETRY_TIME )
3941@asynccontextmanager
40- async def get_and_download_file (session : aiohttp . ClientSession , url : str ) -> AsyncIterator [Path ]:
42+ async def get_and_download_file (session : requests . AsyncSession , url : str ) -> AsyncIterator [Path ]:
4143 """
4244 Downloads a file from the specified url and returns the Path.
4345
4446 Args:
45- session (aiohttp.ClientSession ): The Session.
47+ session (requests.AsyncSession ): The Session.
4648 url (str): The url to the file to download.
4749
4850 Returns:
4951 An Iterator that contains the Path to the downloaded file.
5052 """
5153 url_path = Path (url )
5254 logger .debug ("Downloading %s" , url )
53- async with (
54- session .get (url , raise_for_status = True ) as response ,
55- tempfile .NamedTemporaryFile (prefix = url_path .with_suffix ("" ).name , suffix = url_path .suffix , delete = False ) as f ,
56- ):
55+ async with tempfile .NamedTemporaryFile (prefix = url_path .with_suffix ("" ).name , suffix = url_path .suffix , delete = False ) as f :
56+ response = await session .get (url , stream = True )
57+ response .raise_for_status ()
5758 download_path = Path (cast ("str" , f .name ))
5859 logger .debug ("Saving %s to %s" , url , download_path )
59- async for chunk in response .content . iter_chunked ( constants .BUFFER_LENGTH ):
60+ async for chunk in response .aiter_content ( chunk_size = constants .BUFFER_LENGTH ):
6061 await f .write (chunk )
6162
6263 await f .flush ()
@@ -69,28 +70,27 @@ async def get_and_download_file(session: aiohttp.ClientSession, url: str) -> Asy
6970 download_path .unlink ()
7071
7172
72- @backoff .on_exception (backoff .expo , aiohttp . ClientError , max_tries = constants .MAX_RETRIES , max_time = constants .MAX_RETRY_TIME )
73+ @backoff .on_exception (backoff .expo , requests . exceptions . RequestException , max_tries = constants .MAX_RETRIES , max_time = constants .MAX_RETRY_TIME )
7374@asynccontextmanager
74- async def post_and_download_file (session : aiohttp . ClientSession , url : str , params : dict [str , Any ], data : aiohttp . FormData ) -> AsyncIterator [Path ]:
75+ async def post_and_download_file (session : requests . AsyncSession , url : str , params : dict [str , Any ], mp : curl_cffi . CurlMime ) -> AsyncIterator [Path ]:
7576 """
7677 Downloads a file from the specified url and returns the Path.
7778
7879 Args:
79- session (aiohttp.ClientSession ): The Session.
80+ session (requests.AsyncSession ): The Session.
8081 url (str): The url to the file to download.
8182
8283 Returns:
8384 An Iterator that contains the Path to the downloaded file.
8485 """
8586 url_path = Path (url )
8687 logger .debug ("Downloading %s" , url )
87- async with (
88- session .post (url , data = data , params = params , raise_for_status = True ) as response ,
89- tempfile .NamedTemporaryFile (prefix = url_path .with_suffix ("" ).name , suffix = url_path .suffix , delete = False ) as f ,
90- ):
88+ async with tempfile .NamedTemporaryFile (prefix = url_path .with_suffix ("" ).name , suffix = url_path .suffix , delete = False ) as f :
89+ response = await session .post (url , multipart = mp , params = params , stream = True )
90+ response .raise_for_status ()
9191 download_path = Path (cast ("str" , f .name ))
9292 logger .debug ("Saving %s to %s" , url , download_path )
93- async for chunk in response .content . iter_chunked ( constants .BUFFER_LENGTH ):
93+ async for chunk in response .aiter_content ( chunk_size = constants .BUFFER_LENGTH ):
9494 await f .write (chunk )
9595
9696 await f .flush ()
0 commit comments