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
(: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)]
(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))))
: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

View file

@ -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})))