Skip to content

Commit f491b96

Browse files
Enable setting default Downloader (#207)
1 parent ab5c938 commit f491b96

File tree

3 files changed

+58
-2
lines changed

3 files changed

+58
-2
lines changed

README.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,11 @@ Julia 1.3 through 1.5 as well.
1414

1515
## API
1616

17-
The public API of `Downloads` consists of two functions and three types:
17+
The public API of `Downloads` consists of three functions and three types:
1818

1919
- `download` — download a file from a URL, erroring if it can't be downloaded
2020
- `request` — request a URL, returning a `Response` object indicating success
21+
- `default_downloader!` - set the default `Downloader` object
2122
- `Response` — a type capturing the status and other metadata about a request
2223
- `RequestError` — an error type thrown by `download` and `request` on error
2324
- `Downloader` — an object encapsulating shared resources for downloading
@@ -128,6 +129,18 @@ be downloaded (indicated by non-2xx status code), `request` returns a `Response`
128129
object no matter what the status code of the response is. If there is an error
129130
with getting a response at all, then a `RequestError` is thrown or returned.
130131

132+
### default_downloader!
133+
134+
```jl
135+
default_downloader!(
136+
downloader = <none>
137+
)
138+
```
139+
- `downloader :: Downloader`
140+
141+
Set the default `Downloader`. If no argument is provided, resets the default downloader
142+
so that a fresh one is created the next time the default downloader is needed.
143+
131144
### Response
132145

133146
```jl

src/Downloads.jl

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ using ArgTools
2020
include("Curl/Curl.jl")
2121
using .Curl
2222

23-
export download, request, Downloader, Response, RequestError
23+
export download, request, Downloader, Response, RequestError, default_downloader!
2424

2525
## public API types ##
2626

@@ -423,4 +423,21 @@ function content_length(headers::Union{AbstractVector, AbstractDict})
423423
return nothing
424424
end
425425

426+
"""
427+
default_downloader!(
428+
downloader = <none>
429+
)
430+
431+
downloader :: Downloader
432+
433+
Set the default `Downloader`. If no argument is provided, resets the default downloader so that a fresh one is created the next time the default downloader is needed.
434+
"""
435+
function default_downloader!(
436+
downloader :: Union{Downloader, Nothing} = nothing
437+
)
438+
lock(DOWNLOAD_LOCK) do
439+
DOWNLOADER[] = downloader
440+
end
441+
end
442+
426443
end # module

test/runtests.jl

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,32 @@ include("setup.jl")
253253
@test isempty(cookies)
254254
end
255255

256+
@testset "default_downloader!" begin
257+
original = Downloads.DOWNLOADER[]
258+
try
259+
tripped = false
260+
url = "$server/get"
261+
262+
downloader = Downloader()
263+
downloader.easy_hook = (easy, info) -> tripped = true
264+
265+
# set default
266+
default_downloader!(downloader)
267+
_ = download_body(url)
268+
@test tripped
269+
270+
#reset tripwire
271+
tripped = false
272+
273+
# reset default
274+
default_downloader!()
275+
_ = download_body(url)
276+
@test !tripped
277+
finally
278+
Downloads.DOWNLOADER[] = original
279+
end
280+
end
281+
256282
@testset "netrc support" begin
257283
user = "gVvkQiHN62"
258284
passwd = "dlctfSMTno8n"

0 commit comments

Comments
 (0)