use POST requests for all searches due to HTTP GET query size limits
This commit is contained in:
parent
16b52e6316
commit
d530f429af
|
@ -13,29 +13,31 @@
|
||||||
(and (map? response)
|
(and (map? response)
|
||||||
(.contains (get-in response [:headers "Content-Type"]) "application/json+fhir")))
|
(.contains (get-in response [:headers "Content-Type"]) "application/json+fhir")))
|
||||||
|
|
||||||
(defn- fhir-request [type base-url resource-url & {:keys [params body follow-location?]}]
|
(defn- fhir-request [type base-url resource-url & {:keys [params body params-as-body? follow-location?]}]
|
||||||
(let [query (map->query-string params)
|
(let [query (map->query-string params)
|
||||||
url (build-url base-url resource-url query)
|
url (build-url base-url resource-url (if-not params-as-body? query))
|
||||||
|
body (if params-as-body? query body)
|
||||||
follow-location? (if (nil? follow-location?) true follow-location?)]
|
follow-location? (if (nil? follow-location?) true follow-location?)]
|
||||||
(try
|
(try
|
||||||
(let [response (case type
|
(let [response (case type
|
||||||
:get (http-get-json url)
|
:get (http-get-json url)
|
||||||
:post (http-post-json url body)
|
:form-post (http-post-form url body)
|
||||||
:put (http-put-json url body)
|
:post (http-post-json url body)
|
||||||
:delete (http-delete-json url body))
|
:put (http-put-json url body)
|
||||||
body (:body response)
|
:delete (http-delete-json url body))
|
||||||
location (get-in response [:headers "Location"])]
|
response-body (:body response)
|
||||||
|
location (get-in response [:headers "Location"])]
|
||||||
(if location
|
(if location
|
||||||
(if follow-location?
|
(if follow-location?
|
||||||
(-> (http-get-json location)
|
(-> (http-get-json location)
|
||||||
:body
|
:body
|
||||||
(json/parse-string true))
|
(json/parse-string true))
|
||||||
(if (fhir-response? response)
|
(if (fhir-response? response)
|
||||||
(json/parse-string body true)
|
(json/parse-string response-body true)
|
||||||
location))
|
location))
|
||||||
(if (fhir-response? response)
|
(if (fhir-response? response)
|
||||||
(json/parse-string body true)
|
(json/parse-string response-body true)
|
||||||
body)))
|
response-body)))
|
||||||
(catch ExceptionInfo ex
|
(catch ExceptionInfo ex
|
||||||
(let [{:keys [status body] :as response} (:object (ex-data ex))
|
(let [{:keys [status body] :as response} (:object (ex-data ex))
|
||||||
fhir-resource-response? (fhir-response? response)]
|
fhir-resource-response? (fhir-response? response)]
|
||||||
|
@ -355,14 +357,19 @@
|
||||||
the results of this function can be passed to fetch-next-page or fetch-all to collect resources
|
the results of this function can be passed to fetch-next-page or fetch-all to collect resources
|
||||||
returned in paged search results easier. an exception is thrown if an error response is received.
|
returned in paged search results easier. an exception is thrown if an error response is received.
|
||||||
|
|
||||||
|
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.
|
||||||
|
|
||||||
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 where & params]
|
[base-url type where & params]
|
||||||
(let [resource-name (->fhir-resource-name type)
|
(let [resource-name (->fhir-resource-name type)
|
||||||
url-components ["/" resource-name "/_search"]]
|
url-components ["/" resource-name "/_search"]]
|
||||||
(fhir-request :get
|
(fhir-request :form-post
|
||||||
base-url
|
base-url
|
||||||
(apply join-paths url-components)
|
(apply join-paths url-components)
|
||||||
|
:params-as-body? true
|
||||||
:params (merge
|
:params (merge
|
||||||
(search-params->query-map where)
|
(search-params->query-map where)
|
||||||
(apply hash-map (if (and (seq? params)
|
(apply hash-map (if (and (seq? params)
|
||||||
|
@ -457,3 +464,7 @@
|
||||||
;(def server-url "http://fhir.healthintersections.com.au/open")
|
;(def server-url "http://fhir.healthintersections.com.au/open")
|
||||||
;(def server-url "http://spark.furore.com/fhir")
|
;(def server-url "http://spark.furore.com/fhir")
|
||||||
;(def server-url "http://fhirtest.uhn.ca/base")
|
;(def server-url "http://fhirtest.uhn.ca/base")
|
||||||
|
|
||||||
|
|
||||||
|
(clojure.pprint/pprint
|
||||||
|
(get-resource server-url :patient 38))
|
|
@ -78,6 +78,15 @@
|
||||||
(map? body) {:body (json/generate-string body)}
|
(map? body) {:body (json/generate-string body)}
|
||||||
(string? body) {:body body}))))
|
(string? body) {:body body}))))
|
||||||
|
|
||||||
|
(defn http-post-form [url & [body]]
|
||||||
|
(http-request
|
||||||
|
http/post url
|
||||||
|
(merge
|
||||||
|
{:content-type "application/x-www-form-urlencoded"}
|
||||||
|
(cond
|
||||||
|
(map? body) {:body (json/generate-string body)}
|
||||||
|
(string? body) {:body body}))))
|
||||||
|
|
||||||
(defn http-put-json [url & [body]]
|
(defn http-put-json [url & [body]]
|
||||||
(http-request
|
(http-request
|
||||||
http/put url
|
http/put url
|
||||||
|
|
Reference in a new issue