Skip to content

Commit 21145d4

Browse files
add deserialize_file() (#51)
* add deserialize_file() * change version number * remove overwrite * add extract_filename()
1 parent 522fc2d commit 21145d4

File tree

2 files changed

+53
-4
lines changed

2 files changed

+53
-4
lines changed

Project.toml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ keywords = ["Swagger", "OpenAPI", "REST"]
44
license = "MIT"
55
desc = "OpenAPI server and client helper for Julia"
66
authors = ["JuliaHub Inc."]
7-
version = "0.1.14"
7+
version = "0.1.15"
88

99
[deps]
1010
Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"
@@ -13,6 +13,7 @@ Downloads = "f43a241f-c20a-4ad4-852c-f6b1247861c6"
1313
HTTP = "cd3eb016-35fb-5094-929b-558a96fad6f3"
1414
JSON = "682c06a0-de6a-54ab-a142-c8b1cf79cde6"
1515
LibCURL = "b27032c2-a3e7-50c8-80cd-2d36dbcbfd21"
16+
MIMEs = "6c6e2e6c-3030-632d-7369-2d6c69616d65"
1617
MbedTLS = "739be429-bea8-5141-9913-cc70e7f3736d"
1718
TimeZones = "f269a46b-ccf7-5d73-abea-4c690281aa53"
1819
URIs = "5c2747f8-b7ea-4ff2-ba2e-563bfd36b1d4"
@@ -26,14 +27,15 @@ MbedTLS = "0.6.8, 0.7, 1"
2627
TimeZones = "1"
2728
URIs = "1.3"
2829
julia = "1.6"
30+
MIMEs = "0.1"
2931

3032
[extras]
33+
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
34+
HTTP = "cd3eb016-35fb-5094-929b-558a96fad6f3"
3135
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
3236
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
33-
URIs = "5c2747f8-b7ea-4ff2-ba2e-563bfd36b1d4"
34-
HTTP = "cd3eb016-35fb-5094-929b-558a96fad6f3"
35-
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
3637
TimeZones = "f269a46b-ccf7-5d73-abea-4c690281aa53"
38+
URIs = "5c2747f8-b7ea-4ff2-ba2e-563bfd36b1d4"
3739

3840
[targets]
3941
test = ["Test", "Random", "URIs", "HTTP", "Dates", "TimeZones"]

src/client.jl

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ using Dates
88
using TimeZones
99
using LibCURL
1010
using HTTP
11+
using MIMEs
1112

1213
import Base: convert, show, summary, getproperty, setproperty!, iterate
1314
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
599600
is_request_interrupted(ex::CompositeException) = any(is_request_interrupted, ex.exceptions)
600601
is_request_interrupted(ex::InvocationException) = ex.reason == "request was interrupted"
601602

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+
602649
end # module Clients

0 commit comments

Comments
 (0)