From 41e5b2e46a3d1216865ec763865863f64e0c0ac4 Mon Sep 17 00:00:00 2001 From: gered Date: Mon, 14 Jul 2014 11:26:25 -0400 Subject: [PATCH] add create resource support. re-do request handling a bit to handle http 201 returns which include a "location" header --- src/clj_hl7_fhir/core.clj | 24 +++++++++++++++++++----- src/clj_hl7_fhir/util.clj | 4 +--- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/src/clj_hl7_fhir/core.clj b/src/clj_hl7_fhir/core.clj index ce3ff56..56be45d 100644 --- a/src/clj_hl7_fhir/core.clj +++ b/src/clj_hl7_fhir/core.clj @@ -13,11 +13,16 @@ (let [query (map->query-string params) url (build-url base-url resource-url query)] (try - (case type - :get (http-get-json url) - :post (http-post-json url body) - :put (http-put-json url body) - :delete (http-delete-json url body)) + (let [response (case type + :get (http-get-json url) + :post (http-post-json url body) + :put (http-put-json url body) + :delete (http-delete-json url body))] + (-> (if (= 201 (:status response)) + (http-get-json (get-in response [:headers "Location"])) + response) + :body + (json/parse-string true))) (catch ExceptionInfo ex (let [{:keys [status body headers]} (:object (ex-data ex)) fhir-resource-response? (.contains (get headers "Content-Type") "application/json+fhir")] @@ -262,6 +267,15 @@ (fetch-all (search base-url type where params))) +(defn create + [base-url type resource] + (let [resource-name (->fhir-resource-name type) + uri-components ["/" resource-name]] + (fhir-request :post + base-url + (apply join-paths uri-components) + :body resource))) + ;(def server-url "http://fhir.healthintersections.com.au/open") ;(def server-url "http://spark.furore.com/fhir") (def server-url "http://uhnvesb01d.uhn.on.ca:25180/hapi-fhir-jpaserver/base") diff --git a/src/clj_hl7_fhir/util.clj b/src/clj_hl7_fhir/util.clj index 6ac9c3a..2943fcf 100644 --- a/src/clj_hl7_fhir/util.clj +++ b/src/clj_hl7_fhir/util.clj @@ -77,9 +77,7 @@ (str))) (defn- http-request [f url & [params]] - (-> (f url (merge {:accept "application/json+fhir"} params)) - :body - (json/parse-string true))) + (f url (merge {:accept "application/json+fhir"} params))) (defn http-get-json [url] (http-request http/get url))