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,
|
||||
ArtistSummary,
|
||||
Genre,
|
||||
MusicLibrary,
|
||||
MusicService,
|
||||
slice2,
|
||||
Track,
|
||||
@@ -255,6 +254,42 @@ export const 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 = {
|
||||
credentials?: Credentials;
|
||||
};
|
||||
@@ -281,73 +316,29 @@ function bindSmapiSoapServiceToExpress(
|
||||
{ id }: { id: string },
|
||||
_,
|
||||
headers?: SoapyHeaders
|
||||
) => {
|
||||
if (!headers?.credentials) {
|
||||
throw {
|
||||
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 {
|
||||
) =>
|
||||
auth(musicService, accessTokens, id, headers).then(
|
||||
({ accessToken, type, typeId }) => ({
|
||||
getMediaURIResult: `${webAddress}/stream/${type}/${typeId}`,
|
||||
httpHeaders: [
|
||||
{
|
||||
header: BONOB_ACCESS_TOKEN_HEADER,
|
||||
value: accessTokens.mint(
|
||||
headers?.credentials?.loginToken.token
|
||||
),
|
||||
value: accessToken,
|
||||
},
|
||||
],
|
||||
};
|
||||
},
|
||||
})
|
||||
),
|
||||
getMediaMetadata: async (
|
||||
{ id }: { id: string },
|
||||
_,
|
||||
headers?: SoapyHeaders
|
||||
) => {
|
||||
if (!headers?.credentials) {
|
||||
throw {
|
||||
Fault: {
|
||||
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 {
|
||||
) =>
|
||||
auth(musicService, accessTokens, id, headers).then(
|
||||
async ({ musicLibrary, accessToken, typeId }) =>
|
||||
musicLibrary.track(typeId!).then((it) => ({
|
||||
getMediaMetadataResult: track(webAddress, accessToken, it),
|
||||
};
|
||||
});
|
||||
},
|
||||
}))
|
||||
),
|
||||
getExtendedMetadata: async (
|
||||
{
|
||||
id,
|
||||
@@ -357,35 +348,16 @@ function bindSmapiSoapServiceToExpress(
|
||||
{ id: string; index: number; count: number; recursive: boolean },
|
||||
_,
|
||||
headers?: SoapyHeaders
|
||||
) => {
|
||||
if (!headers?.credentials) {
|
||||
throw {
|
||||
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(":");
|
||||
) =>
|
||||
auth(musicService, accessTokens, id, headers).then(
|
||||
async ({ musicLibrary, accessToken, type, typeId }) => {
|
||||
const paging = { _index: index, _count: count };
|
||||
switch (type) {
|
||||
case "artist":
|
||||
return await musicLibrary.artist(typeId!).then((artist) => {
|
||||
const [page, total] = slice2<Album>(paging)(artist.albums);
|
||||
const accessToken = accessTokens.mint(authToken);
|
||||
|
||||
return musicLibrary.artist(typeId!).then((artist) => {
|
||||
const [page, total] = slice2<Album>(paging)(
|
||||
artist.albums
|
||||
);
|
||||
return {
|
||||
getExtendedMetadataResult: {
|
||||
count: page.length,
|
||||
@@ -409,7 +381,8 @@ function bindSmapiSoapServiceToExpress(
|
||||
default:
|
||||
throw `Unsupported id:${id}`;
|
||||
}
|
||||
},
|
||||
}
|
||||
),
|
||||
getMetadata: async (
|
||||
{
|
||||
id,
|
||||
@@ -419,34 +392,16 @@ function bindSmapiSoapServiceToExpress(
|
||||
{ id: string; index: number; count: number; recursive: boolean },
|
||||
_,
|
||||
headers?: SoapyHeaders
|
||||
) => {
|
||||
if (!headers?.credentials) {
|
||||
throw {
|
||||
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(":");
|
||||
) =>
|
||||
auth(musicService, accessTokens, id, headers).then(
|
||||
({ musicLibrary, accessToken, type, typeId }) => {
|
||||
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> =>
|
||||
musicLibrary.albums(q).then((result) => {
|
||||
const accessToken = accessTokens.mint(authToken);
|
||||
return getMetadataResult({
|
||||
mediaCollection: result.results.map((it) =>
|
||||
album(webAddress, accessToken, it)
|
||||
@@ -464,7 +419,10 @@ function bindSmapiSoapServiceToExpress(
|
||||
container({ id: "albums", title: "Albums" }),
|
||||
container({ id: "genres", title: "Genres" }),
|
||||
container({ id: "randomAlbums", title: "Random" }),
|
||||
container({ id: "recentlyAdded", title: "Recently Added" }),
|
||||
container({
|
||||
id: "recentlyAdded",
|
||||
title: "Recently Added",
|
||||
}),
|
||||
container({
|
||||
id: "recentlyPlayed",
|
||||
title: "Recently Played",
|
||||
@@ -474,8 +432,7 @@ function bindSmapiSoapServiceToExpress(
|
||||
total: 6,
|
||||
});
|
||||
case "artists":
|
||||
return await musicLibrary.artists(paging).then((result) => {
|
||||
const accessToken = accessTokens.mint(authToken);
|
||||
return musicLibrary.artists(paging).then((result) => {
|
||||
return getMetadataResult({
|
||||
mediaCollection: result.results.map((it) =>
|
||||
artist(webAddress, accessToken, it)
|
||||
@@ -485,34 +442,34 @@ function bindSmapiSoapServiceToExpress(
|
||||
});
|
||||
});
|
||||
case "albums": {
|
||||
return await albums({
|
||||
return albums({
|
||||
type: "alphabeticalByArtist",
|
||||
...paging,
|
||||
});
|
||||
}
|
||||
case "randomAlbums":
|
||||
return await albums({
|
||||
return albums({
|
||||
type: "random",
|
||||
...paging,
|
||||
});
|
||||
case "genre":
|
||||
return await albums({
|
||||
return albums({
|
||||
type: "byGenre",
|
||||
genre: typeId,
|
||||
...paging,
|
||||
});
|
||||
case "recentlyAdded":
|
||||
return await albums({
|
||||
return albums({
|
||||
type: "newest",
|
||||
...paging,
|
||||
});
|
||||
case "recentlyPlayed":
|
||||
return await albums({
|
||||
return albums({
|
||||
type: "frequent",
|
||||
...paging,
|
||||
});
|
||||
case "genres":
|
||||
return await musicLibrary
|
||||
return musicLibrary
|
||||
.genres()
|
||||
.then(slice2(paging))
|
||||
.then(([page, total]) =>
|
||||
@@ -523,12 +480,11 @@ function bindSmapiSoapServiceToExpress(
|
||||
})
|
||||
);
|
||||
case "artist":
|
||||
return await musicLibrary
|
||||
return musicLibrary
|
||||
.artist(typeId!)
|
||||
.then((artist) => artist.albums)
|
||||
.then(slice2(paging))
|
||||
.then(([page, total]) => {
|
||||
const accessToken = accessTokens.mint(authToken);
|
||||
return getMetadataResult({
|
||||
mediaCollection: page.map((it) =>
|
||||
album(webAddress, accessToken, it)
|
||||
@@ -538,12 +494,11 @@ function bindSmapiSoapServiceToExpress(
|
||||
});
|
||||
});
|
||||
case "relatedArtists":
|
||||
return await musicLibrary
|
||||
return musicLibrary
|
||||
.artist(typeId!)
|
||||
.then((artist) => artist.similarArtists)
|
||||
.then(slice2(paging))
|
||||
.then(([page, total]) => {
|
||||
const accessToken = accessTokens.mint(authToken);
|
||||
return getMetadataResult({
|
||||
mediaCollection: page.map((it) =>
|
||||
artist(webAddress, accessToken, it)
|
||||
@@ -553,11 +508,10 @@ function bindSmapiSoapServiceToExpress(
|
||||
});
|
||||
});
|
||||
case "album":
|
||||
return await musicLibrary
|
||||
return musicLibrary
|
||||
.tracks(typeId!)
|
||||
.then(slice2(paging))
|
||||
.then(([page, total]) => {
|
||||
const accessToken = accessTokens.mint(authToken);
|
||||
return getMetadataResult({
|
||||
mediaMetadata: page.map((it) =>
|
||||
track(webAddress, accessToken, it)
|
||||
@@ -569,7 +523,8 @@ function bindSmapiSoapServiceToExpress(
|
||||
default:
|
||||
throw `Unsupported id:${id}`;
|
||||
}
|
||||
},
|
||||
}
|
||||
),
|
||||
},
|
||||
},
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user