2020-03-31 10:27:39 -08:00
|
|
|
import { fetchUtils } from 'react-admin'
|
2020-11-10 15:27:28 -09:00
|
|
|
import { baseUrl } from '../utils'
|
2020-03-31 10:27:39 -08:00
|
|
|
|
|
|
|
|
const url = (command, id, options) => {
|
2020-02-15 10:20:04 -09:00
|
|
|
const params = new URLSearchParams()
|
|
|
|
|
params.append('u', localStorage.getItem('username'))
|
|
|
|
|
params.append('t', localStorage.getItem('subsonic-token'))
|
|
|
|
|
params.append('s', localStorage.getItem('subsonic-salt'))
|
|
|
|
|
params.append('f', 'json')
|
|
|
|
|
params.append('v', '1.8.0')
|
|
|
|
|
params.append('c', 'NavidromeUI')
|
2020-11-07 20:06:48 -09:00
|
|
|
id && params.append('id', id)
|
2020-02-07 08:36:26 -09:00
|
|
|
if (options) {
|
2020-02-15 10:20:04 -09:00
|
|
|
if (options.ts) {
|
|
|
|
|
options['_'] = new Date().getTime()
|
|
|
|
|
delete options.ts
|
|
|
|
|
}
|
|
|
|
|
Object.keys(options).forEach((k) => {
|
|
|
|
|
params.append(k, options[k])
|
|
|
|
|
})
|
2020-02-07 08:36:26 -09:00
|
|
|
}
|
2020-04-03 13:50:42 -08:00
|
|
|
const url = `/rest/${command}?${params.toString()}`
|
|
|
|
|
return baseUrl(url)
|
2020-02-07 08:36:26 -09:00
|
|
|
}
|
|
|
|
|
|
2020-04-25 06:30:54 -08:00
|
|
|
const scrobble = (id, submit) =>
|
|
|
|
|
fetchUtils.fetchJson(url('scrobble', id, { submission: submit }))
|
2020-03-31 10:27:39 -08:00
|
|
|
|
2020-10-03 16:06:38 -08:00
|
|
|
const star = (id) => fetchUtils.fetchJson(url('star', id))
|
|
|
|
|
|
|
|
|
|
const unstar = (id) => fetchUtils.fetchJson(url('unstar', id))
|
|
|
|
|
|
2020-08-14 08:11:35 -08:00
|
|
|
const download = (id) => (window.location.href = url('download', id))
|
2020-08-05 10:57:59 -08:00
|
|
|
|
2021-03-12 11:39:31 -09:00
|
|
|
const getCoverArtUrl = (record, size) => {
|
|
|
|
|
const options = {
|
|
|
|
|
...(record.updatedAt && { _: record.updatedAt }),
|
|
|
|
|
...(size && { size }),
|
|
|
|
|
}
|
|
|
|
|
return url('getCoverArt', record.coverArtId || 'not_found', options)
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-07 12:02:52 -08:00
|
|
|
const setRating = (id, rating) =>
|
|
|
|
|
fetchUtils.fetchJson(url('setRating', id, { rating }))
|
|
|
|
|
|
2021-03-12 11:39:31 -09:00
|
|
|
export default {
|
|
|
|
|
url,
|
2021-04-07 06:57:14 -08:00
|
|
|
getCoverArtUrl,
|
2021-03-12 11:39:31 -09:00
|
|
|
scrobble,
|
|
|
|
|
download,
|
|
|
|
|
star,
|
|
|
|
|
unstar,
|
2021-04-07 12:02:52 -08:00
|
|
|
setRating,
|
2021-03-12 11:39:31 -09:00
|
|
|
}
|