From cfb949732522f6ae895239261267fe59759c0f80 Mon Sep 17 00:00:00 2001 From: Remco van 't Veer Date: Mon, 15 Jul 2024 15:12:37 +0200 Subject: [PATCH] Expose version information through API endpoints Adds: - /version.json - /versions.json - X-BAG-Version header to /lookup.json --- src/straatnaam/data.clj | 30 +++++++++++++++--------- src/straatnaam/web.clj | 43 +++++++++++++++++++++++++++-------- test/straatnaam/data_test.clj | 6 ++--- 3 files changed, 55 insertions(+), 24 deletions(-) diff --git a/src/straatnaam/data.clj b/src/straatnaam/data.clj index eee48ae..82efd18 100644 --- a/src/straatnaam/data.clj +++ b/src/straatnaam/data.clj @@ -50,14 +50,29 @@ SELECT * FROM " sn ".verblijfsobject_pand")]) (log/info "version activated:" sn)) +(defn versions + "Return currently available versions, sorted by BAG `updated` property." + [db] + (sql/query db "SELECT schema, updated, started_at, finished_at, ok + FROM bag_syncs + ORDER BY updated")) + +(defn current-version + "Return currently active version." + [db] + (-> db + (sql/query "SELECT schema, updated, started_at, finished_at + FROM bag_syncs + WHERE ok + ORDER BY finished_at DESC + LIMIT 1") + (first))) + (defn- current-sn "Determine scheme name of currently exposed version (which ever was imported successfully last)." [db] - (-> db - (sql/query "SELECT schema FROM bag_syncs WHERE ok ORDER BY finished_at DESC LIMIT 1") - first - :schema)) + (-> db (current-version) :schema)) (defn version-ok? "Returns true when version passes sanity checks." @@ -65,13 +80,6 @@ (and (sanity/count-stats-ok? db sn (current-sn db)) (sanity/common-addresses-available? db sn))) -(defn versions - "Return collection of activatable versions (schema names)." - [db] - (map :schema - (sql/query db "SELECT schema FROM bag_syncs - WHERE schema IS NOT NULL ORDER BY schema"))) - (defn- need-sync? [db current] (let [updated (-> db diff --git a/src/straatnaam/web.clj b/src/straatnaam/web.clj index dd350ae..0bfead1 100644 --- a/src/straatnaam/web.clj +++ b/src/straatnaam/web.clj @@ -23,7 +23,8 @@ [ring.middleware.defaults :refer [api-defaults wrap-defaults]] [ring.util.response :as response] [straatnaam.data :as data] - [straatnaam.http-status :as http-status])) + [straatnaam.http-status :as http-status]) + (:import java.text.SimpleDateFormat)) (defn- lookup-params [{:keys [postcode huisnummer]}] [(when (and postcode (re-matches #"\d{4}[A-Z]{2}" postcode)) @@ -31,19 +32,41 @@ (when (and huisnummer (re-matches #"\d+" huisnummer)) (Integer/parseInt huisnummer))]) +(defn- json-response [data] + (-> data + (json/write-str) + (response/response) + (response/content-type "application/json; charset=utf-8"))) + +(def date-formatter (doto (SimpleDateFormat. "YYYY-MM-dd") + (.setTimeZone (java.util.TimeZone/getTimeZone "Z")))) + +(defn- current-version-str [db] + (.format date-formatter + (-> db (data/current-version) :updated))) + (defroutes handler (GET "/" _ - (response/redirect "https://bestaatditadres.nl")) + (response/redirect "https://bestaatditadres.nl")) (GET "/lookup.json" {:keys [db params]} - (let [[postcode huisnummer] (lookup-params params)] - (if (and postcode huisnummer) - (-> db - (data/lookup postcode huisnummer) - (json/write-str) - (response/response) - (response/content-type "application/json; charset=utf-8")) - (response/status http-status/bad-request)))) + (let [[postcode huisnummer] (lookup-params params)] + (if (and postcode huisnummer) + (-> db + (data/lookup postcode huisnummer) + (json-response) + (response/header "X-BAG-Version" (current-version-str db))) + (response/status http-status/bad-request)))) + + (GET "/version.json" {:keys [db]} + (-> db + (data/current-version) + (json-response))) + + (GET "/versions.json" {:keys [db]} + (-> db + (data/versions) + (json-response))) (resources "/" {:root "public"}) (not-found "..")) diff --git a/test/straatnaam/data_test.clj b/test/straatnaam/data_test.clj index d36745a..a412595 100644 --- a/test/straatnaam/data_test.clj +++ b/test/straatnaam/data_test.clj @@ -75,7 +75,7 @@ (testing "first sync" (is (= ::data/success (data/sync test-db/*db* feed-url)) "sync successful") - (is (= [test-db/version] (data/versions test-db/*db*)) + (is (= [test-db/version] (->> test-db/*db* data/versions (map :schema))) "version loaded") (is (= "De Ruijterkade" (-> test-db/*db* (data/lookup "1011AB" 105) first :openbareruimte)) @@ -89,7 +89,7 @@ (is (data/sync test-db/*db* feed-url) "sync successful") (is (= [test-db/version "version_202112090000"] - (data/versions test-db/*db*)) + (->> test-db/*db* data/versions (map :schema))) "new version loaded")) (testing "a newer but bad version" @@ -101,7 +101,7 @@ (is (= ::data/unchanged (data/sync test-db/*db* feed-url)) "nothing changed") (is (= [test-db/version "version_202112090000" "version_202112100000"] - (data/versions test-db/*db*)) + (->> test-db/*db* data/versions (map :schema))) "but new version loaded") (is (= "De Ruijterkade" (-> test-db/*db* (data/lookup "1011AB" 105) first :openbareruimte)) -- 2.45.2