2014-07-04 09:04:45 -04:00
|
|
|
(ns clj-hl7-fhir.core
|
2014-07-04 09:40:51 -04:00
|
|
|
(:import (java.util Date)
|
|
|
|
(clojure.lang ExceptionInfo))
|
2014-07-14 10:19:02 -04:00
|
|
|
(:require [clojure.string :as str]
|
2014-09-02 13:01:09 -04:00
|
|
|
[cemerick.url :refer [url]]
|
2014-07-14 10:19:02 -04:00
|
|
|
[cheshire.core :as json])
|
2014-07-04 09:04:45 -04:00
|
|
|
(:use [camel-snake-kebab]
|
|
|
|
[clj-hl7-fhir.util]))
|
|
|
|
|
2014-08-29 09:50:36 -04:00
|
|
|
; HACK: using this dynamic/"with"-wrapping type of API design is arguably a "lazy" design.
|
|
|
|
; in the future I intend to explore reworking the API so as to not require this if
|
|
|
|
; authentication support is needed, but I didn't want to get too held up on it right
|
|
|
|
; now. the problem at the moment is that passing authentication info to the main
|
|
|
|
; FHIR operation functions is that it only works well in the simple cases. usage
|
|
|
|
; of functions like fetch-next-page, fetch-all and get-relative-resource becomes
|
|
|
|
; a little bit messy (have to pass in server/auth info where before none of it
|
|
|
|
; was necessary... kind of gross in my opinion, would rather come up with
|
|
|
|
; something cleaner if at all possible)
|
|
|
|
|
|
|
|
(def ^:dynamic *server-auth* nil)
|
|
|
|
|
|
|
|
(defmacro with-auth
|
|
|
|
"wraps code that performs FHIR operations such that each will have authentication info
|
|
|
|
added to the HTTP requests made. auth should be a map containing one entry where the
|
|
|
|
key is one of :basic-auth, :digest-auth or :oauth-token (authentication headers that
|
|
|
|
clj-http supports)"
|
|
|
|
[auth & body]
|
|
|
|
`(binding [*server-auth* (select-keys ~auth [:basic-auth :digest-auth :oauth-token])]
|
|
|
|
~@body))
|
|
|
|
|
2014-07-04 09:04:45 -04:00
|
|
|
(defn- ->fhir-resource-name [x]
|
|
|
|
(name (->CamelCase x)))
|
|
|
|
|
2014-08-01 15:29:53 -04:00
|
|
|
(defn- fhir-response? [response]
|
|
|
|
(and (map? response)
|
|
|
|
(.contains (get-in response [:headers "Content-Type"]) "application/json+fhir")))
|
|
|
|
|
2014-08-26 14:11:22 -04:00
|
|
|
(defn- fhir-request [type base-url resource-url & {:keys [params body params-as-body? follow-location?]}]
|
2014-08-01 15:07:28 -04:00
|
|
|
(let [query (map->query-string params)
|
2014-08-29 09:50:36 -04:00
|
|
|
auth *server-auth*
|
2014-08-26 14:11:22 -04:00
|
|
|
url (build-url base-url resource-url (if-not params-as-body? query))
|
|
|
|
body (if params-as-body? query body)
|
2014-08-01 15:07:28 -04:00
|
|
|
follow-location? (if (nil? follow-location?) true follow-location?)]
|
2014-07-14 10:19:02 -04:00
|
|
|
(try
|
2014-08-26 14:11:22 -04:00
|
|
|
(let [response (case type
|
2014-08-29 09:50:36 -04:00
|
|
|
:get (http-get-json url auth)
|
|
|
|
:form-post (http-post-form url auth body)
|
|
|
|
:post (http-post-json url auth body)
|
|
|
|
:put (http-put-json url auth body)
|
|
|
|
:delete (http-delete-json url auth body))
|
2014-08-26 14:11:22 -04:00
|
|
|
response-body (:body response)
|
|
|
|
location (get-in response [:headers "Location"])]
|
2014-08-01 15:07:28 -04:00
|
|
|
(if location
|
|
|
|
(if follow-location?
|
|
|
|
(-> (http-get-json location)
|
|
|
|
:body
|
|
|
|
(json/parse-string true))
|
2014-08-01 15:29:53 -04:00
|
|
|
(if (fhir-response? response)
|
2014-08-26 14:11:22 -04:00
|
|
|
(json/parse-string response-body true)
|
2014-08-01 15:29:53 -04:00
|
|
|
location))
|
|
|
|
(if (fhir-response? response)
|
2014-08-26 14:11:22 -04:00
|
|
|
(json/parse-string response-body true)
|
|
|
|
response-body)))
|
2014-07-14 10:19:02 -04:00
|
|
|
(catch ExceptionInfo ex
|
2014-08-01 15:29:53 -04:00
|
|
|
(let [{:keys [status body] :as response} (:object (ex-data ex))
|
|
|
|
fhir-resource-response? (fhir-response? response)]
|
2014-07-14 11:03:51 -04:00
|
|
|
(throw (ex-info (str "FHIR request failed: HTTP " status)
|
|
|
|
{:status status
|
|
|
|
:fhir-resource? fhir-resource-response?
|
|
|
|
:response
|
|
|
|
(if fhir-resource-response?
|
|
|
|
(json/parse-string body true)
|
|
|
|
body)})))))))
|
2014-07-04 09:04:45 -04:00
|
|
|
|
2014-07-04 10:54:19 -04:00
|
|
|
(defn- ->search-param-name [parameter & [modifier]]
|
2014-07-04 09:04:45 -04:00
|
|
|
(keyword
|
|
|
|
(str
|
2014-07-04 11:15:42 -04:00
|
|
|
(if (vector? parameter)
|
|
|
|
(->> parameter
|
|
|
|
(map name)
|
|
|
|
(str/join ".")
|
|
|
|
)
|
|
|
|
(name parameter))
|
2014-07-04 09:04:45 -04:00
|
|
|
(if modifier
|
|
|
|
(str ":" (name modifier))))))
|
|
|
|
|
2014-07-08 08:22:21 -04:00
|
|
|
(defn- ->search-param-descriptor [parameter value operator {:keys [modifier]}]
|
2014-07-04 10:54:19 -04:00
|
|
|
{:name (->search-param-name parameter modifier)
|
|
|
|
:operator operator
|
|
|
|
:value value})
|
|
|
|
|
2014-07-08 08:22:21 -04:00
|
|
|
(defmacro ^:private single-search-op [name operator]
|
2014-07-04 10:54:19 -04:00
|
|
|
`(defn ~name [parameter# value# & options#]
|
|
|
|
[(->search-param-descriptor parameter# value# ~operator (apply hash-map options#))]))
|
2014-07-04 09:04:45 -04:00
|
|
|
|
2014-07-08 08:22:21 -04:00
|
|
|
(defmacro ^:private double-search-op [name operator1 operator2]
|
2014-07-04 10:54:19 -04:00
|
|
|
`(defn ~name [parameter# value1# value2# & options#]
|
|
|
|
[(->search-param-descriptor parameter# value1# ~operator1 (apply hash-map options#))
|
|
|
|
(->search-param-descriptor parameter# value2# ~operator2 (apply hash-map options#))]))
|
2014-07-04 09:04:45 -04:00
|
|
|
|
2014-07-04 10:54:19 -04:00
|
|
|
(defn- escape-parameter [value]
|
|
|
|
(-> value
|
|
|
|
(.replace "\\" "\\\\")
|
|
|
|
(.replace "$" "\\$")
|
|
|
|
(.replace "," "\\,")
|
|
|
|
(.replace "|" "\\|")))
|
|
|
|
|
|
|
|
(defn- format-search-value [value]
|
|
|
|
(cond
|
|
|
|
(sequential? value)
|
|
|
|
(->> value
|
|
|
|
(map format-search-value)
|
|
|
|
(str/join ","))
|
|
|
|
|
|
|
|
(map? value)
|
|
|
|
(str (:namespace value) "|" (format-search-value (:value value)))
|
|
|
|
|
|
|
|
(instance? Date value)
|
2014-07-08 10:07:36 -04:00
|
|
|
(->timestamp value)
|
2014-07-04 10:54:19 -04:00
|
|
|
|
|
|
|
:else
|
|
|
|
(-> value str escape-parameter)))
|
|
|
|
|
2014-07-04 18:55:00 -04:00
|
|
|
(defn- search-params->query-map [params]
|
2014-07-04 10:54:19 -04:00
|
|
|
(->> params
|
|
|
|
(apply concat)
|
|
|
|
(map
|
|
|
|
(fn [{:keys [name operator value]}]
|
|
|
|
[name
|
|
|
|
(str
|
|
|
|
(if-not (= "=" operator) operator)
|
|
|
|
(format-search-value value))]))
|
2014-07-04 18:55:00 -04:00
|
|
|
(reduce
|
|
|
|
(fn [m [name value]]
|
|
|
|
(if (contains? m name)
|
|
|
|
(update-in m [name] #(conj (if (vector? %) % [%]) value))
|
|
|
|
(assoc m name value)))
|
|
|
|
{})))
|
2014-07-04 10:54:19 -04:00
|
|
|
|
2014-07-18 14:12:41 -04:00
|
|
|
(defn resource?
|
|
|
|
"returns true if the given argument is an EDN representation of a FHIR resource"
|
|
|
|
[x]
|
|
|
|
(and (map? x)
|
|
|
|
(string? (:resourceType x))
|
|
|
|
(not= "Bundle" (:resourceType x))))
|
|
|
|
|
|
|
|
(defn bundle?
|
|
|
|
"returns true if the given argument is an EDN representation of a FHIR bundle"
|
|
|
|
[x]
|
|
|
|
(and (map? x)
|
|
|
|
(= "Bundle" (:resourceType x))))
|
|
|
|
|
2014-07-18 15:50:52 -04:00
|
|
|
(defn validate-resource! [resource]
|
|
|
|
(if (and resource
|
|
|
|
(not (resource? resource)))
|
|
|
|
(throw (Exception. "Not a valid FHIR resource"))))
|
|
|
|
|
|
|
|
(defn validate-bundle! [bundle]
|
|
|
|
(if (and bundle
|
|
|
|
(not (bundle? bundle)))
|
|
|
|
(throw (Exception. "Not a valid FHIR bundle"))))
|
|
|
|
|
2014-07-08 08:22:21 -04:00
|
|
|
(single-search-op eq "=")
|
|
|
|
(single-search-op lt "<")
|
|
|
|
(single-search-op lte "<=")
|
|
|
|
(single-search-op gt ">")
|
|
|
|
(single-search-op gte ">=")
|
|
|
|
(double-search-op between ">" "<")
|
|
|
|
|
|
|
|
(defn namespaced
|
|
|
|
([value]
|
|
|
|
(namespaced nil value))
|
|
|
|
([namespace value]
|
|
|
|
{:namespace namespace
|
|
|
|
:value value}))
|
|
|
|
|
2014-08-06 09:44:36 -04:00
|
|
|
(defn- strip-query-params [url]
|
|
|
|
(let [pos (.indexOf url "?")]
|
|
|
|
(if-not (= -1 pos)
|
|
|
|
(subs url 0 pos)
|
|
|
|
url)))
|
|
|
|
|
|
|
|
(defn- strip-base-url [url server-url]
|
|
|
|
(if (.startsWith url server-url)
|
|
|
|
(let [stripped-url (subs url (count server-url))]
|
|
|
|
(if (= \/ (first stripped-url))
|
|
|
|
(subs stripped-url 1)
|
|
|
|
stripped-url))
|
|
|
|
url))
|
|
|
|
|
2014-09-02 13:01:09 -04:00
|
|
|
(defn- format-resource-url-type [url-path-parts keywordize?]
|
|
|
|
(if keywordize?
|
|
|
|
(-> url-path-parts first ->kebab-case keyword)
|
|
|
|
(-> url-path-parts first ->fhir-resource-name)))
|
|
|
|
|
|
|
|
(defn parse-relative-url
|
|
|
|
"parses a relative FHIR resource URL, returning a map containing each of the discrete
|
|
|
|
components of the URL (resource type, id, version number). if the optional
|
|
|
|
keywordize? arg is true, then returned resource type names will be turned into a
|
|
|
|
\"kebab case\" keyword (as opposed to a camelcase string which is the default). if
|
|
|
|
the URL cannot be parsed, returns nil"
|
|
|
|
[resource-url & [keywordize?]]
|
|
|
|
(let [parts (-> (strip-query-params resource-url)
|
|
|
|
(str/split #"/"))]
|
|
|
|
(cond
|
|
|
|
(= 2 (count parts))
|
|
|
|
{:type (format-resource-url-type parts keywordize?)
|
|
|
|
:id (second parts)}
|
|
|
|
|
|
|
|
(and (= 4 (count parts))
|
|
|
|
(= "_history" (nth parts 2)))
|
|
|
|
{:type (format-resource-url-type parts keywordize?)
|
|
|
|
:id (second parts)
|
|
|
|
:version (last parts)})))
|
|
|
|
|
|
|
|
(defn parse-absolute-url
|
|
|
|
"parses an absolute FHIR resource URL, returning a map containing each of the discrete
|
|
|
|
components of the URL (resource type, id, version number). if the optional
|
|
|
|
keywordize? arg is true, then returned resource type names will be turned into a
|
|
|
|
\"kebab case\" keyword (as opposed to a camelcase string which is the default). if
|
|
|
|
the URL cannot be parsed, returns nil."
|
|
|
|
[absolute-url & [keywordize?]]
|
|
|
|
(let [{:keys [path]} (url absolute-url)
|
|
|
|
parts (str/split path #"/")
|
|
|
|
has-version? (= "_history" (second (reverse parts)))]
|
|
|
|
(cond
|
|
|
|
(and (> (count parts) 4)
|
|
|
|
(= "_history" (second-last parts))
|
|
|
|
has-version?)
|
|
|
|
(let [versioned-url-parts (take-last 4 parts)]
|
|
|
|
{:type (format-resource-url-type versioned-url-parts keywordize?)
|
|
|
|
:id (second versioned-url-parts)
|
|
|
|
:version (last parts)})
|
|
|
|
|
|
|
|
(and (> (count parts) 2)
|
|
|
|
(not has-version?))
|
|
|
|
(let [no-version-url-parts (take-last 2 parts)]
|
|
|
|
{:type (format-resource-url-type no-version-url-parts keywordize?)
|
|
|
|
:id (second no-version-url-parts)})
|
|
|
|
)))
|
2014-08-06 09:44:36 -04:00
|
|
|
|
2014-09-02 13:09:41 -04:00
|
|
|
(defn absolute->relative-url
|
|
|
|
"turns an absolute FHIR resource URL into a relative one."
|
|
|
|
[absolute-url]
|
|
|
|
(if-let [{:keys [type id version]} (parse-absolute-url absolute-url)]
|
|
|
|
(if version
|
|
|
|
(str type "/" id "/_history/" version)
|
|
|
|
(str type "/" id))))
|
|
|
|
|
|
|
|
(defn relative->absolute-url
|
|
|
|
"combines a base URL to a FHIR server and a relative FHIR resource URL into an
|
|
|
|
absolute resource URL."
|
|
|
|
[base-url relative-url]
|
|
|
|
(if-not (or (str/blank? base-url)
|
|
|
|
(str/blank? relative-url))
|
|
|
|
(-> (join-paths base-url relative-url)
|
|
|
|
(url)
|
|
|
|
(.toString))))
|
|
|
|
|
2014-09-03 08:49:23 -04:00
|
|
|
(defn absolute-url?
|
|
|
|
"returns true if the passed URL is an absolute URL, false if not. if the value
|
|
|
|
passed in is not a string (or an empty string) an exception is thrown."
|
|
|
|
[^String resource-url]
|
|
|
|
(if (and (string? resource-url)
|
|
|
|
(not (str/blank? resource-url)))
|
|
|
|
(boolean
|
|
|
|
(try
|
|
|
|
(url resource-url)
|
|
|
|
(catch Exception ex)))
|
|
|
|
(throw (new Exception "Invalid URL or non-string value."))))
|
|
|
|
|
2014-07-04 12:34:04 -04:00
|
|
|
(defn collect-resources
|
2014-08-06 09:14:39 -04:00
|
|
|
"returns a sequence containing all of the resources contained in the given bundle.
|
|
|
|
deleted resources listed in the bundle will not be included in the returned
|
|
|
|
sequence (they have no :content)
|
2014-07-04 13:56:43 -04:00
|
|
|
|
|
|
|
reference:
|
|
|
|
bundles: http://hl7.org/implement/standards/fhir/extras.html#bundle"
|
2014-07-04 12:34:04 -04:00
|
|
|
[bundle]
|
2014-07-18 15:50:52 -04:00
|
|
|
(validate-bundle! bundle)
|
2014-07-04 12:34:04 -04:00
|
|
|
(->> bundle
|
|
|
|
:entry
|
2014-08-06 09:14:39 -04:00
|
|
|
(map :content)
|
|
|
|
(remove nil?)))
|
2014-07-04 12:34:04 -04:00
|
|
|
|
2014-09-02 15:20:52 -04:00
|
|
|
(defn get-bundle-next-page-url
|
|
|
|
"returns the 'next' bundle URL from the given FHIR bundle. useful for paged
|
|
|
|
search results. throws an exception if the value passed is not a valid FHIR
|
|
|
|
bundle."
|
|
|
|
[bundle]
|
2014-07-18 15:50:52 -04:00
|
|
|
(validate-bundle! bundle)
|
|
|
|
(->> (:link bundle)
|
|
|
|
(filter #(= "next" (:rel %)))
|
|
|
|
(first)
|
|
|
|
:href))
|
|
|
|
|
2014-09-02 15:20:52 -04:00
|
|
|
(defn get-base-url-from-bundle
|
|
|
|
"returns the base-url from the given FHIR bundle. throws an exception if the
|
|
|
|
value passed is not a valid FHIR bundle."
|
|
|
|
[bundle]
|
|
|
|
(validate-bundle!)
|
|
|
|
(->> (:link bundle)
|
|
|
|
(filter #(= "fhir-base" (:rel %)))
|
|
|
|
(first)
|
|
|
|
:href))
|
|
|
|
|
2014-07-04 13:40:27 -04:00
|
|
|
(defn fetch-next-page
|
|
|
|
"for resources that are returned over more then one page, this will fetch the
|
|
|
|
next page of resources as indicated by the link information contained in the
|
|
|
|
passed bundle. the return value is another bundle that can be passed again
|
|
|
|
to this function to get subsequent pages. if this function is passed the
|
2014-07-04 13:56:43 -04:00
|
|
|
bundle for the last page of resources, nil is returned
|
|
|
|
|
|
|
|
reference:
|
|
|
|
bundles: http://hl7.org/implement/standards/fhir/extras.html#bundle
|
|
|
|
paging: http://hl7.org/implement/standards/fhir/http.html#paging"
|
2014-07-04 13:40:27 -04:00
|
|
|
[bundle]
|
|
|
|
(if-let [next-url (get-bundle-next-page-url bundle)]
|
|
|
|
(http-get-json next-url)))
|
|
|
|
|
2014-07-08 11:08:10 -04:00
|
|
|
(defn- concat-bundle-entries [bundle other-bundle]
|
|
|
|
(if (nil? bundle)
|
|
|
|
other-bundle
|
|
|
|
(update-in
|
|
|
|
bundle [:entry]
|
|
|
|
(fn [existing-entries]
|
|
|
|
(->> (:entry other-bundle)
|
|
|
|
(concat existing-entries)
|
|
|
|
(vec))))))
|
|
|
|
|
|
|
|
(defn- strip-bundle-page-links [bundle]
|
|
|
|
(if bundle
|
|
|
|
(assoc bundle
|
|
|
|
:link
|
|
|
|
(->> (:link bundle)
|
|
|
|
(remove
|
|
|
|
(fn [{:keys [rel]}]
|
|
|
|
(or (= rel "first")
|
|
|
|
(= rel "last")
|
|
|
|
(= rel "next")
|
|
|
|
(= rel "previous"))))
|
|
|
|
(vec)))))
|
|
|
|
|
2014-07-04 13:40:27 -04:00
|
|
|
(defn fetch-all
|
|
|
|
"for resources that are returned over more then one page, this will automatically
|
2014-07-08 11:08:10 -04:00
|
|
|
fetch all pages of resources and them into a single bundle that contains all of
|
|
|
|
the resources.
|
2014-07-04 13:56:43 -04:00
|
|
|
|
|
|
|
reference:
|
|
|
|
bundles: http://hl7.org/implement/standards/fhir/extras.html#bundle
|
|
|
|
paging: http://hl7.org/implement/standards/fhir/http.html#paging"
|
2014-07-04 13:40:27 -04:00
|
|
|
[bundle]
|
2014-07-08 11:08:10 -04:00
|
|
|
(loop [current-page bundle
|
|
|
|
working-bundle nil]
|
|
|
|
(let [merged (concat-bundle-entries working-bundle current-page)
|
|
|
|
next-page (fetch-next-page current-page)]
|
2014-07-04 13:40:27 -04:00
|
|
|
(if next-page
|
2014-07-08 11:08:10 -04:00
|
|
|
(recur next-page merged)
|
|
|
|
(strip-bundle-page-links merged)))))
|
2014-07-04 13:40:27 -04:00
|
|
|
|
2014-07-04 09:04:45 -04:00
|
|
|
(defn get-resource
|
2014-07-08 13:52:54 -04:00
|
|
|
"gets a single resource from a FHIR server. the raw resource itself is returned (that is,
|
|
|
|
it is not contained in a bundle). if the resource could not be found, nil is returned.
|
2014-07-14 13:34:14 -04:00
|
|
|
for any other type of response (errors), an exception is thrown.
|
2014-07-08 13:52:54 -04:00
|
|
|
|
|
|
|
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.
|
2014-07-04 09:04:45 -04:00
|
|
|
|
|
|
|
reference:
|
|
|
|
read: http://hl7.org/implement/standards/fhir/http.html#read
|
2014-07-08 13:52:54 -04:00
|
|
|
vread: http://hl7.org/implement/standards/fhir/http.html#vread
|
|
|
|
relative url: http://hl7.org/implement/standards/fhir/references.html#atom-rel"
|
|
|
|
([base-url relative-resource-url]
|
|
|
|
(try
|
2014-07-08 14:24:55 -04:00
|
|
|
(fhir-request :get
|
2014-07-08 13:52:54 -04:00
|
|
|
base-url
|
|
|
|
relative-resource-url)
|
|
|
|
(catch ExceptionInfo ex
|
2014-07-14 11:03:51 -04:00
|
|
|
(let [http-status (:status (ex-data ex))]
|
2014-07-11 15:16:48 -04:00
|
|
|
; TODO: do we want to handle 410 differently? either way, the resource is not available
|
|
|
|
; though, a 410 could indicate to the caller that it might be available under a
|
|
|
|
; previous version ...
|
|
|
|
(if-not (or (= http-status 404)
|
|
|
|
(= http-status 410))
|
|
|
|
(throw ex))))))
|
2014-07-08 13:52:54 -04:00
|
|
|
([base-url type id & {:keys [version]}]
|
|
|
|
(let [resource-name (->fhir-resource-name type)
|
|
|
|
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
|
2014-07-14 13:34:14 -04:00
|
|
|
'fhir-base' link in the provided bundle. an exception is thrown if an error response is
|
|
|
|
received."
|
2014-07-08 13:52:54 -04:00
|
|
|
[bundle relative-url]
|
|
|
|
(if bundle
|
|
|
|
(let [base-url (->> (:link bundle)
|
|
|
|
(filter #(= "fhir-base" (:rel %)))
|
|
|
|
(first)
|
|
|
|
:href)]
|
|
|
|
(get-resource base-url relative-url))))
|
2014-07-04 09:04:45 -04:00
|
|
|
|
2014-07-04 09:34:37 -04:00
|
|
|
(defn get-resource-bundle
|
2014-07-08 11:16:39 -04:00
|
|
|
"gets a single resource from a FHIR server. the returned resource will be contained in a
|
|
|
|
bundle. if the resource could not be found, a bundle containing zero resources is returned.
|
2014-07-14 13:34:14 -04:00
|
|
|
an exception is thrown if an error response is received.
|
2014-07-04 13:56:43 -04:00
|
|
|
|
|
|
|
reference:
|
|
|
|
bundles: http://hl7.org/implement/standards/fhir/extras.html#bundle"
|
2014-07-04 09:34:37 -04:00
|
|
|
[base-url type id]
|
|
|
|
(let [resource-name (->fhir-resource-name type)
|
|
|
|
url-components ["/" resource-name]]
|
2014-07-08 14:24:55 -04:00
|
|
|
(fhir-request :get
|
2014-07-04 09:34:37 -04:00
|
|
|
base-url
|
|
|
|
(apply join-paths url-components)
|
2014-07-08 14:24:55 -04:00
|
|
|
:params {:_id id})))
|
2014-07-04 09:34:37 -04:00
|
|
|
|
2014-08-06 10:20:26 -04:00
|
|
|
(defn history
|
|
|
|
"returns a bundle containing the history of a single FHIR resource. note that this history can
|
|
|
|
include deletions as well, and these entries are not in a format parseable as a normal FHIR
|
|
|
|
resource. as a result, using a function like collect-resources on the returned bundle is not
|
|
|
|
generally recommended. if the resource could not be found, a bundle containing zero entries is
|
|
|
|
returned. an exception is thrown if an error response is received.
|
|
|
|
|
|
|
|
because some resources may have a large history, the bundle's contents may be paged. use the
|
|
|
|
helper functions fetch-next-page and fetch-all to work through all returned pages.
|
|
|
|
|
|
|
|
reference:
|
|
|
|
history: http://hl7.org/implement/standards/fhir/http.html#history"
|
|
|
|
[base-url type id & params]
|
|
|
|
(let [resource-name (->fhir-resource-name type)
|
|
|
|
url-components ["/" resource-name id "_history"]]
|
|
|
|
(fhir-request :get
|
|
|
|
base-url
|
|
|
|
(apply join-paths url-components)
|
|
|
|
:params (apply hash-map (if (and (seq? params)
|
|
|
|
(= 1 (count params)))
|
|
|
|
(first params)
|
|
|
|
params)))))
|
|
|
|
|
2014-07-04 09:04:45 -04:00
|
|
|
(defn search
|
|
|
|
"searches for resources on a FHIR server. multiple parameters are ANDed together. use of the search
|
|
|
|
operator helper functions is encouraged to ensure proper escaping/encoding of search parameters.
|
2014-07-04 13:56:43 -04:00
|
|
|
the results of this function can be passed to fetch-next-page or fetch-all to collect resources
|
2014-07-14 13:34:14 -04:00
|
|
|
returned in paged search results easier. an exception is thrown if an error response is received.
|
2014-07-04 09:04:45 -04:00
|
|
|
|
2014-08-26 14:11:22 -04:00
|
|
|
to overcome HTTP GET query size limitations that could be an issue for search operations with
|
|
|
|
a large number of parameters, all search requests are submitted as
|
|
|
|
application/x-www-form-urlencoded HTTP POST requests.
|
|
|
|
|
2014-07-04 09:04:45 -04:00
|
|
|
reference:
|
|
|
|
search: http://hl7.org/implement/standards/fhir/http.html#search"
|
2014-07-04 18:55:00 -04:00
|
|
|
[base-url type where & params]
|
2014-07-04 09:04:45 -04:00
|
|
|
(let [resource-name (->fhir-resource-name type)
|
2014-08-26 13:51:01 -04:00
|
|
|
url-components ["/" resource-name "/_search"]]
|
2014-08-26 14:11:22 -04:00
|
|
|
(fhir-request :form-post
|
2014-07-04 09:04:45 -04:00
|
|
|
base-url
|
|
|
|
(apply join-paths url-components)
|
2014-08-26 14:11:22 -04:00
|
|
|
:params-as-body? true
|
2014-07-08 14:24:55 -04:00
|
|
|
:params (merge
|
|
|
|
(search-params->query-map where)
|
|
|
|
(apply hash-map (if (and (seq? params)
|
|
|
|
(= 1 (count params)))
|
|
|
|
(first params)
|
|
|
|
params))))))
|
2014-07-08 11:33:13 -04:00
|
|
|
|
|
|
|
(defn search-and-fetch
|
|
|
|
"same as search, but automatically fetches all pages of resources returning a single bundle
|
2014-07-14 13:34:14 -04:00
|
|
|
that contains all search results. an exception is thrown if an error response is received."
|
2014-07-08 11:33:13 -04:00
|
|
|
[base-url type where & params]
|
|
|
|
(fetch-all
|
|
|
|
(search base-url type where params)))
|
2014-07-08 08:30:36 -04:00
|
|
|
|
2014-07-14 11:26:25 -04:00
|
|
|
(defn create
|
2014-08-01 15:12:04 -04:00
|
|
|
"creates a new resource. if the creation succeeded, then the new resource is returned,
|
|
|
|
unless the return-resource? argument is false, in which case the url to the new
|
|
|
|
resource is returned (which will contain the resource id). if a 'Location' header
|
|
|
|
was not set in the response, nil is returned on success. throws an exception if an
|
|
|
|
error was received.
|
2014-07-14 13:34:14 -04:00
|
|
|
|
|
|
|
reference:
|
|
|
|
create: http://hl7.org/implement/standards/fhir/http.html#create"
|
2014-08-01 15:08:11 -04:00
|
|
|
[base-url type resource & {:keys [return-resource?]}]
|
2014-07-18 14:12:41 -04:00
|
|
|
(if-not (resource? resource)
|
|
|
|
(throw (Exception. "Not a valid FHIR resource")))
|
2014-08-01 15:08:11 -04:00
|
|
|
(let [resource-name (->fhir-resource-name type)
|
|
|
|
uri-components ["/" resource-name]
|
|
|
|
return-resource? (if (nil? return-resource?) true return-resource?)]
|
2014-07-14 11:26:25 -04:00
|
|
|
(fhir-request :post
|
|
|
|
base-url
|
|
|
|
(apply join-paths uri-components)
|
2014-08-01 15:08:11 -04:00
|
|
|
:body resource
|
|
|
|
:follow-location? return-resource?)))
|
2014-07-14 11:26:25 -04:00
|
|
|
|
2014-07-14 15:22:17 -04:00
|
|
|
(defn update
|
2014-08-01 15:12:04 -04:00
|
|
|
"updates an existing resource. if the update succeeded, then the updated resource is
|
|
|
|
returned, unless the return-resource? argument is false, in which case the url to
|
|
|
|
the updated resource is returned (which will contain the resource id and version
|
|
|
|
number). if a 'Location' header was not set in the response, nil is returned on
|
|
|
|
success. throws an exception if an error response was received.
|
2014-07-18 13:34:47 -04:00
|
|
|
|
|
|
|
reference:
|
|
|
|
update: http://hl7.org/implement/standards/fhir/http.html#update"
|
2014-08-01 15:08:11 -04:00
|
|
|
[base-url type id resource & {:keys [version return-resource?]}]
|
2014-07-18 14:12:41 -04:00
|
|
|
(if-not (resource? resource)
|
|
|
|
(throw (Exception. "Not a valid FHIR resource")))
|
2014-08-01 15:08:11 -04:00
|
|
|
(let [resource-name (->fhir-resource-name type)
|
|
|
|
uri-components (if version
|
|
|
|
["/" resource-name id "_history" version]
|
|
|
|
["/" resource-name id])
|
|
|
|
return-resource? (if (nil? return-resource?) true return-resource?)]
|
2014-07-14 15:22:17 -04:00
|
|
|
(fhir-request :put
|
|
|
|
base-url
|
|
|
|
(apply join-paths uri-components)
|
2014-08-01 15:08:11 -04:00
|
|
|
:body resource
|
|
|
|
:follow-location? return-resource?)))
|
2014-07-14 15:22:17 -04:00
|
|
|
|
2014-08-01 15:06:43 -04:00
|
|
|
(defn delete
|
|
|
|
"deletes an existing resource. returns nil on success, throws an exception if an error
|
|
|
|
response was received.
|
|
|
|
|
|
|
|
reference:
|
|
|
|
delete: http://hl7.org/implement/standards/fhir/http.html#delete"
|
|
|
|
[base-url type id]
|
|
|
|
(let [resource-name (->fhir-resource-name type)
|
|
|
|
uri-components ["/" resource-name id]]
|
|
|
|
(fhir-request :delete
|
|
|
|
base-url
|
|
|
|
(apply join-paths uri-components))))
|
|
|
|
|
2014-08-05 15:28:29 -04:00
|
|
|
(defn deleted?
|
2014-08-06 08:57:52 -04:00
|
|
|
"checks if a resource has been deleted or not. this is based on FHIR servers returning
|
|
|
|
an HTTP 410 response when trying to retrieve a resource that has been deleted."
|
2014-08-05 15:28:29 -04:00
|
|
|
[base-url type id]
|
|
|
|
(let [resource-name (->fhir-resource-name type)
|
|
|
|
url-components ["/" resource-name id]
|
|
|
|
relative-url (apply join-paths url-components)]
|
|
|
|
(try
|
|
|
|
(fhir-request :get
|
|
|
|
base-url
|
|
|
|
relative-url)
|
|
|
|
; not deleted
|
|
|
|
false
|
|
|
|
(catch ExceptionInfo ex
|
|
|
|
(let [http-status (:status (ex-data ex))]
|
|
|
|
(cond
|
|
|
|
(= http-status 410) true
|
|
|
|
(= http-status 404) false
|
|
|
|
:else (throw ex)))))))
|
|
|
|
|
2014-08-29 15:34:42 -04:00
|
|
|
(defn transaction
|
|
|
|
"creates/updates/deletes resources specified in a bundle. if the entire transaction
|
|
|
|
succeeded, then a bundle is returned containing changed resources. some servers
|
|
|
|
may also return an additional OperationOutcome resource with additional information
|
|
|
|
about the transaction. throws an exception if an error response was received.
|
|
|
|
|
|
|
|
reference:
|
|
|
|
http://hl7.org/implement/standards/fhir/http.html#transaction"
|
|
|
|
[base-url bundle]
|
|
|
|
(if-not (bundle? bundle)
|
|
|
|
(throw (Exception. "Not a valid FHIR bundle")))
|
|
|
|
(fhir-request :post
|
|
|
|
base-url
|
|
|
|
"/"
|
|
|
|
:body bundle))
|
|
|
|
|
2014-07-08 08:30:36 -04:00
|
|
|
;(def server-url "http://fhir.healthintersections.com.au/open")
|
|
|
|
;(def server-url "http://spark.furore.com/fhir")
|
2014-07-14 13:37:06 -04:00
|
|
|
;(def server-url "http://fhirtest.uhn.ca/base")
|