add support for getting resources via relative urls

This commit is contained in:
Gered 2014-07-08 13:52:54 -04:00
parent cc3b85a2b1
commit 2dda8d9bbf

View file

@ -162,25 +162,41 @@
(strip-bundle-page-links merged))))) (strip-bundle-page-links merged)))))
(defn get-resource (defn get-resource
"gets a single resource from a FHIR server. can optionally get a specific version of a resource. "gets a single resource from a FHIR server. the raw resource itself is returned (that is,
the raw resource itself is returned (that is, it is not contained in a bundle). if the it is not contained in a bundle). if the resource could not be found, nil is returned.
resource could not be found, nil is returned.
a relative url can be used to identify the resource to be retrieved, or a resource type,
id and optional version number can be used.
reference: reference:
read: http://hl7.org/implement/standards/fhir/http.html#read read: http://hl7.org/implement/standards/fhir/http.html#read
vread: http://hl7.org/implement/standards/fhir/http.html#vread" vread: http://hl7.org/implement/standards/fhir/http.html#vread
[base-url type id & {:keys [version]}] relative url: http://hl7.org/implement/standards/fhir/references.html#atom-rel"
(let [resource-name (->fhir-resource-name type) ([base-url relative-resource-url]
url-components (if version (try
["/" resource-name id "_history" version] (fhir-get-request
["/" resource-name id])] base-url
(try relative-resource-url)
(fhir-get-request (catch ExceptionInfo ex
base-url (if (not= 404 (get-in (ex-data ex) [:object :status]))
(apply join-paths url-components)) (throw ex)))))
(catch ExceptionInfo ex ([base-url type id & {:keys [version]}]
(if (not= 404 (get-in (ex-data ex) [:object :status])) (let [resource-name (->fhir-resource-name type)
(throw ex)))))) url-components (if version
["/" resource-name id "_history" version]
["/" resource-name id])]
(get-resource base-url (apply join-paths url-components)))))
(defn get-relative-resource
"gets a single resource from a FHIR server. the server to be queried will be taken from the
'fhir-base' link in the provided bundle."
[bundle relative-url]
(if bundle
(let [base-url (->> (:link bundle)
(filter #(= "fhir-base" (:rel %)))
(first)
:href)]
(get-resource base-url relative-url))))
(defn get-resource-bundle (defn get-resource-bundle
"gets a single resource from a FHIR server. the returned resource will be contained in a "gets a single resource from a FHIR server. the returned resource will be contained in a