1
0
Fork 0
mirror of https://github.com/heyarne/airsonic-ui.git synced 2026-05-07 02:33:39 +02:00

Implement variadic url parameters

This commit is contained in:
Arne Schlüter 2019-01-14 22:17:27 +01:00
commit 6d3cd2104b
2 changed files with 26 additions and 3 deletions

View file

@ -6,16 +6,29 @@
:c "airsonic-ui-cljs" :c "airsonic-ui-cljs"
:v "1.15.0"}) :v "1.15.0"})
(defn- unroll-variadic-params
"Turns {:id [1 2 3], :foo :bar} into [[:id 1] [:id 2] [:id 3] [:foo :bar]]"
[params]
(->>
(map (fn [[k vs]]
(if (sequential? vs)
(map (fn [v] [k v]) vs)
[k vs])) params)
(flatten)
(partition 2)))
(def ^:private encode js/encodeURIComponent) (def ^:private encode js/encodeURIComponent)
(defn url (defn url
"Returns an absolute url to an API endpoint" "Returns an absolute url to an API endpoint"
[credentials endpoint params] [credentials endpoint params]
(let [server (:server credentials) (let [server (:server credentials)
query (->> (merge default-params (select-keys credentials [:u :p]) params) auth (select-keys credentials [:u :p])
query (->> (merge default-params auth params)
(unroll-variadic-params)
(map (fn [[k v]] (str (encode (name k)) "=" (encode v)))) (map (fn [[k v]] (str (encode (name k)) "=" (encode v))))
(str/join "&"))] (str/join "&"))]
(str server (when-not (str/ends-with? server "/") "/") "rest/" endpoint "?" query))) (str (str/replace server #"/+$" "") "/rest/" endpoint "?" query)))
(defn stream-url [credentials song-or-episode] (defn stream-url [credentials song-or-episode]
;; podcasts have a stream-id, normal songs just use their id ;; podcasts have a stream-id, normal songs just use their id
@ -63,4 +76,3 @@
#{:artistId :name :songCount :artist} :album #{:artistId :name :songCount :artist} :album
#{:id :name :albumCount} :artist #{:id :name :albumCount} :artist
:unknown))) :unknown)))

View file

@ -26,6 +26,17 @@
encoded-str (js/encodeURIComponent query)] encoded-str (js/encodeURIComponent query)]
(is (str/includes? (api/url {:server "http://localhost"} "search3" {:query query}) encoded-str))))) (is (str/includes? (api/url {:server "http://localhost"} "search3" {:query query}) encoded-str)))))
(deftest variadic-parameters
(testing "Should append list-like parameters correctly"
(is (= (count (re-seq #"id=" (api/url {:server "http://localost"} "test" {:id []}))) 0))
(is (= (count (re-seq #"id=" (api/url {:server "http://localost"} "test" {:id [1]}))) 1))
(is (= (count (re-seq #"id=" (api/url {:server "http://localost"} "test" {:id (range 10)}))) 10)))
(testing "Should keep the non-lists"
(let [mixed (api/url {:server "http://localost"} "test" {:id (range 5) :foo "bar"})]
(is (some? (re-find #"u=user" (api/url {:server "http://localhost"} "test" {:u "user"}))))
(is (and (some? (re-find #"foo=bar" mixed))
(= (count (re-seq #"id=" mixed)) 5))))))
(deftest stream-urls (deftest stream-urls
(testing "Should construct the url based on a song's id" (testing "Should construct the url based on a song's id"
(let [stream-url (api/stream-url {:server "http://localhost"} fixtures/song)] (let [stream-url (api/stream-url {:server "http://localhost"} fixtures/song)]