@@ -8,6 +8,7 @@ using Dates
8
8
using TimeZones
9
9
using LibCURL
10
10
using HTTP
11
+ using MIMEs
11
12
12
13
import Base: convert, show, summary, getproperty, setproperty!, iterate
13
14
import .. OpenAPI: APIModel, UnionAPIModel, OneOfAPIModel, AnyOfAPIModel, APIClientImpl, OpenAPIException, InvocationException, to_json, from_json, validate_property, property_type
@@ -599,4 +600,50 @@ is_request_interrupted(ex::TaskFailedException) = is_request_interrupted(ex.task
599
600
is_request_interrupted (ex:: CompositeException ) = any (is_request_interrupted, ex. exceptions)
600
601
is_request_interrupted (ex:: InvocationException ) = ex. reason == " request was interrupted"
601
602
603
+
604
+ """
605
+ deserialize_file(api_call::Function;
606
+ folder_path::String=pwd(),
607
+ rename_file::String="",
608
+ overwrite::Bool=true
609
+ )::Tuple{Any,ApiResponse,String}
610
+
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).
617
+ """
618
+ function deserialize_file (api_call:: Function ;
619
+ folder_path:: String = pwd (),
620
+ rename_file:: String = " " ,
621
+ ):: Tuple{Any,ApiResponse,String}
622
+
623
+ result, http_response = api_call ()
624
+
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))
634
+ end
635
+
636
+ file_path = joinpath (mkpath (folder_path),file_name)
637
+ open (file_path," w" ) do file
638
+ write (file,result)
639
+ end
640
+ return result, http_response, file_path
641
+ end
642
+
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 ]
647
+ end
648
+
602
649
end # module Clients
0 commit comments