Skip to content

Commit

Permalink
Fix the constructor for BaseURL when the argument is a string (#123)
Browse files Browse the repository at this point in the history
* Fix the constructor for `BaseURL` when the argument is a string

* Bump the version from "2.0.0" to "2.0.1"

* Fix a typo

* Add more tests

* Fix some mistakes

* Fix the tests

* Fix the tests
  • Loading branch information
DilumAluthge authored Feb 28, 2023
1 parent c3bafc5 commit a46d92f
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 16 deletions.
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

2 comments on commit a46d92f

@DilumAluthge
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/78700

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v2.0.1 -m "<description of version>" a46d92fa0248e890e96d7306c108a4c6772852ca
git push origin v2.0.1

Please sign in to comment.