mirror of
https://github.com/wkulhanek/bonob.git
synced 2025-12-21 17:33:29 +01:00
Pull auth code out into function
This commit is contained in:
203
src/smapi.ts
203
src/smapi.ts
@@ -12,7 +12,6 @@ import {
|
|||||||
AlbumSummary,
|
AlbumSummary,
|
||||||
ArtistSummary,
|
ArtistSummary,
|
||||||
Genre,
|
Genre,
|
||||||
MusicLibrary,
|
|
||||||
MusicService,
|
MusicService,
|
||||||
slice2,
|
slice2,
|
||||||
Track,
|
Track,
|
||||||
@@ -255,6 +254,42 @@ export const artist = (
|
|||||||
albumArtURI: defaultArtistArtURI(webAddress, accessToken, artist),
|
albumArtURI: defaultArtistArtURI(webAddress, accessToken, artist),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const auth = async (
|
||||||
|
musicService: MusicService,
|
||||||
|
accessTokens: AccessTokens,
|
||||||
|
id: string,
|
||||||
|
headers?: SoapyHeaders
|
||||||
|
) => {
|
||||||
|
if (!headers?.credentials) {
|
||||||
|
throw {
|
||||||
|
Fault: {
|
||||||
|
faultcode: "Client.LoginUnsupported",
|
||||||
|
faultstring: "Missing credentials...",
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
const authToken = headers.credentials.loginToken.token;
|
||||||
|
const accessToken = accessTokens.mint(authToken);
|
||||||
|
const [type, typeId] = id.split(":");
|
||||||
|
return musicService
|
||||||
|
.login(authToken)
|
||||||
|
.then((musicLibrary) => ({
|
||||||
|
musicLibrary,
|
||||||
|
authToken,
|
||||||
|
accessToken,
|
||||||
|
type,
|
||||||
|
typeId,
|
||||||
|
}))
|
||||||
|
.catch((_) => {
|
||||||
|
throw {
|
||||||
|
Fault: {
|
||||||
|
faultcode: "Client.LoginUnauthorized",
|
||||||
|
faultstring: "Credentials not found...",
|
||||||
|
},
|
||||||
|
};
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
type SoapyHeaders = {
|
type SoapyHeaders = {
|
||||||
credentials?: Credentials;
|
credentials?: Credentials;
|
||||||
};
|
};
|
||||||
@@ -281,73 +316,29 @@ function bindSmapiSoapServiceToExpress(
|
|||||||
{ id }: { id: string },
|
{ id }: { id: string },
|
||||||
_,
|
_,
|
||||||
headers?: SoapyHeaders
|
headers?: SoapyHeaders
|
||||||
) => {
|
) =>
|
||||||
if (!headers?.credentials) {
|
auth(musicService, accessTokens, id, headers).then(
|
||||||
throw {
|
({ accessToken, type, typeId }) => ({
|
||||||
Fault: {
|
|
||||||
faultcode: "Client.LoginUnsupported",
|
|
||||||
faultstring: "Missing credentials...",
|
|
||||||
},
|
|
||||||
};
|
|
||||||
}
|
|
||||||
await musicService
|
|
||||||
.login(headers.credentials.loginToken.token)
|
|
||||||
.catch((_) => {
|
|
||||||
throw {
|
|
||||||
Fault: {
|
|
||||||
faultcode: "Client.LoginUnauthorized",
|
|
||||||
faultstring: "Credentials not found...",
|
|
||||||
},
|
|
||||||
};
|
|
||||||
});
|
|
||||||
|
|
||||||
const [type, typeId] = id.split(":");
|
|
||||||
return {
|
|
||||||
getMediaURIResult: `${webAddress}/stream/${type}/${typeId}`,
|
getMediaURIResult: `${webAddress}/stream/${type}/${typeId}`,
|
||||||
httpHeaders: [
|
httpHeaders: [
|
||||||
{
|
{
|
||||||
header: BONOB_ACCESS_TOKEN_HEADER,
|
header: BONOB_ACCESS_TOKEN_HEADER,
|
||||||
value: accessTokens.mint(
|
value: accessToken,
|
||||||
headers?.credentials?.loginToken.token
|
|
||||||
),
|
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
};
|
})
|
||||||
},
|
),
|
||||||
getMediaMetadata: async (
|
getMediaMetadata: async (
|
||||||
{ id }: { id: string },
|
{ id }: { id: string },
|
||||||
_,
|
_,
|
||||||
headers?: SoapyHeaders
|
headers?: SoapyHeaders
|
||||||
) => {
|
) =>
|
||||||
if (!headers?.credentials) {
|
auth(musicService, accessTokens, id, headers).then(
|
||||||
throw {
|
async ({ musicLibrary, accessToken, typeId }) =>
|
||||||
Fault: {
|
musicLibrary.track(typeId!).then((it) => ({
|
||||||
faultcode: "Client.LoginUnsupported",
|
|
||||||
faultstring: "Missing credentials...",
|
|
||||||
},
|
|
||||||
};
|
|
||||||
}
|
|
||||||
const authToken = headers.credentials.loginToken.token;
|
|
||||||
const login = await musicService
|
|
||||||
.login(headers.credentials.loginToken.token)
|
|
||||||
.catch((_) => {
|
|
||||||
throw {
|
|
||||||
Fault: {
|
|
||||||
faultcode: "Client.LoginUnauthorized",
|
|
||||||
faultstring: "Credentials not found...",
|
|
||||||
},
|
|
||||||
};
|
|
||||||
});
|
|
||||||
|
|
||||||
const typeId = id.split(":")[1];
|
|
||||||
const musicLibrary = login as MusicLibrary;
|
|
||||||
return musicLibrary.track(typeId!).then((it) => {
|
|
||||||
const accessToken = accessTokens.mint(authToken);
|
|
||||||
return {
|
|
||||||
getMediaMetadataResult: track(webAddress, accessToken, it),
|
getMediaMetadataResult: track(webAddress, accessToken, it),
|
||||||
};
|
}))
|
||||||
});
|
),
|
||||||
},
|
|
||||||
getExtendedMetadata: async (
|
getExtendedMetadata: async (
|
||||||
{
|
{
|
||||||
id,
|
id,
|
||||||
@@ -357,35 +348,16 @@ function bindSmapiSoapServiceToExpress(
|
|||||||
{ id: string; index: number; count: number; recursive: boolean },
|
{ id: string; index: number; count: number; recursive: boolean },
|
||||||
_,
|
_,
|
||||||
headers?: SoapyHeaders
|
headers?: SoapyHeaders
|
||||||
) => {
|
) =>
|
||||||
if (!headers?.credentials) {
|
auth(musicService, accessTokens, id, headers).then(
|
||||||
throw {
|
async ({ musicLibrary, accessToken, type, typeId }) => {
|
||||||
Fault: {
|
|
||||||
faultcode: "Client.LoginUnsupported",
|
|
||||||
faultstring: "Missing credentials...",
|
|
||||||
},
|
|
||||||
};
|
|
||||||
}
|
|
||||||
const authToken = headers.credentials.loginToken.token;
|
|
||||||
const login = await musicService.login(authToken).catch((_) => {
|
|
||||||
throw {
|
|
||||||
Fault: {
|
|
||||||
faultcode: "Client.LoginUnauthorized",
|
|
||||||
faultstring: "Credentials not found...",
|
|
||||||
},
|
|
||||||
};
|
|
||||||
});
|
|
||||||
|
|
||||||
const musicLibrary = login as MusicLibrary;
|
|
||||||
|
|
||||||
const [type, typeId] = id.split(":");
|
|
||||||
const paging = { _index: index, _count: count };
|
const paging = { _index: index, _count: count };
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case "artist":
|
case "artist":
|
||||||
return await musicLibrary.artist(typeId!).then((artist) => {
|
return musicLibrary.artist(typeId!).then((artist) => {
|
||||||
const [page, total] = slice2<Album>(paging)(artist.albums);
|
const [page, total] = slice2<Album>(paging)(
|
||||||
const accessToken = accessTokens.mint(authToken);
|
artist.albums
|
||||||
|
);
|
||||||
return {
|
return {
|
||||||
getExtendedMetadataResult: {
|
getExtendedMetadataResult: {
|
||||||
count: page.length,
|
count: page.length,
|
||||||
@@ -409,7 +381,8 @@ function bindSmapiSoapServiceToExpress(
|
|||||||
default:
|
default:
|
||||||
throw `Unsupported id:${id}`;
|
throw `Unsupported id:${id}`;
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
|
),
|
||||||
getMetadata: async (
|
getMetadata: async (
|
||||||
{
|
{
|
||||||
id,
|
id,
|
||||||
@@ -419,34 +392,16 @@ function bindSmapiSoapServiceToExpress(
|
|||||||
{ id: string; index: number; count: number; recursive: boolean },
|
{ id: string; index: number; count: number; recursive: boolean },
|
||||||
_,
|
_,
|
||||||
headers?: SoapyHeaders
|
headers?: SoapyHeaders
|
||||||
) => {
|
) =>
|
||||||
if (!headers?.credentials) {
|
auth(musicService, accessTokens, id, headers).then(
|
||||||
throw {
|
({ musicLibrary, accessToken, type, typeId }) => {
|
||||||
Fault: {
|
|
||||||
faultcode: "Client.LoginUnsupported",
|
|
||||||
faultstring: "Missing credentials...",
|
|
||||||
},
|
|
||||||
};
|
|
||||||
}
|
|
||||||
const authToken = headers.credentials.loginToken.token;
|
|
||||||
const login = await musicService.login(authToken).catch((_) => {
|
|
||||||
throw {
|
|
||||||
Fault: {
|
|
||||||
faultcode: "Client.LoginUnauthorized",
|
|
||||||
faultstring: "Credentials not found...",
|
|
||||||
},
|
|
||||||
};
|
|
||||||
});
|
|
||||||
|
|
||||||
const musicLibrary = login as MusicLibrary;
|
|
||||||
|
|
||||||
const [type, typeId] = id.split(":");
|
|
||||||
const paging = { _index: index, _count: count };
|
const paging = { _index: index, _count: count };
|
||||||
logger.debug(`Fetching metadata type=${type}, typeId=${typeId}`);
|
logger.debug(
|
||||||
|
`Fetching metadata type=${type}, typeId=${typeId}`
|
||||||
|
);
|
||||||
|
|
||||||
const albums = (q: AlbumQuery): Promise<GetMetadataResponse> =>
|
const albums = (q: AlbumQuery): Promise<GetMetadataResponse> =>
|
||||||
musicLibrary.albums(q).then((result) => {
|
musicLibrary.albums(q).then((result) => {
|
||||||
const accessToken = accessTokens.mint(authToken);
|
|
||||||
return getMetadataResult({
|
return getMetadataResult({
|
||||||
mediaCollection: result.results.map((it) =>
|
mediaCollection: result.results.map((it) =>
|
||||||
album(webAddress, accessToken, it)
|
album(webAddress, accessToken, it)
|
||||||
@@ -464,7 +419,10 @@ function bindSmapiSoapServiceToExpress(
|
|||||||
container({ id: "albums", title: "Albums" }),
|
container({ id: "albums", title: "Albums" }),
|
||||||
container({ id: "genres", title: "Genres" }),
|
container({ id: "genres", title: "Genres" }),
|
||||||
container({ id: "randomAlbums", title: "Random" }),
|
container({ id: "randomAlbums", title: "Random" }),
|
||||||
container({ id: "recentlyAdded", title: "Recently Added" }),
|
container({
|
||||||
|
id: "recentlyAdded",
|
||||||
|
title: "Recently Added",
|
||||||
|
}),
|
||||||
container({
|
container({
|
||||||
id: "recentlyPlayed",
|
id: "recentlyPlayed",
|
||||||
title: "Recently Played",
|
title: "Recently Played",
|
||||||
@@ -474,8 +432,7 @@ function bindSmapiSoapServiceToExpress(
|
|||||||
total: 6,
|
total: 6,
|
||||||
});
|
});
|
||||||
case "artists":
|
case "artists":
|
||||||
return await musicLibrary.artists(paging).then((result) => {
|
return musicLibrary.artists(paging).then((result) => {
|
||||||
const accessToken = accessTokens.mint(authToken);
|
|
||||||
return getMetadataResult({
|
return getMetadataResult({
|
||||||
mediaCollection: result.results.map((it) =>
|
mediaCollection: result.results.map((it) =>
|
||||||
artist(webAddress, accessToken, it)
|
artist(webAddress, accessToken, it)
|
||||||
@@ -485,34 +442,34 @@ function bindSmapiSoapServiceToExpress(
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
case "albums": {
|
case "albums": {
|
||||||
return await albums({
|
return albums({
|
||||||
type: "alphabeticalByArtist",
|
type: "alphabeticalByArtist",
|
||||||
...paging,
|
...paging,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
case "randomAlbums":
|
case "randomAlbums":
|
||||||
return await albums({
|
return albums({
|
||||||
type: "random",
|
type: "random",
|
||||||
...paging,
|
...paging,
|
||||||
});
|
});
|
||||||
case "genre":
|
case "genre":
|
||||||
return await albums({
|
return albums({
|
||||||
type: "byGenre",
|
type: "byGenre",
|
||||||
genre: typeId,
|
genre: typeId,
|
||||||
...paging,
|
...paging,
|
||||||
});
|
});
|
||||||
case "recentlyAdded":
|
case "recentlyAdded":
|
||||||
return await albums({
|
return albums({
|
||||||
type: "newest",
|
type: "newest",
|
||||||
...paging,
|
...paging,
|
||||||
});
|
});
|
||||||
case "recentlyPlayed":
|
case "recentlyPlayed":
|
||||||
return await albums({
|
return albums({
|
||||||
type: "frequent",
|
type: "frequent",
|
||||||
...paging,
|
...paging,
|
||||||
});
|
});
|
||||||
case "genres":
|
case "genres":
|
||||||
return await musicLibrary
|
return musicLibrary
|
||||||
.genres()
|
.genres()
|
||||||
.then(slice2(paging))
|
.then(slice2(paging))
|
||||||
.then(([page, total]) =>
|
.then(([page, total]) =>
|
||||||
@@ -523,12 +480,11 @@ function bindSmapiSoapServiceToExpress(
|
|||||||
})
|
})
|
||||||
);
|
);
|
||||||
case "artist":
|
case "artist":
|
||||||
return await musicLibrary
|
return musicLibrary
|
||||||
.artist(typeId!)
|
.artist(typeId!)
|
||||||
.then((artist) => artist.albums)
|
.then((artist) => artist.albums)
|
||||||
.then(slice2(paging))
|
.then(slice2(paging))
|
||||||
.then(([page, total]) => {
|
.then(([page, total]) => {
|
||||||
const accessToken = accessTokens.mint(authToken);
|
|
||||||
return getMetadataResult({
|
return getMetadataResult({
|
||||||
mediaCollection: page.map((it) =>
|
mediaCollection: page.map((it) =>
|
||||||
album(webAddress, accessToken, it)
|
album(webAddress, accessToken, it)
|
||||||
@@ -538,12 +494,11 @@ function bindSmapiSoapServiceToExpress(
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
case "relatedArtists":
|
case "relatedArtists":
|
||||||
return await musicLibrary
|
return musicLibrary
|
||||||
.artist(typeId!)
|
.artist(typeId!)
|
||||||
.then((artist) => artist.similarArtists)
|
.then((artist) => artist.similarArtists)
|
||||||
.then(slice2(paging))
|
.then(slice2(paging))
|
||||||
.then(([page, total]) => {
|
.then(([page, total]) => {
|
||||||
const accessToken = accessTokens.mint(authToken);
|
|
||||||
return getMetadataResult({
|
return getMetadataResult({
|
||||||
mediaCollection: page.map((it) =>
|
mediaCollection: page.map((it) =>
|
||||||
artist(webAddress, accessToken, it)
|
artist(webAddress, accessToken, it)
|
||||||
@@ -553,11 +508,10 @@ function bindSmapiSoapServiceToExpress(
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
case "album":
|
case "album":
|
||||||
return await musicLibrary
|
return musicLibrary
|
||||||
.tracks(typeId!)
|
.tracks(typeId!)
|
||||||
.then(slice2(paging))
|
.then(slice2(paging))
|
||||||
.then(([page, total]) => {
|
.then(([page, total]) => {
|
||||||
const accessToken = accessTokens.mint(authToken);
|
|
||||||
return getMetadataResult({
|
return getMetadataResult({
|
||||||
mediaMetadata: page.map((it) =>
|
mediaMetadata: page.map((it) =>
|
||||||
track(webAddress, accessToken, it)
|
track(webAddress, accessToken, it)
|
||||||
@@ -569,7 +523,8 @@ function bindSmapiSoapServiceToExpress(
|
|||||||
default:
|
default:
|
||||||
throw `Unsupported id:${id}`;
|
throw `Unsupported id:${id}`;
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
|
),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user