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

Use more sensible naming for api responses

This commit is contained in:
heyarne 2019-12-07 22:17:42 +01:00
commit 02f558226c
4 changed files with 16 additions and 16 deletions

View file

@ -15,13 +15,13 @@
{:http-xhrio {:method :get {:http-xhrio {:method :get
:uri (api/url (:credentials db) endpoint params) :uri (api/url (:credentials db) endpoint params)
:response-format (ajax/json-response-format {:keywords? true}) :response-format (ajax/json-response-format {:keywords? true})
:on-success [:api/good-response endpoint params] :on-success [:api.response/ok endpoint params]
:on-failure [:api/failed-response endpoint params]} :on-failure [:api.response/failed endpoint params]}
:db (assoc-in db (conj (cache-path endpoint params) :api/is-loading?) true)}) :db (assoc-in db (conj (cache-path endpoint params) :api/is-loading?) true)})
(reg-event-fx :api/request api-request) (reg-event-fx :api/request api-request)
(defn good-api-response (defn api-success
"Handles when the server responded. There could still be an error while "Handles when the server responded. There could still be an error while
processing the request on the server side which we have to account for." processing the request on the server side which we have to account for."
[{:keys [db]} [_ endpoint params response]] [{:keys [db]} [_ endpoint params response]]
@ -32,9 +32,9 @@
{:dispatch [:notification/show :error (api/error-msg e)] {:dispatch [:notification/show :error (api/error-msg e)]
:db (update-in db response-cache dissoc :api/is-loading?)})))) :db (update-in db response-cache dissoc :api/is-loading?)}))))
(reg-event-fx :api/good-response good-api-response) (reg-event-fx :api.response/ok api-success)
(defn failed-api-response (defn api-failure
"Handler for catastrophic failures (network errors and such things)" "Handler for catastrophic failures (network errors and such things)"
[fx [ev endpoint params]] [fx [ev endpoint params]]
(let [response-cache (cons :db (cache-path endpoint params))] (let [response-cache (cons :db (cache-path endpoint params))]
@ -42,4 +42,4 @@
:dispatch [:notification/show :error "Communication with server failed. Check browser logs for details."] :dispatch [:notification/show :error "Communication with server failed. Check browser logs for details."]
:db (update-in fx response-cache dissoc :api/is-loading?)})) :db (update-in fx response-cache dissoc :api/is-loading?)}))
(reg-event-fx :api/failed-response failed-api-response) (reg-event-fx :api.response/failed api-failure)

View file

@ -67,7 +67,7 @@
:uri (api/url credentials "getUser" {:username (:u credentials)}) :uri (api/url credentials "getUser" {:username (:u credentials)})
:response-format (ajax/json-response-format {:keywords? true}) :response-format (ajax/json-response-format {:keywords? true})
:on-success [:credentials/authentication-response credentials] :on-success [:credentials/authentication-response credentials]
:on-failure [:api/failed-response]}}) ; <- we don't need endpoint and params here because the response is not cached :on-failure [:api.response/failed]}}) ; <- we don't need endpoint and params here because the response is not cached
(rf/reg-event-fx :credentials/send-authentication-request authentication-request) (rf/reg-event-fx :credentials/send-authentication-request authentication-request)

View file

@ -7,7 +7,7 @@
(deftest api-failure-notifcations (deftest api-failure-notifcations
(testing "Should show an error notification when airsonic responds with an error" (testing "Should show an error notification when airsonic responds with an error"
(let [fx (events/good-api-response {} [:api/good-response "ping" nil (:error fixtures/responses)]) (let [fx (events/api-success {} [:api.response/ok "ping" nil (:error fixtures/responses)])
ev (:dispatch fx)] ev (:dispatch fx)]
(is (= :notification/show (first ev))) (is (= :notification/show (first ev)))
(is (= :error (second ev)))))) (is (= :error (second ev))))))
@ -18,13 +18,13 @@
(testing "Should be cached" (testing "Should be cached"
(testing "when the response was successful" (testing "when the response was successful"
(let [endpoint "getScanStatus" (let [endpoint "getScanStatus"
successful (events/good-api-response {} [:api/good-response endpoint nil (:ok fixtures/responses)]) successful (events/api-success {} [:api.response/ok endpoint nil (:ok fixtures/responses)])
unsuccessful (events/good-api-response {} [:api/good-response endpoint nil (:error fixtures/responses)])] unsuccessful (events/api-success {} [:api.response/ok endpoint nil (:error fixtures/responses)])]
(is (map? (cache successful [endpoint]))) (is (map? (cache successful [endpoint])))
(is (nil? (cache unsuccessful [endpoint]))))) (is (nil? (cache unsuccessful [endpoint])))))
(testing "in an unwrapped format" (testing "in an unwrapped format"
(let [endpoint "getScanStatus" (let [endpoint "getScanStatus"
fx (events/good-api-response {} [:api/good-response endpoint nil (:ok fixtures/responses)])] fx (events/api-success {} [:api.response/ok endpoint nil (:ok fixtures/responses)])]
(is (= #{:count :scanning} (set (keys (cache fx [endpoint])))))))) (is (= #{:count :scanning} (set (keys (cache fx [endpoint]))))))))
(testing "When being issued" (testing "When being issued"
(let [endpoint "getScanStatus" (let [endpoint "getScanStatus"
@ -34,16 +34,16 @@
(is (contains? fx :http-xhrio))) (is (contains? fx :http-xhrio)))
(testing "should indicate that a request is ongoing" (testing "should indicate that a request is ongoing"
(is (true? (:api/is-loading? (cache fx [endpoint]))) "for non-cached responses") (is (true? (:api/is-loading? (cache fx [endpoint]))) "for non-cached responses")
(is (true? (-> (events/good-api-response fx [:api/good-response endpoint nil (:ok fixtures/responses)]) (is (true? (-> (events/api-success fx [:api.response/ok endpoint nil (:ok fixtures/responses)])
(events/api-request [:api/request endpoint]) (events/api-request [:api/request endpoint])
(cache [endpoint]) (cache [endpoint])
:api/is-loading?)) "for cached responses")) :api/is-loading?)) "for cached responses"))
(testing "should remove the indication that a request is ongoing when there is a response" (testing "should remove the indication that a request is ongoing when there is a response"
(is (not (:api/is-loading? (-> (events/good-api-response fx [:api/good-response endpoint nil (:ok fixtures/responses)]) (is (not (:api/is-loading? (-> (events/api-success fx [:api.response/ok endpoint nil (:ok fixtures/responses)])
(cache [endpoint])))) "for a good response") (cache [endpoint])))) "for a good response")
(is (not (:api/is-loading? (-> (merge fx (events/good-api-response fx [:api/good-response endpoint nil (:error fixtures/responses)])) (is (not (:api/is-loading? (-> (merge fx (events/api-success fx [:api.response/ok endpoint nil (:error fixtures/responses)]))
(cache [endpoint])))) "when an error is returned") (cache [endpoint])))) "when an error is returned")
(is (not (:api/is-loading? (-> (merge fx (events/failed-api-response fx [:api/failed-response endpoint])) (is (not (:api/is-loading? (-> (merge fx (events/api-failure fx [:api.response/failed endpoint]))
(cache [endpoint])))) "when communication with the server failed")))) (cache [endpoint])))) "when communication with the server failed"))))
(testing "Should be able to avoid the cache" (testing "Should be able to avoid the cache"
;; FIXME: Implement this ;; FIXME: Implement this

View file

@ -62,7 +62,7 @@
(testing "invokes correct callback on server response" (testing "invokes correct callback on server response"
(is (= [:credentials/authentication-response fixtures/credentials] (:on-success request)))) (is (= [:credentials/authentication-response fixtures/credentials] (:on-success request))))
(testing "invokes correct callback when server is not reachable" (testing "invokes correct callback when server is not reachable"
(is (= [:api/failed-response] (:on-failure request)))))) (is (= [:api.response/failed] (:on-failure request))))))
(deftest authentication-response (deftest authentication-response
(testing "On success" (testing "On success"