From 28f2cef6dd3827b65d769e04432dd059e8f27e59 Mon Sep 17 00:00:00 2001 From: gered Date: Tue, 5 Aug 2014 15:28:29 -0400 Subject: [PATCH] add helper function for checking if a resource has been deleted or not --- README.md | 13 +++++++++++++ src/clj_hl7_fhir/core.clj | 18 ++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/README.md b/README.md index 89beb70..19aaf91 100644 --- a/README.md +++ b/README.md @@ -426,6 +426,11 @@ resource to be deleted. The return value will typically be `nil` on success, tho some servers may return an OperationOutcome resource that includes more details about the successful deletion. +You can use the helper function `deleted?` to determine if a resource has been deleted +or not, since `get-resource` returns nil for both deleted resources and resources +which do not exist at all (an important distinction, as in FHIR a deleted resource +technically still exists under previous version numbers). + ##### Examples ```clojure @@ -440,6 +445,14 @@ the successful deletion. ; try to delete a non-existant patient (delete server-url :patient 9001) => ExceptionInfo FHIR request failed: HTTP 404 + +; testing if a resource has been deleted or not +(deleted? server-url :patient 1654) +=> true + +; testing if a non-existant resource has been deleted +(deleted? server-url :patient 9001) +=> false ``` ### Error Handling diff --git a/src/clj_hl7_fhir/core.clj b/src/clj_hl7_fhir/core.clj index 2b5caf8..be0a98a 100644 --- a/src/clj_hl7_fhir/core.clj +++ b/src/clj_hl7_fhir/core.clj @@ -365,6 +365,24 @@ base-url (apply join-paths uri-components)))) +(defn deleted? + [base-url type id] + (let [resource-name (->fhir-resource-name type) + url-components ["/" resource-name id] + relative-url (apply join-paths url-components)] + (try + (fhir-request :get + base-url + relative-url) + ; not deleted + false + (catch ExceptionInfo ex + (let [http-status (:status (ex-data ex))] + (cond + (= http-status 410) true + (= http-status 404) false + :else (throw ex))))))) + ;(def server-url "http://fhir.healthintersections.com.au/open") ;(def server-url "http://spark.furore.com/fhir") ;(def server-url "http://fhirtest.uhn.ca/base")