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

Cache API responses and make sure we remember more than just one

Closes #21.
Squashed commit of the following:

commit 964b29cf127cf51de86543d040bcb6c674b36d7e
Author: Arne Schlüter <arne@schlueter.is>
Date:   Wed Aug 22 17:56:48 2018 +0200

    Pass content for current route nicely to views

commit b469a0a4b69457ddf3a679ac1acc82fbaffdc8fd
Author: Arne Schlüter <arne@schlueter.is>
Date:   Wed Aug 22 16:01:04 2018 +0200

    Add response cache in app-db

commit da9faf89138f42ee544efc64c2e46787091b3dc7
Author: Arne Schlüter <arne@schlueter.is>
Date:   Wed Aug 22 13:40:57 2018 +0200

    Move api helpers and tests to own namespace
This commit is contained in:
Arne Schlüter 2018-08-22 17:58:03 +02:00
commit 2cdae0d683
13 changed files with 222 additions and 94 deletions

View file

@ -0,0 +1,51 @@
(ns airsonic-ui.api.events
"This namespace contains all events relevant to API interaction. It contains
an event handler which issues requests as well as the appropriate handlers,
which dispatch :notification events in case of errors."
(:require [re-frame.core :refer [reg-event-fx]]
[ajax.core :as ajax]
[airsonic-ui.api.helpers :as api]))
(defn- api-url
"Small helper function which makes constructing API URLs a bit easier"
[db endpoint params]
(let [creds (:credentials db)]
(api/url (:server creds) endpoint (merge params (select-keys creds [:u :p])))))
(defn- cache-path [endpoint params] [:api/responses [endpoint params]])
(defn api-request
"Event handler to issue API request; takes care of authorization based on our
current app state."
[{:keys [db]} [_ endpoint params]]
{:http-xhrio {:method :get
:uri (api-url db endpoint params)
:response-format (ajax/json-response-format {:keywords? true})
:on-success [:api/good-response endpoint params]
:on-failure [:api/failed-response endpoint params]}
:db (assoc-in db (conj (cache-path endpoint params) :api/is-loading?) true)})
(reg-event-fx :api/request api-request)
(defn good-api-response
"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."
[fx [_ endpoint params response]]
(let [response-cache (cons :db (cache-path endpoint params))]
(try
(assoc-in fx response-cache (api/unwrap-response response))
(catch ExceptionInfo e
{:dispatch [:notification/show :error (api/error-msg e)]
:db (update-in fx response-cache dissoc :api/is-loading?)}))))
(reg-event-fx :api/good-response good-api-response)
(defn failed-api-response
"Handler for catastrophic failures (network errors and such things)"
[fx [ev endpoint params]]
(let [response-cache (cons :db (cache-path endpoint params))]
{:log ["API call gone bad; are CORS headers missing? check for :status 0" ev] ; <- the :log effect is registered in ../events.cljs
:dispatch [:notification/show :error "Communication with server failed. Check browser logs for details."]
:db (update-in fx response-cache dissoc :api/is-loading?)}))
(reg-event-fx :api/failed-response failed-api-response)