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

This commit is contained in:
Gered 2014-07-14 10:19:02 -04:00
parent cb4355768a
commit 04c8a56390
2 changed files with 33 additions and 9 deletions

View file

@ -1,21 +1,33 @@
(ns clj-hl7-fhir.core (ns clj-hl7-fhir.core
(:import (java.util Date) (:import (java.util Date)
(clojure.lang ExceptionInfo)) (clojure.lang ExceptionInfo))
(:require [clojure.string :as str]) (:require [clojure.string :as str]
[cheshire.core :as json])
(:use [camel-snake-kebab] (:use [camel-snake-kebab]
[clj-hl7-fhir.util])) [clj-hl7-fhir.util]))
(defn- ->fhir-resource-name [x] (defn- ->fhir-resource-name [x]
(name (->CamelCase 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]}] (defn- fhir-request [type base-url resource-url & {:keys [params body]}]
(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 query)]
(case type (try
:get (http-get-json url) (case type
:post (http-post-json url body) :get (http-get-json url)
:put (http-put-json url body) :post (http-post-json url body)
:delete (http-delete-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]] (defn- ->search-param-name [parameter & [modifier]]
(keyword (keyword

View file

@ -85,10 +85,22 @@
(http-request http/get url)) (http-request http/get url))
(defn http-post-json [url & [body]] (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]] (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]] (defn http-delete-json [url & [body]]
(http-request http/delete url (if body {:body body}))) (http-request http/delete url (if body {:body body})))