Making data reconciliation better
This commit is contained in:
@@ -13,6 +13,19 @@
|
||||
[unilog.context :as lc]
|
||||
[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 []
|
||||
(->> (client/get "https://connect.squareup.com/v2/locations"
|
||||
{:headers {"Square-Version" "2020-08-12"
|
||||
@@ -27,9 +40,7 @@
|
||||
(try
|
||||
(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)
|
||||
{:headers {"Square-Version" "2020-08-12"
|
||||
"Authorization" "Bearer EAAAEO2xSqesDutZz71hz3eulKmrlKTiEqG3uZ4j25x5GYlOluQ2cj2JxNUXqXD7"
|
||||
"Content-Type" "application/json"}
|
||||
{:headers base-headers
|
||||
:query-params {"include_related_items" "true"}
|
||||
:as :json})
|
||||
:body
|
||||
@@ -225,27 +236,32 @@
|
||||
(retry f (inc i))))
|
||||
(log/error "Too many failures"))))
|
||||
|
||||
(defn settlements [l]
|
||||
(log/info "Searching for" l)
|
||||
(let [settlements (->> (client/get (str "https://connect.squareup.com/v1/" l "/settlements")
|
||||
{:headers {"Authorization" "Bearer EAAAEO2xSqesDutZz71hz3eulKmrlKTiEqG3uZ4j25x5GYlOluQ2cj2JxNUXqXD7"
|
||||
"Content-Type" "application/json"}
|
||||
:as :json})
|
||||
:body
|
||||
(map :id))]
|
||||
(loop [[s & xs] (take 5 settlements)
|
||||
result []]
|
||||
(log/info "Looking up settlement " s " for location " l)
|
||||
(let [n (:body (retry #(client/get (str "https://connect.squareup.com/v1/" l "/settlements/" s)
|
||||
{:headers {"Authorization" "Bearer EAAAEO2xSqesDutZz71hz3eulKmrlKTiEqG3uZ4j25x5GYlOluQ2cj2JxNUXqXD7"
|
||||
"Content-Type" "application/json"}
|
||||
:as :json
|
||||
:retry-handler (fn [ex try-count http-context]
|
||||
(log/warn "Retrying after failure " ex)
|
||||
(if (> try-count 4) false true))})))]
|
||||
(if (seq xs)
|
||||
(recur xs (conj result n))
|
||||
(conj result n))))))
|
||||
(defn settlements
|
||||
([l] (settlements l (lookup-dates)))
|
||||
([l lookup-dates]
|
||||
(log/info "Searching for" l)
|
||||
(->> lookup-dates
|
||||
(mapcat (fn [[start-date end-date]]
|
||||
(log/info "looking up settlements for " l " on dates " start-date " to " end-date)
|
||||
(let [settlements (->> (client/get (str "https://connect.squareup.com/v1/" l "/settlements")
|
||||
{:headers base-headers
|
||||
:query-params {"begin_time" start-date
|
||||
"end_time" end-date}
|
||||
:as :json})
|
||||
:body
|
||||
(map :id))]
|
||||
settlements)))
|
||||
set
|
||||
seq
|
||||
(map (fn [s]
|
||||
(log/info "Looking up settlement " s " for location " l)
|
||||
(let [n (:body (retry #(client/get (str "https://connect.squareup.com/v1/" l "/settlements/" s)
|
||||
{: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 []
|
||||
(->> (locations)
|
||||
|
||||
Reference in New Issue
Block a user