From 1d2498c62ea17a6edf72bf99792eea8d5bc1cc49 Mon Sep 17 00:00:00 2001 From: gered Date: Fri, 4 Jul 2014 13:40:27 -0400 Subject: [PATCH] add support for navigating/fetching across paged search results --- src/clj_hl7_fhir/core.clj | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/clj_hl7_fhir/core.clj b/src/clj_hl7_fhir/core.clj index 9d07121..dd52085 100644 --- a/src/clj_hl7_fhir/core.clj +++ b/src/clj_hl7_fhir/core.clj @@ -88,6 +88,12 @@ (format-search-value value))])) (apply concat))) +(defn- get-bundle-next-page-url [bundle] + (->> (:link bundle) + (filter #(= "next" (:rel %))) + (first) + :href)) + (defn collect-resources "returns a sequence containing all of the resources contained in the given bundle" [bundle] @@ -95,6 +101,29 @@ :entry (map :content))) +(defn fetch-next-page + "for resources that are returned over more then one page, this will fetch the + next page of resources as indicated by the link information contained in the + passed bundle. the return value is another bundle that can be passed again + to this function to get subsequent pages. if this function is passed the + bundle for the last page of resources, nil is returned" + [bundle] + (if-let [next-url (get-bundle-next-page-url bundle)] + (http-get-json next-url))) + +(defn fetch-all + "for resources that are returned over more then one page, this will automatically + fetch all pages of resources and return a final sequence containing all of them + in order" + [bundle] + (loop [current-page bundle + fetched []] + (let [latest-fetched (concat fetched (collect-resources current-page)) + next-page (fetch-next-page current-page)] + (if next-page + (recur next-page latest-fetched) + latest-fetched)))) + (defn get-resource "gets a single resource from a FHIR server. can optionally get a specific version of a resource.