Current section
Files
Jump to
Current section
Files
lib/opencadc_registry/client.ex
defmodule OpencadcRegistry.Client do
@moduledoc """
IVOA compliant Registry client to lookup service URL endpoints.
"""
alias OpencadcRegistry.Parser
@doc """
Lookup the URL that can be used to obtain the Capabilities ASCII document. Specify the REGISTRY_ENDPOINT_HOST
environment variable as <http(s)://myhost.com> or leave the default to use the CADC's Production host address.
This assumes that the host will support the /reg/resource-caps endpoint.
Capability XML document specification here: http://www.ivoa.net/xml/VOSICapabilities/v1.0
Sample CADC document containing Capabilities URLs: http://www.cadc-ccda.hia-iha.nrc-cnrc.gc.ca/reg/resource-caps
Returns a string URL.
## Examples
iex> OpencadcRegistryClient.lookup_capabilities_url("ivo://cadc.nrc.ca/vospace")
"http://www.canfar.phys.uvic.ca/vospace/capabilities"
@param resource_url The Resource URI to use to lookup the URL.
"""
def lookup_capabilities_url(resource_url) do
registry_entries(
System.get_env("REGISTRY_ENDPOINT_HOST") || "http://www.cadc-ccda.hia-iha.nrc-cnrc.gc.ca"
)
|> handle_response(resource_url)
end
@doc """
Lookup the Service URL to be used to issue requests to. This function will navigate the Capabilities XML document
and pull the appropriate public (non-secure) service URL.
Capability XML document specification here: http://www.ivoa.net/xml/VOSICapabilities/v1.0
Sample CADC document containing Capabilities URLs: http://www.cadc-ccda.hia-iha.nrc-cnrc.gc.ca/reg/resource-caps
Returns a string URL.
## Examples
iex> OpencadcRegistryClient.lookup_service_url("ivo://cadc.nrc.ca/vospace", "ivo://ivoa.net/std/VOSpace/v2.0#nodes", "vs:ParamHTTP")
"http://www.canfar.phys.uvic.ca/vospace/nodes"
@param resource_url The Resource URI to use to lookup the Capabilities URL.
@param standard_url The Standard URI identified on the capability.
@param xsi_type The XSI value used in the capability's interface attribute.
"""
def lookup_service_url(resource_url, standard_url, xsi_type) do
with cap_response = lookup_capabilities_url(resource_url) |> HTTPoison.get!() do
Parser.all(
Parser.from_string(cap_response.body),
"//capability[@standardID=\"#{standard_url}\"]/interface[@xsi:type=\"#{xsi_type}\"][not(securityMethod)]/accessURL"
)
|> extract_service_url
end
end
@doc """
Lookup the Service URL to be used to issue requests to. This function will navigate the Capabilities XML document
and pull the appropriate public secure service URL. The
Capability XML document specification here: http://www.ivoa.net/xml/VOSICapabilities/v1.0
Sample CADC document containing Capabilities URLs: http://www.cadc-ccda.hia-iha.nrc-cnrc.gc.ca/reg/resource-caps
Returns a string URL.
## Examples
iex> OpencadcRegistryClient.lookup_service_url("ivo://cadc.nrc.ca/vospace", "ivo://ivoa.net/std/VOSpace/v2.0#nodes", "vs:ParamHTTP", "ivo://ivoa.net/sso#tls-with-certificate")
"https://www.canfar.phys.uvic.ca/vospace/nodes"
@param resource_url The Resource URI to use to lookup the Capabilities URL.
@param standard_url The Standard URI identified on the capability.
@param xsi_type The XSI value used in the capability's interface attribute.
@param security_uri The URI identifying what kind of security to use. Valid values are
"""
def lookup_service_url(resource_url, standard_url, xsi_type, security_uri) do
with cap_response = lookup_capabilities_url(resource_url) |> HTTPoison.get!(),
capabilities_url = cap_response.body do
Parser.all(
Parser.from_string(capabilities_url),
"//capability[@standardID=\"#{standard_url}\"]/interface[@xsi:type=\"#{xsi_type}\"][securityMethod[@standardID=\"#{
security_uri
}\"]]/accessURL"
)
|> extract_service_url
end
end
defp registry_entries(host) do
(host <> "/reg/resource-caps") |> HTTPoison.get!()
end
defp extract_capabilities_url([]) do
raise ArgumentError, message: "No capabilities found"
end
defp extract_capabilities_url(capabilities) do
capabilities |> Enum.at(0) |> String.split("=") |> Enum.at(1) |> String.trim()
end
defp handle_response(%HTTPoison.Response{status_code: 200} = response, resource_url) do
response.body
|> String.split("\n", trim: true)
|> Enum.filter(fn str -> String.starts_with?(str, resource_url) == true end)
|> extract_capabilities_url
end
defp handle_response(response, _) do
raise ArgumentError,
message: "Unable to query service registry: Response code #{response.status_code}"
end
defp extract_service_url([]) do
raise ArgumentError, message: "No service endpoints found."
end
defp extract_service_url(endpoints) do
endpoints |> Enum.at(0) |> Parser.text()
end
end