Thanks for the response. Hmm I have pasted the code below but after some more tests I have found that if I run function from in the file it works as intended. It is only once the module is imported that the error begins. Not sure how this would effect my parameters though.
module Longurl
export expand_urls
using HTTP
"""
Urls(expanded_url, status_code)
"""
struct Urls
expanded_url::Vector{Union{String, Nothing}}
status_code::Vector{Union{String, Nothing}}
end
"""
Takes a list of short urls and expands them into their long form
...
# Arguments
- `urls_to_expand::Array`: the list of short urls
- `seconds::Int64`: the timeout in seconds
# Returns
- `Urls`: Struct containing properties expanded_url and status_code
...
"""
function expand_urls(urls_to_expand::A, seconds::N=2) where {A<:Union{String,Vector{String}}, N <: Number}
if urls_to_expand isa String
urls_to_expand = [urls_to_expand]
end
original_stdout = stdout
original_error = stderr
short_urls = Vector{Union{String, Nothing}}(nothing, length(urls_to_expand))
expanded_urls = Vector{Union{String, Nothing}}(nothing, length(urls_to_expand))
status_codes = Vector{Union{String, Nothing}}(nothing, length(urls_to_expand))
i = 0
for url in urls_to_expand
i += 1
last_head = nothing
last_host = nothing
last_code = nothing
(rd, wr) = redirect_stdout()
try
HTTP.head(url, readtimeout=seconds, verbose=2, retry=false)
catch e
print(e)
finally
redirect_stdout(original_stdout)
close(wr)
for line in readlines(rd)
if occursin("HEAD", line)
last_head = split(line, " ")[2]
end
if occursin("Host", line)
last_host = split(line, " ")[2]
end
if occursin("HTTP/1.1 ", line)
last_code = split(line, " ")[2]
end
end
short_urls[i] = url
status_codes[i] = last_code
expanded_urls[i] = last_host * last_head
end
end
long_urls = Urls(expanded_urls, status_codes)
return long_urls
end
end
Aha that makes sense! However I have now added a check to make sure if it is in fact a string it gets converted to a vector containing said string but I still seem to be getting the problem.
EDIT: I have figured out this problem is more down to my misunderstanding of the Julia package system. Thanks for all the help, it seems that whenever I called Pkg.add and using it was using an old version of the package even if I activated the environment with the new package. I went through and removed the old package files from my .julia and re added them and the issue was gone.
This sort of design isn’t normally ideal. I wouldn’t write the function to accept both strings and vectors of strings. It’s cleaner and less error prone to write your function to accept strings, and then iterate outside your expand_urls function (and call it expand_url), using for example broadcasting.
And then consider defining your Urls struct to only accept scalars and create a vector of Urls, instead of holding the vectors inside a single object.