Skip to content

Instantly share code, notes, and snippets.

@mdemare
Created November 1, 2022 16:19
Show Gist options
  • Save mdemare/cd55f1f80a022171bf7a3a301d79703d to your computer and use it in GitHub Desktop.
Save mdemare/cd55f1f80a022171bf7a3a301d79703d to your computer and use it in GitHub Desktop.
Normal function called by wrapping function
(defn load-entities
[f loader {:keys [::ooapi/type] :as request}]
(if (= "relation" type)
(f request)
(when-result [entity (loader request)
offerings (load-offerings loader request)
education-specification (if (= type "education-specification")
entity
(loader (assoc request
::ooapi/type "education-specification"
::ooapi/id (ooapi/education-specification-id entity))))]
(f (assoc request
::ooapi/entity (assoc entity :offerings offerings)
::ooapi/education-specification education-specification)))))
(defn wrap-load-entities
[f ooapi-loader]
(let [loader (validating-loader ooapi-loader)]
(fn [request]
(load-entities f loader request))))
(let [ooapi-loader (make-ooapi-http-loader gateway-root-url gateway-credentials)
entity-loader (wrap-load-entities rio-config ooapi-loader)]
..)
@joodie
Copy link

joodie commented Nov 3, 2022

(defn load-entities
  [{:keys [::ooapi/type] :as request} loader]
  (when-result [entity                  (loader request)
                offerings               (load-offerings loader request)
                education-specification (if (= type "education-specification")
                                          entity
                                          (loader (assoc request
                                                         ::ooapi/type "education-specification"
                                                         ::ooapi/id (ooapi/education-specification-id entity))))]
    (assoc request
           ::ooapi/entity (assoc entity :offerings offerings)
           ::ooapi/education-specification education-specification)))

(defn wrap-load-entities
  [f ooapi-loader]
  (let [loader (validating-loader ooapi-loader)]
    (fn [{:keys [::ooapi/type] :as request}]
      (if (= "relation" type)
        (f request)
        ;; ensure we don't call f when load-entities returns errors
        (result-> request 
                  (load-entities loader)
                  (f))))))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment