allow passing of search params and other general FHIR params as separate arguments to 'search'. refactoring work with how params are converted to a URL query string
This commit is contained in:
parent
2900e42be7
commit
e27640e30a
|
@ -5,13 +5,13 @@
|
||||||
(:use [camel-snake-kebab]
|
(:use [camel-snake-kebab]
|
||||||
[clj-hl7-fhir.util]))
|
[clj-hl7-fhir.util]))
|
||||||
|
|
||||||
|
(def ^:private base-params {:_format "json"})
|
||||||
|
|
||||||
(defn- ->fhir-resource-name [x]
|
(defn- ->fhir-resource-name [x]
|
||||||
(name (->CamelCase x)))
|
(name (->CamelCase x)))
|
||||||
|
|
||||||
(defn- fhir-get-request [base-url resource-url & [params]]
|
(defn- fhir-get-request [base-url resource-url & [params]]
|
||||||
(let [query (cond
|
(let [query (map->query-string (merge base-params params))]
|
||||||
(sequential? params) (->> params (concat [:_format "json"]) (kv-vector->query))
|
|
||||||
:else (merge {:_format "json"} params))]
|
|
||||||
(http-get-json (build-url base-url resource-url query))))
|
(http-get-json (build-url base-url resource-url query))))
|
||||||
|
|
||||||
(defn- ->search-param-name [parameter & [modifier]]
|
(defn- ->search-param-name [parameter & [modifier]]
|
||||||
|
@ -77,7 +77,7 @@
|
||||||
:else
|
:else
|
||||||
(-> value str escape-parameter)))
|
(-> value str escape-parameter)))
|
||||||
|
|
||||||
(defn- search-params->query-kvs [params]
|
(defn- search-params->query-map [params]
|
||||||
(->> params
|
(->> params
|
||||||
(apply concat)
|
(apply concat)
|
||||||
(map
|
(map
|
||||||
|
@ -86,7 +86,12 @@
|
||||||
(str
|
(str
|
||||||
(if-not (= "=" operator) operator)
|
(if-not (= "=" operator) operator)
|
||||||
(format-search-value value))]))
|
(format-search-value value))]))
|
||||||
(apply concat)))
|
(reduce
|
||||||
|
(fn [m [name value]]
|
||||||
|
(if (contains? m name)
|
||||||
|
(update-in m [name] #(conj (if (vector? %) % [%]) value))
|
||||||
|
(assoc m name value)))
|
||||||
|
{})))
|
||||||
|
|
||||||
(defn- get-bundle-next-page-url [bundle]
|
(defn- get-bundle-next-page-url [bundle]
|
||||||
(->> (:link bundle)
|
(->> (:link bundle)
|
||||||
|
@ -175,11 +180,12 @@
|
||||||
|
|
||||||
reference:
|
reference:
|
||||||
search: http://hl7.org/implement/standards/fhir/http.html#search"
|
search: http://hl7.org/implement/standards/fhir/http.html#search"
|
||||||
[base-url type & params]
|
[base-url type where & params]
|
||||||
(let [resource-name (->fhir-resource-name type)
|
(let [resource-name (->fhir-resource-name type)
|
||||||
url-components ["/" resource-name]]
|
url-components ["/" resource-name]]
|
||||||
(fhir-get-request
|
(fhir-get-request
|
||||||
base-url
|
base-url
|
||||||
(apply join-paths url-components)
|
(apply join-paths url-components)
|
||||||
(search-params->query-kvs params))))
|
(merge
|
||||||
|
(search-params->query-map where)
|
||||||
|
(apply hash-map params)))))
|
||||||
|
|
|
@ -18,6 +18,22 @@
|
||||||
(.setTimeZone df tz)
|
(.setTimeZone df tz)
|
||||||
(.format df date))))
|
(.format df date))))
|
||||||
|
|
||||||
|
(defn map->query-string [m]
|
||||||
|
(->> m
|
||||||
|
(reduce
|
||||||
|
(fn [query-values [param-name value]]
|
||||||
|
(concat
|
||||||
|
query-values
|
||||||
|
(map
|
||||||
|
#(str (url-encode (name param-name))
|
||||||
|
"="
|
||||||
|
(url-encode (str %)))
|
||||||
|
(if (vector? value) value [value]))))
|
||||||
|
[])
|
||||||
|
(interpose "&")
|
||||||
|
(flatten)
|
||||||
|
(apply str)))
|
||||||
|
|
||||||
(defn kv-vector->query
|
(defn kv-vector->query
|
||||||
"should really be using cemerick.url/map->query in all cases except when you need 2 values under the
|
"should really be using cemerick.url/map->query in all cases except when you need 2 values under the
|
||||||
name name in the query string (such as with FHIR's date 'between' search support)"
|
name name in the query string (such as with FHIR's date 'between' search support)"
|
||||||
|
|
Reference in a new issue