update fetch-all. now returns a single bundle with all the resources

This commit is contained in:
Gered 2014-07-08 11:08:10 -04:00
parent 87ea877b26
commit a394eda3a3

View file

@ -121,22 +121,45 @@
(if-let [next-url (get-bundle-next-page-url bundle)] (if-let [next-url (get-bundle-next-page-url bundle)]
(http-get-json next-url))) (http-get-json next-url)))
(defn- concat-bundle-entries [bundle other-bundle]
(if (nil? bundle)
other-bundle
(update-in
bundle [:entry]
(fn [existing-entries]
(->> (:entry other-bundle)
(concat existing-entries)
(vec))))))
(defn- strip-bundle-page-links [bundle]
(if bundle
(assoc bundle
:link
(->> (:link bundle)
(remove
(fn [{:keys [rel]}]
(or (= rel "first")
(= rel "last")
(= rel "next")
(= rel "previous"))))
(vec)))))
(defn fetch-all (defn fetch-all
"for resources that are returned over more then one page, this will automatically "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 fetch all pages of resources and them into a single bundle that contains all of
in order the resources.
reference: reference:
bundles: http://hl7.org/implement/standards/fhir/extras.html#bundle bundles: http://hl7.org/implement/standards/fhir/extras.html#bundle
paging: http://hl7.org/implement/standards/fhir/http.html#paging" paging: http://hl7.org/implement/standards/fhir/http.html#paging"
[bundle] [bundle]
(loop [current-page bundle (loop [current-page bundle
fetched []] working-bundle nil]
(let [latest-fetched (concat fetched (collect-resources current-page)) (let [merged (concat-bundle-entries working-bundle current-page)
next-page (fetch-next-page current-page)] next-page (fetch-next-page current-page)]
(if next-page (if next-page
(recur next-page latest-fetched) (recur next-page merged)
latest-fetched)))) (strip-bundle-page-links merged)))))
(defn get-resource (defn get-resource
"gets a single resource from a FHIR server. can optionally get a specific version of a resource. "gets a single resource from a FHIR server. can optionally get a specific version of a resource.