From 0b4a82591778ddd5b01c2d47e4af97d96d2c5585 Mon Sep 17 00:00:00 2001 From: Michael Crenshaw Date: Tue, 15 Sep 2020 18:14:37 +0000 Subject: [PATCH] add rapidapi support --- .gitignore | 3 ++ src/main/lemondronor/advisorycircular.cljs | 31 ++++++++++++------- .../lemondronor/advisorycircular/util.cljs | 13 ++++++-- 3 files changed, 33 insertions(+), 14 deletions(-) diff --git a/.gitignore b/.gitignore index 17fe5ae..eeb41e0 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,7 @@ node_modules out .DS_Store screenshots +config.yaml secrets.yaml +build +advisorycircular.json diff --git a/src/main/lemondronor/advisorycircular.cljs b/src/main/lemondronor/advisorycircular.cljs index 15fef8f..aec3925 100644 --- a/src/main/lemondronor/advisorycircular.cljs +++ b/src/main/lemondronor/advisorycircular.cljs @@ -1,6 +1,7 @@ (ns lemondronor.advisorycircular (:require ["canvas" :as canvas] + [cemerick.url :as c-url] ["commander" :as commander] ["fs" :as fs] ["tmp-promise" :as tmp] @@ -107,15 +108,18 @@ (js->clj (parse-json json-str)) "ac"))}) - -(defn get-adsbexchange-live-data [{:keys [url lat lon radius-nm api-key api-whitelist]}] - (let [url (->> [url - "lat" lat - "lon" lon - "dist" (.toFixed radius-nm 1)] - (map str) - (string/join "/")) - headers (cond-> {:api-auth api-key} +(defn get-adsbexchange-live-data [{:keys [url lat lon radius-nm api-key api-whitelist rapid-api?]}] + (let [url (str + (->> [url + "lat" lat + "lon" lon + "dist" (.toFixed radius-nm (if rapid-api? 0 1))] + (map str) + (string/join "/")) + (if rapid-api? "/" "")) + headers (cond-> (if rapid-api? + {:x-rapidapi-key api-key :x-rapidapi-host (:host (c-url/url url)) :useQueryString true} + {:auth-key api-key}) api-whitelist (assoc :ADSBX-WL api-whitelist))] (p/let [http-result (util/http-get url {:headers headers})] (let [result (parse-adsbexchange-live-data http-result)] @@ -716,7 +720,8 @@ :minimum-airport-distance-km 3.5 :history-db-path "advisorycircular.json" :aircraft-info-db-path "aircraft-info.sqb" - :twitter {:enabled? true}}) + :twitter {:enabled? true} + :rapid-api? false}) (defn build-config-from-commander [commander] @@ -754,7 +759,8 @@ [:aircraft-info-db-path] [:adsbx :url] [:adsbx :api-key] - [:pelias :url]] + [:pelias :url] + [:rapid-api?]] present (util/nested-keys config) missing1 (set/difference (set required) (set present)) missing2 (when (get-in config [:twitter :enabled?]) @@ -826,7 +832,8 @@ (merge (:adsbx config) {:lat (:lat config) :lon (:lon config) - :radius-nm (* (:radius-km config) 0.539957)})) + :radius-nm (* (:radius-km config) 0.539957) + :rapid-api? (:rapid-api? config)})) filtered-api-data (remove-blocked-icaos api-data (get config :icao-blocklist '())) now (current-time) [new-db potential-circles] (-> db diff --git a/src/main/lemondronor/advisorycircular/util.cljs b/src/main/lemondronor/advisorycircular/util.cljs index af3a781..c47eed1 100644 --- a/src/main/lemondronor/advisorycircular/util.cljs +++ b/src/main/lemondronor/advisorycircular/util.cljs @@ -1,6 +1,7 @@ (ns lemondronor.advisorycircular.util (:require [cemerick.url :as c-url] ["fs" :as fs] + [goog.string :as gstring] [kitchen-async.promise :as p] [lemondronor.advisorycircular.logging :as logging] ["request-promise-native" :as request] @@ -11,6 +12,9 @@ (def fs-promises (.-promises fs)) +(defn ^boolean ends-with? + [s substr] + (gstring/endsWith s substr)) ;; Reads a file, returns a promise resolving to the file contents. @@ -51,8 +55,13 @@ ([url options] (let [query (or (:query options) {}) options (dissoc options :query) - url (-> (c-url/url (str url)) - (assoc :query query))] + parsed-url (-> (c-url/url (str url)) + (assoc :query query)) + ;; Work around c-url bug to re-append any stripped trailing slash. + ;; https://github.com/cemerick/url/issues/24 + url (if (ends-with? (str url) "/") + (str parsed-url "/") + parsed-url)] (p/do (log-verbose "Fetching %s" url) (p/let [result (.get request