From aa32c20ba79c66bb493ed058bd94b1feb555453d Mon Sep 17 00:00:00 2001 From: gered Date: Tue, 8 Jul 2014 14:24:55 -0400 Subject: [PATCH] update http request functions, adding basic support for POST/PUT/DELETE --- src/clj_hl7_fhir/core.clj | 31 ++++++++++++++++++------------- src/clj_hl7_fhir/util.clj | 18 +++++++++++++++--- 2 files changed, 33 insertions(+), 16 deletions(-) diff --git a/src/clj_hl7_fhir/core.clj b/src/clj_hl7_fhir/core.clj index b265a8c..66c1faf 100644 --- a/src/clj_hl7_fhir/core.clj +++ b/src/clj_hl7_fhir/core.clj @@ -8,9 +8,14 @@ (defn- ->fhir-resource-name [x] (name (->CamelCase x))) -(defn- fhir-get-request [base-url resource-url & [params]] - (let [query (map->query-string params)] - (http-get-json (build-url base-url resource-url query)))) +(defn- fhir-request [type base-url resource-url & {:keys [params body]}] + (let [query (map->query-string params) + url (build-url base-url resource-url query)] + (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)))) (defn- ->search-param-name [parameter & [modifier]] (keyword @@ -174,7 +179,7 @@ relative url: http://hl7.org/implement/standards/fhir/references.html#atom-rel" ([base-url relative-resource-url] (try - (fhir-get-request + (fhir-request :get base-url relative-resource-url) (catch ExceptionInfo ex @@ -207,10 +212,10 @@ [base-url type id] (let [resource-name (->fhir-resource-name type) url-components ["/" resource-name]] - (fhir-get-request + (fhir-request :get base-url (apply join-paths url-components) - {:_id id}))) + :params {:_id id}))) (defn search "searches for resources on a FHIR server. multiple parameters are ANDed together. use of the search @@ -223,15 +228,15 @@ [base-url type where & params] (let [resource-name (->fhir-resource-name type) url-components ["/" resource-name]] - (fhir-get-request + (fhir-request :get base-url (apply join-paths url-components) - (merge - (search-params->query-map where) - (apply hash-map (if (and (seq? params) - (= 1 (count params))) - (first params) - params)))))) + :params (merge + (search-params->query-map where) + (apply hash-map (if (and (seq? params) + (= 1 (count params))) + (first params) + params)))))) (defn search-and-fetch "same as search, but automatically fetches all pages of resources returning a single bundle diff --git a/src/clj_hl7_fhir/util.clj b/src/clj_hl7_fhir/util.clj index 5f3029d..2dbd21a 100644 --- a/src/clj_hl7_fhir/util.clj +++ b/src/clj_hl7_fhir/util.clj @@ -76,7 +76,19 @@ (assoc :query params) (str))) -(defn http-get-json [url] - (-> (http/get url {:accept "application/json+fhir"}) +(defn- http-request [f url & [params]] + (-> (f url (merge {:accept "application/json+fhir"} params)) :body - (json/parse-string true))) \ No newline at end of file + (json/parse-string true))) + +(defn http-get-json [url] + (http-request http/get url)) + +(defn http-post-json [url & [body]] + (http-request http/post url (if body {:body body}))) + +(defn http-put-json [url & [body]] + (http-request http/put url (if body {:body body}))) + +(defn http-delete-json [url & [body]] + (http-request http/delete url (if body {:body body}))) \ No newline at end of file