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

Fix the constructor for BaseURL when the argument is a string #123

Merged
merged 7 commits into from
Feb 28, 2023
Merged
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
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "FHIRClient"
uuid = "b44d2ca2-8176-4fa9-8684-826e17b2a2da"
authors = ["Dilum Aluthge", "Rhode Island Quality Institute", "contributors"]
version = "2.0.0"
version = "2.0.1"

[deps]
Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"
Expand Down
37 changes: 25 additions & 12 deletions src/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ abstract type FHIRVersion <: Any
abstract type FHIRVersion
end

function _uses_https(url_str::AbstractString)
@inline function _uses_https(url_str::AbstractString)
return startswith(lowercase(strip(url_str)), "https://")
end

function _uses_https(uri::HTTP.URI)
@inline function _uses_https(uri::HTTP.URI)
return _uses_https(Base.string(uri))
end

Expand All @@ -38,6 +38,7 @@ struct BaseURL <: Any
"""
struct BaseURL
uri::HTTP.URI

function BaseURL(uri::HTTP.URI; require_https::Bool = true)
this_uri_uses_https = _uses_https(uri)
if !this_uri_uses_https
Expand All @@ -51,22 +52,34 @@ struct BaseURL
end
return new(uri)
end

@doc """
BaseURL(base_url::AbstractString)

Construct a `BaseURL` object given the base URL.

The base URL is also called the "Service Root URL"
"""
function BaseURL(uri::AbstractString; require_https::Bool = true)
this_uri_uses_https = _uses_https(uri)
if !this_uri_uses_https
msg = "The following FHIR Base URL does not use HTTPS: $(uri)"
if require_https
throw(ArgumentError(msg))
else
@warn "`require_https` is set to `false` - we strongly recommend setting it to `true`"
@warn msg
end
end
return new(HTTP.URI(uri))
end
end

_get_http_uri(base_url::BaseURL) = base_url.uri
function _get_http_uri_string(uri::HTTP.URI)::String
return Base.string(uri)
end

"""
BaseURL(base_url::AbstractString)

Construct a `BaseURL` object given the base URL.

The base URL is also called the "Service Root URL"
"""
BaseURL(base_url::AbstractString) = BaseURL(HTTP.URI(base_url))


"""
A FHIR client.

Expand Down
19 changes: 16 additions & 3 deletions test/unit/types.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
@test_throws Exception FHIRClient.BaseURL(HTTP.URI("http://example.com"); require_https = true)
@test FHIRClient.BaseURL(HTTP.URI("http://example.com"); require_https = false) isa FHIRClient.BaseURL
@test_logs (:warn,) match_mode=:any FHIRClient.BaseURL(HTTP.URI("http://example.com"); require_https = false)
@testset "BaseURL constructor" begin
https_uris = ["https://example.com", HTTP.URI("https://example.com")]
@testset for uri in https_uris
@test FHIRClient.BaseURL(uri) isa FHIRClient.BaseURL
@test FHIRClient.BaseURL(uri; require_https = true) isa FHIRClient.BaseURL
@test FHIRClient.BaseURL(uri; require_https = false) isa FHIRClient.BaseURL
end

http_uris = ["http://example.com", HTTP.URI("http://example.com")]
@testset for uri in http_uris
@test_throws Exception FHIRClient.BaseURL(uri)
@test_throws Exception FHIRClient.BaseURL(uri; require_https = true)
@test FHIRClient.BaseURL(uri; require_https = false) isa FHIRClient.BaseURL
@test_logs (:warn,) match_mode=:any FHIRClient.BaseURL(uri; require_https = false)
end
end