@@ -52,7 +52,8 @@ def __new__(cls, *args, **kwargs) -> "CodeBox":
5252 """
5353 Creates a CodeBox session
5454 """
55- api_key = kwargs .get ("api_key" ) or os .getenv ("CODEBOX_API_KEY" , "local" )
55+ api_key = kwargs .get ("api_key" ) or os .getenv ("CODEBOX_API_KEY" )
56+ # todo make sure "local" is not hardcoded default
5657 if api_key == "local" :
5758 return import_module ("codeboxapi.local" ).LocalBox (* args , ** kwargs )
5859
@@ -175,6 +176,24 @@ async def ainstall(self, *packages: str) -> str:
175176 )
176177 return " " .join (packages ) + " installed successfully"
177178
179+ async def afile_from_url (self , url : str , file_path : str ) -> "RemoteFile" :
180+ """
181+ Download a file from a URL to the specified destination in the CodeBox.
182+ Example:
183+ >>> codebox.afile_from_url("https://github.com/org/repo/file.txt", "file.txt")
184+ """
185+ code = (
186+ "import httpx\n "
187+ "async with httpx.AsyncClient() as client:\n "
188+ f" async with client.stream('GET', '{ url } ') as response:\n "
189+ " response.raise_for_status()\n "
190+ f" with open('{ file_path } ', 'wb') as f:\n "
191+ " async for chunk in response.aiter_bytes():\n "
192+ " f.write(chunk)\n "
193+ )
194+ await self .aexec (code )
195+ return await self .adownload (file_path )
196+
178197 async def alist_files (self ) -> list ["RemoteFile" ]:
179198 from .types import RemoteFile
180199
@@ -246,6 +265,9 @@ def healthcheck(self) -> str:
246265 def install (self , * packages : str ) -> str :
247266 return syncify (self .ainstall )(* packages )
248267
268+ def file_from_url (self , url : str , file_path : str ) -> "RemoteFile" :
269+ return syncify (self .afile_from_url )(url , file_path )
270+
249271 def list_files (self ) -> list ["RemoteFile" ]:
250272 return syncify (self .alist_files )()
251273
0 commit comments