66from dataclasses import dataclass , field
77from io import BufferedReader , FileIO
88from pathlib import Path
9- from typing import Any , List , Literal , Optional , Union , cast
9+ from typing import Any , Dict , List , Literal , Optional , Union , cast
1010
1111from httpx import Client , Headers , HTTPStatusError , Response
1212from yarl import URL
@@ -77,10 +77,14 @@ def _request(
7777 )
7878 response .raise_for_status ()
7979 except HTTPStatusError as exc :
80- resp = exc .response .json ()
81- raise StorageApiError (
82- resp ["message" ], resp ["error" ], resp ["statusCode" ]
83- ) from exc
80+ try :
81+ resp = exc .response .json ()
82+ raise StorageApiError (
83+ resp ["message" ], resp ["error" ], resp ["statusCode" ]
84+ ) from exc
85+ except KeyError as err :
86+ message = f"Unable to parse error message: { resp .text } "
87+ raise StorageApiError (message , "InternalError" , 400 ) from err
8488
8589 # close the resource before returning the response
8690 if files and "file" in files and isinstance (files ["file" ][1 ], BufferedReader ):
@@ -436,7 +440,12 @@ def list(
436440 )
437441 return response .json ()
438442
439- def download (self , path : str , options : Optional [DownloadOptions ] = None ) -> bytes :
443+ def download (
444+ self ,
445+ path : str ,
446+ options : Optional [DownloadOptions ] = None ,
447+ query_params : Optional [Dict [str , str ]] = None ,
448+ ) -> bytes :
440449 """
441450 Downloads a file.
442451
@@ -445,20 +454,23 @@ def download(self, path: str, options: Optional[DownloadOptions] = None) -> byte
445454 path
446455 The file path to be downloaded, including the path and file name. For example `folder/image.png`.
447456 """
448- url_options = options or {}
457+ url_options = options or DownloadOptions ()
449458 render_path = (
450459 ["render" , "image" , "authenticated" ]
451460 if url_options .get ("transform" )
452461 else ["object" ]
453462 )
454463
455- transform_options = url_options .get ("transform" ) or {}
464+ transform_options = url_options .get ("transform" ) or TransformOptions ()
456465
457466 path_parts = relative_path_to_parts (path )
458467 response = self ._request (
459468 "GET" ,
460469 [* render_path , self .id , * path_parts ],
461- query_params = transform_to_dict (transform_options ),
470+ query_params = {
471+ ** transform_to_dict (transform_options ),
472+ ** (query_params or {}),
473+ },
462474 )
463475 return response .content
464476
0 commit comments