Factored out adsbx-url and adsbx-headers, with tests.

This commit is contained in:
John Wiseman 2020-10-12 14:11:39 -07:00
parent 632fb472e9
commit 02ee1c5050
2 changed files with 62 additions and 15 deletions

View File

@ -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)))

View File

@ -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})))