diff --git a/src/main/lemondronor/advisorycircular.cljs b/src/main/lemondronor/advisorycircular.cljs index 90e6ee5..2c67e3d 100644 --- a/src/main/lemondronor/advisorycircular.cljs +++ b/src/main/lemondronor/advisorycircular.cljs @@ -108,20 +108,30 @@ (js->clj (parse-json json-str)) "ac"))}) -(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 "/")) - ;; RapidAPI expects a trailing slash. - (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))] + +(defn adsbx-url [{:keys [url lat lon radius-nm rapid-api?]}] + (let [url-pieces [url + "lat" lat + "lon" lon + "dist" (.toFixed radius-nm (if rapid-api? 0 1))] + url-str (->> url-pieces + (map str) + (string/join "/"))] + url-str)) + + +(defn adsbx-headers [{:keys [url api-key api-whitelist rapid-api?]}] + (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))) + + +(defn get-adsbexchange-live-data [options] + (let [url (adsbx-url options) + headers (adsbx-headers (assoc options :url url))] (p/let [http-result (util/http-get url {:headers headers})] (let [result (parse-adsbexchange-live-data http-result)] (log-verbose "Got %s aircraft from API" (count (:aircraft result))) diff --git a/src/test/lemondronor/advisorycircular_test.cljs b/src/test/lemondronor/advisorycircular_test.cljs index 87d79e5..3ee52b9 100644 --- a/src/test/lemondronor/advisorycircular_test.cljs +++ b/src/test/lemondronor/advisorycircular_test.cljs @@ -2,7 +2,8 @@ (:require [cljs.test :refer (async deftest is testing)] [kitchen-async.promise :as p] [lemondronor.advisorycircular :as advisorycircular] - [lemondronor.advisorycircular.pelias :as pelias])) + [lemondronor.advisorycircular.pelias :as pelias] + [lemondronor.advisorycircular.util :as util])) (def epsilon 0.0000010) @@ -457,3 +458,39 @@ :aircraft-info-db-path "/" :pelias {:url "http://pelias/"} :icaos ["123" "456"]}))))) + + +(deftest adsbx-url + (is (= (advisorycircular/adsbx-url + {:url "http://adsbx" + :lat 0 + :lon 1 + :radius-nm 2}) + "http://adsbx/lat/0/lon/1/dist/2.0")) + (is (= (advisorycircular/adsbx-url + {:url "http://adsbx" + :lat 0 + :lon 1 + :radius-nm 2 + :rapid-api? true}) + "http://adsbx/lat/0/lon/1/dist/2"))) + + +(deftest adsbx-headers + (is (= (advisorycircular/adsbx-headers + {:url "http://adsbx/lat/0/lon/1/dist/2" + :api-key "key"}) + {:auth-key "key"})) + (is (= (advisorycircular/adsbx-headers + {:url "http://adsbx/lat/0/lon/1/dist/2" + :api-key "key" + :api-whitelist "whitelist"}) + {:auth-key "key" + :ADSBX-WL "whitelist"})) + (is (= (advisorycircular/adsbx-headers + {:url "http://adsbx/lat/0/lon/1/dist/2" + :api-key "key" + :rapid-api? true}) + {:x-rapidapi-key "key" + :x-rapidapi-host "adsbx" + :useQueryString true})))