Skip to content

Instantly share code, notes, and snippets.

@kennyjwilli
Created January 15, 2022 00:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kennyjwilli/65dfbc30c75d76ba2e22cb044c3f8d1f to your computer and use it in GitHub Desktop.
Save kennyjwilli/65dfbc30c75d76ba2e22cb044c3f8d1f to your computer and use it in GitHub Desktop.
(defn iteration-op-map
"Iterates an op-map based step. Useful in aws-api and gcp-api. Returns a list
of results from each page after applying :itemsf. If an anomaly is returned
from invoke-fn, it will be included in the list."
[invoke-fn op-map {:keys [request-key response-key itemsf]}]
(iteration
(fn [next-token]
(invoke-fn
(cond-> op-map
next-token (assoc-in [:request request-key] next-token))))
:kf (fn [ret]
(let [k (response-key ret)]
(when (and k (if (string? k) (not (str/blank? k)) true))
k)))
:vf (fn [ret]
(if (:cognitect.anomalies/category ret)
ret
(itemsf ret)))))
(comment
(iteration-op-map
#(aws/invoke client %)
{:op :ListObjectsV2
:request {:Bucket "my-bucket"}}
{:request-key :ContinuationToken
:response-key :ContinuationToken
:itemsf :Contents}))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment