Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow specifying the username #225

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ download(url, [ output = tempname() ];
[ verbose = false, ]
[ debug = <none>, ]
[ downloader = <default>, ]
[ username = <none>, ]
) -> output
```
- `url :: AbstractString`
Expand All @@ -45,6 +46,7 @@ download(url, [ output = tempname() ];
- `verbose :: Bool`
- `debug :: (type, message) --> Any`
- `downloader :: Downloader`
- `username :: AbstractString`

Download a file from the given url, saving it to `output` or if not specified, a
temporary path. The `output` can also be an `IO` handle, in which case the body
Expand Down Expand Up @@ -97,6 +99,7 @@ request(url;
[ debug = <none>, ]
[ throw = true, ]
[ downloader = <default>, ]
[ username = <none>, ]
) -> Union{Response, RequestError}
```
- `url :: AbstractString`
Expand All @@ -110,6 +113,7 @@ request(url;
- `debug :: (type, message) --> Any`
- `throw :: Bool`
- `downloader :: Downloader`
- `username :: AbstractString`

Make a request to the given url, returning a `Response` object capturing the
status, headers and other information about the response. The body of the
Expand Down
1 change: 1 addition & 0 deletions src/Curl/Curl.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export
set_url,
set_method,
set_verbose,
set_username,
set_debug,
set_body,
set_upload_size,
Expand Down
4 changes: 4 additions & 0 deletions src/Curl/Easy.jl
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,10 @@ function set_verbose(easy::Easy, verbose::Bool)
setopt(easy, CURLOPT_VERBOSE, verbose)
end

function set_username(easy::Easy, username::AbstractString)
setopt(easy, CURLOPT_USERNAME, username)
end

function set_debug(easy::Easy, debug::Function)
hasmethod(debug, Tuple{String,String}) ||
throw(ArgumentError("debug callback must take (::String, ::String)"))
Expand Down
11 changes: 11 additions & 0 deletions src/Downloads.jl
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ end
[ verbose = false, ]
[ debug = <none>, ]
[ downloader = <default>, ]
[ username = <none>, ]
) -> output

url :: AbstractString
Expand All @@ -189,6 +190,7 @@ end
verbose :: Bool
debug :: (type, message) --> Any
downloader :: Downloader
username :: AbstractString

Download a file from the given url, saving it to `output` or if not specified, a
temporary path. The `output` can also be an `IO` handle, in which case the body
Expand Down Expand Up @@ -253,6 +255,7 @@ function download(
verbose :: Bool = false,
debug :: Union{Function, Nothing} = nothing,
downloader :: Union{Downloader, Nothing} = nothing,
username :: Union{AbstractString, Nothing} = nothing,
) :: ArgWrite
arg_write(output) do output
response = request(
Expand All @@ -265,6 +268,7 @@ function download(
verbose = verbose,
debug = debug,
downloader = downloader,
username = username,
)::Response
status_ok(response) && return output
throw(RequestError(url, Curl.CURLE_OK, "", response))
Expand All @@ -285,6 +289,7 @@ end
[ debug = <none>, ]
[ throw = true, ]
[ downloader = <default>, ]
[ username = <none>, ]
) -> Union{Response, RequestError}

url :: AbstractString
Expand All @@ -298,6 +303,7 @@ end
debug :: (type, message) --> Any
throw :: Bool
downloader :: Downloader
username :: AbstractString

Make a request to the given url, returning a `Response` object capturing the
status, headers and other information about the response. The body of the
Expand Down Expand Up @@ -329,6 +335,7 @@ function request(
debug :: Union{Function, Nothing} = nothing,
throw :: Bool = true,
downloader :: Union{Downloader, Nothing} = nothing,
username :: Union{AbstractString, Nothing} = nothing,
) :: Union{Response, RequestError}
if downloader === nothing
lock(DOWNLOAD_LOCK) do
Expand Down Expand Up @@ -359,6 +366,10 @@ function request(
set_debug(easy, debug)
add_headers(easy, headers)

if !isnothing(username)
set_username(easy, username)
end

# libcurl does not set the default header reliably so set it
# explicitly unless user has specified it, xref
# https://github.com/JuliaLang/Pkg.jl/pull/2357
Expand Down