@@ -602,48 +602,75 @@ is_request_interrupted(ex::InvocationException) = ex.reason == "request was inte
602602
603603
604604"""
605- deserialize_file (api_call::Function;
606- folder_path::String= pwd(),
605+ storefile (api_call::Function;
606+ folder::AbstractString = pwd(),
607607 rename_file::String="",
608- overwrite::Bool=true
609608 )::Tuple{Any,ApiResponse,String}
610609
611- Saves response body into a file in a temporary folder,
612- using the filename from the `Content-Disposition` header if provided.
613- - `api_call`: API function that return `(result, http_response)` Tuple.
614- - `folder_path`: file save location, default value is `pwd()``.
615- - `rename_file`: rename the file, default value is `""`.
616- - return: (result, http_response, file_path).
610+ Helper method that stores the result of an API call that returns file
611+ contents (as binary or text string) into a file.
612+
613+ Convenient to use it in a do block. Returns the path where file is stored additionally.
614+
615+ E.g.:
616+ ```
617+ _result, _http_response, file = OpenAPI.Clients.storefile() do
618+ # Invoke the OpenaPI method that returns file contents.
619+ # This is the method that returns a tuple of (result, http_response).
620+ # The result is the file contents as binary or text string.
621+ fetch_file(api, "reports", "category1")
622+ end
623+ ```
624+
625+ Parameters:
626+
627+ - `api_call`: The OpenAPI function call that returns file contents (as binary or text string). See example in method description.
628+ - `folder`: Location to store file, defaults to `pwd()`.
629+ - `filename`: Use this filename, overrides any filename that may be there in the `Content-Disposition` header.
630+
631+ Returns: (result, http_response, file_path)
617632"""
618- function deserialize_file (api_call:: Function ;
619- folder_path :: String = pwd (),
620- rename_file :: String = " " ,
633+ function storefile (api_call:: Function ;
634+ folder :: AbstractString = pwd (),
635+ filename :: Union{ String,Nothing} = nothing ,
621636 ):: Tuple{Any,ApiResponse,String}
622637
623638 result, http_response = api_call ()
624639
625- content_disposition_str = OpenAPI. Clients. header (http_response. raw," content-disposition" ," " )
626- content_type_str = extract_filename (OpenAPI. Clients. header (http_response. raw," content-type" ," " ))
627-
628- file_name = if ! isempty (rename_file)
629- rename_file
630- elseif ! isempty (content_disposition_str)
631- content_disposition_str
632- else
633- " response" * extension_from_mime (MIME (content_type_str))
640+ if isnothing (filename)
641+ filename = extract_filename (http_response)
634642 end
635643
636- file_path = joinpath (mkpath (folder_path),file_name)
637- open (file_path," w" ) do file
638- write (file,result)
644+ mkpath (folder)
645+ filepath = joinpath (folder, filename)
646+
647+ open (filepath, " w" ) do io
648+ write (io, result)
639649 end
640- return result, http_response, file_path
650+
651+ return result, http_response, filepath
641652end
642653
643- # extract_filename from content-disposition
644- function extract_filename (str:: String ):: String
645- m = match (r" filename=\" (.*?)\" " ,str)
646- return isnothing (m) ? " " : m. captures[1 ]
654+ const content_disposition_re = r" filename\* ?=['\" ]?(?:UTF-\d ['\" ]*)?([^;\r\n\" ']*)['\" ]?;?"
655+
656+ """
657+ extract_filename(resp::Downloads.Response)::String
658+
659+ Extracts the filename from the `Content-Disposition` header of the HTTP response.
660+ If not found, then creates a filename from the `Content-Type` header.
661+ """
662+ extract_filename (resp:: ApiResponse ) = extract_filename (resp. raw)
663+ function extract_filename (resp:: Downloads.Response ):: String
664+ # attempt to extract filename from content-disposition header
665+ content_disposition_str = header (resp, " content-disposition" , " " )
666+ m = match (content_disposition_re, content_disposition_str)
667+ if ! isnothing (m) && ! isempty (m. captures) && ! isnothing (m. captures[1 ])
668+ return m. captures[1 ]
669+ end
670+
671+ # attempt to create a filename from content-type header
672+ content_type_str = header (resp, " content-type" , " " )
673+ return string (" response" , extension_from_mime (MIME (content_type_str)))
647674end
648675
649676end # module Clients
0 commit comments