Making data reconciliation better

This commit is contained in:
Bryce Covert
2021-08-24 09:51:23 -07:00
parent 86f37790ab
commit eb00dc0af6

View File

@@ -13,6 +13,19 @@
[unilog.context :as lc] [unilog.context :as lc]
[yang.scheduler :as scheduler])) [yang.scheduler :as scheduler]))
(def base-headers {"Square-Version" "2020-08-12"
"Authorization" "Bearer EAAAEO2xSqesDutZz71hz3eulKmrlKTiEqG3uZ4j25x5GYlOluQ2cj2JxNUXqXD7"
"Content-Type" "application/json"})
(defn lookup-dates []
(->> (clj-time.periodic/periodic-seq (time/plus (time/now) (time/days -15))
(time/now)
(time/days 5))
(map (fn [d]
[(auto-ap.time/unparse (time/plus d (time/days 1)) auto-ap.time/iso-date)
(auto-ap.time/unparse (time/plus d (time/days 5)) auto-ap.time/iso-date)]))))
(defn locations [] (defn locations []
(->> (client/get "https://connect.squareup.com/v2/locations" (->> (client/get "https://connect.squareup.com/v2/locations"
{:headers {"Square-Version" "2020-08-12" {:headers {"Square-Version" "2020-08-12"
@@ -27,9 +40,7 @@
(try (try
(log/info "looking up catalog for" (str "https://connect.squareup.com/v2/catalog/object/" i)) (log/info "looking up catalog for" (str "https://connect.squareup.com/v2/catalog/object/" i))
(->> (client/get (str "https://connect.squareup.com/v2/catalog/object/" i) (->> (client/get (str "https://connect.squareup.com/v2/catalog/object/" i)
{:headers {"Square-Version" "2020-08-12" {:headers base-headers
"Authorization" "Bearer EAAAEO2xSqesDutZz71hz3eulKmrlKTiEqG3uZ4j25x5GYlOluQ2cj2JxNUXqXD7"
"Content-Type" "application/json"}
:query-params {"include_related_items" "true"} :query-params {"include_related_items" "true"}
:as :json}) :as :json})
:body :body
@@ -225,27 +236,32 @@
(retry f (inc i)))) (retry f (inc i))))
(log/error "Too many failures")))) (log/error "Too many failures"))))
(defn settlements [l] (defn settlements
(log/info "Searching for" l) ([l] (settlements l (lookup-dates)))
(let [settlements (->> (client/get (str "https://connect.squareup.com/v1/" l "/settlements") ([l lookup-dates]
{:headers {"Authorization" "Bearer EAAAEO2xSqesDutZz71hz3eulKmrlKTiEqG3uZ4j25x5GYlOluQ2cj2JxNUXqXD7" (log/info "Searching for" l)
"Content-Type" "application/json"} (->> lookup-dates
:as :json}) (mapcat (fn [[start-date end-date]]
:body (log/info "looking up settlements for " l " on dates " start-date " to " end-date)
(map :id))] (let [settlements (->> (client/get (str "https://connect.squareup.com/v1/" l "/settlements")
(loop [[s & xs] (take 5 settlements) {:headers base-headers
result []] :query-params {"begin_time" start-date
(log/info "Looking up settlement " s " for location " l) "end_time" end-date}
(let [n (:body (retry #(client/get (str "https://connect.squareup.com/v1/" l "/settlements/" s) :as :json})
{:headers {"Authorization" "Bearer EAAAEO2xSqesDutZz71hz3eulKmrlKTiEqG3uZ4j25x5GYlOluQ2cj2JxNUXqXD7" :body
"Content-Type" "application/json"} (map :id))]
:as :json settlements)))
:retry-handler (fn [ex try-count http-context] set
(log/warn "Retrying after failure " ex) seq
(if (> try-count 4) false true))})))] (map (fn [s]
(if (seq xs) (log/info "Looking up settlement " s " for location " l)
(recur xs (conj result n)) (let [n (:body (retry #(client/get (str "https://connect.squareup.com/v1/" l "/settlements/" s)
(conj result n)))))) {:headers base-headers
:as :json
:retry-handler (fn [ex try-count http-context]
(log/warn "Retrying after failure " ex)
(if (> try-count 4) false true))})))]
n))))))
(defn daily-settlements [] (defn daily-settlements []
(->> (locations) (->> (locations)