From 04c8a563900f1e623d90af5c6ef1be261a557862 Mon Sep 17 00:00:00 2001 From: gered Date: Mon, 14 Jul 2014 10:19:02 -0400 Subject: [PATCH] update http post/put calls to sent a proper content-type. rework core fhir-request function to accept a request type and handle fhir error response parsing/returns --- src/clj_hl7_fhir/core.clj | 24 ++++++++++++++++++------ src/clj_hl7_fhir/util.clj | 18 +++++++++++++++--- 2 files changed, 33 insertions(+), 9 deletions(-) diff --git a/src/clj_hl7_fhir/core.clj b/src/clj_hl7_fhir/core.clj index ff3e265..3ed5548 100644 --- a/src/clj_hl7_fhir/core.clj +++ b/src/clj_hl7_fhir/core.clj @@ -1,21 +1,33 @@ (ns clj-hl7-fhir.core (:import (java.util Date) (clojure.lang ExceptionInfo)) - (:require [clojure.string :as str]) + (:require [clojure.string :as str] + [cheshire.core :as json]) (:use [camel-snake-kebab] [clj-hl7-fhir.util])) (defn- ->fhir-resource-name [x] (name (->CamelCase x))) +(defn- get-exception-fhir-body [ex] + (let [resp-content-type (get-in (ex-data ex) [:object :headers "Content-Type"]) + fhir-response? (.contains resp-content-type "application/json+fhir")] + (if fhir-response? + (json/parse-string (get-in (ex-data ex) [:object :body]) true)))) + (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)))) + (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)) + (catch ExceptionInfo ex + (if-let [fhir-response (get-exception-fhir-body ex)] + fhir-response + (throw ex)))))) (defn- ->search-param-name [parameter & [modifier]] (keyword diff --git a/src/clj_hl7_fhir/util.clj b/src/clj_hl7_fhir/util.clj index 2dbd21a..6ac9c3a 100644 --- a/src/clj_hl7_fhir/util.clj +++ b/src/clj_hl7_fhir/util.clj @@ -85,10 +85,22 @@ (http-request http/get url)) (defn http-post-json [url & [body]] - (http-request http/post url (if body {:body body}))) + (http-request + http/post url + (merge + {:content-type "application/json+fhir"} + (cond + (map? body) {:body (json/generate-string body)} + (string? body) {:body body})))) (defn http-put-json [url & [body]] - (http-request http/put url (if body {:body body}))) + (http-request + http/put url + (merge + {:content-type "application/json+fhir"} + (cond + (map? body) {:body (json/generate-string body)} + (string? body) {:body body})))) (defn http-delete-json [url & [body]] - (http-request http/delete url (if body {:body body}))) \ No newline at end of file + (http-request http/delete url (if body {:body body})))