Add getItemImageUrl method

This commit is contained in:
Bill Thornton 2021-10-14 14:39:41 -04:00
parent 03caf5a813
commit 7625d832a2
3 changed files with 70 additions and 4 deletions

View File

@ -9,10 +9,13 @@ import { mocked } from 'ts-jest/utils';
import { Api, AUTHORIZATION_HEADER } from '..';
import { SERVER_URL, TEST_CLIENT, TEST_DEVICE } from '../__helpers__/common';
import { ActivityLogApi, ApiKeyApi, ArtistsApi, AudioApi, BrandingApi, ChannelsApi, CollectionApi, ConfigurationApi, DashboardApi, DevicesApi, DisplayPreferencesApi, DlnaApi, DlnaServerApi, DynamicHlsApi, EnvironmentApi, FilterApi, GenresApi, HlsSegmentApi, ImageApi, ImageByNameApi, InstantMixApi, ItemLookupApi, ItemRefreshApi, ItemsApi, ItemUpdateApi, LibraryApi, LibraryStructureApi, LiveTvApi, LocalizationApi, MediaInfoApi, MoviesApi, MusicGenresApi, NotificationsApi, PackageApi, PersonsApi, PlaylistsApi, PlaystateApi, PluginsApi, QuickConnectApi, RemoteImageApi, ScheduledTasksApi, SearchApi, SessionApi, StartupApi, StudiosApi, SubtitleApi, SuggestionsApi, SyncPlayApi, SystemApi, TimeSyncApi, TrailersApi, TvShowsApi, UniversalAudioApi, UserApi, UserLibraryApi, UserViewsApi, VideoAttachmentsApi, VideoHlsApi, VideosApi, YearsApi } from '../generated-client';
import { ActivityLogApi, ApiKeyApi, ArtistsApi, AudioApi, BrandingApi, ChannelsApi, CollectionApi, ConfigurationApi, DashboardApi, DevicesApi, DisplayPreferencesApi, DlnaApi, DlnaServerApi, DynamicHlsApi, EnvironmentApi, FilterApi, GenresApi, HlsSegmentApi, ImageApi, ImageByNameApi, ImageType, InstantMixApi, ItemLookupApi, ItemRefreshApi, ItemsApi, ItemUpdateApi, LibraryApi, LibraryStructureApi, LiveTvApi, LocalizationApi, MediaInfoApi, MoviesApi, MusicGenresApi, NotificationsApi, PackageApi, PersonsApi, PlaylistsApi, PlaystateApi, PluginsApi, QuickConnectApi, RemoteImageApi, ScheduledTasksApi, SearchApi, SessionApi, StartupApi, StudiosApi, SubtitleApi, SuggestionsApi, SyncPlayApi, SystemApi, TimeSyncApi, TrailersApi, TvShowsApi, UniversalAudioApi, UserApi, UserLibraryApi, UserViewsApi, VideoAttachmentsApi, VideoHlsApi, VideosApi, YearsApi } from '../generated-client';
import { getAuthorizationHeader } from '../utils';
jest.mock('axios');
jest.mock('axios', () => ({
...jest.requireActual('axios'),
request: jest.fn()
}));
const mockAxios = mocked(axios, true);
const TEST_ACCESS_TOKEN = 'TEST-ACCESS-TOKEN';
@ -70,6 +73,13 @@ describe('Api', () => {
expect(api.authorizationHeader).toBe(getAuthorizationHeader(TEST_CLIENT, TEST_DEVICE));
});
it('should return an item image url', () => {
const api = new Api(SERVER_URL, TEST_CLIENT, TEST_DEVICE);
expect(api.getItemImageUrl('TEST')).toBe('https://example.com/Items/TEST/Images/Primary');
expect(api.getItemImageUrl('TEST', ImageType.Backdrop, { fillWidth: 100, fillHeight: 100 }))
.toBe('https://example.com/Items/TEST/Images/Backdrop?fillWidth=100&fillHeight=100');
});
it('should return api instances', () => {
const api = new Api(SERVER_URL, TEST_CLIENT, TEST_DEVICE);

View File

@ -5,10 +5,16 @@
*/
import globalInstance, { AxiosInstance, AxiosResponse } from 'axios';
import { ActivityLogApi, ApiKeyApi, ArtistsApi, AudioApi, AuthenticationResult, BrandingApi, ChannelsApi, CollectionApi, Configuration, ConfigurationApi, DashboardApi, DevicesApi, DisplayPreferencesApi, DlnaApi, DlnaServerApi, DynamicHlsApi, EnvironmentApi, FilterApi, GenresApi, HlsSegmentApi, ImageApi, ImageByNameApi, InstantMixApi, ItemLookupApi, ItemRefreshApi, ItemsApi, ItemUpdateApi, LibraryApi, LibraryStructureApi, LiveTvApi, LocalizationApi, MediaInfoApi, MoviesApi, MusicGenresApi, NotificationsApi, PackageApi, PersonsApi, PlaylistsApi, PlaystateApi, PluginsApi, QuickConnectApi, RemoteImageApi, ScheduledTasksApi, SearchApi, SessionApi, StartupApi, StudiosApi, SubtitleApi, SuggestionsApi, SyncPlayApi, SystemApi, TimeSyncApi, TrailersApi, TvShowsApi, UniversalAudioApi, UserApi, UserLibraryApi, UserViewsApi, VideoAttachmentsApi, VideoHlsApi, VideosApi, YearsApi } from './generated-client';
import { ActivityLogApi, ApiKeyApi, ArtistsApi, AudioApi, AuthenticationResult, BrandingApi, ChannelsApi, CollectionApi, Configuration, ConfigurationApi, DashboardApi, DevicesApi, DisplayPreferencesApi, DlnaApi, DlnaServerApi, DynamicHlsApi, EnvironmentApi, FilterApi, GenresApi, HlsSegmentApi, ImageApi, ImageByNameApi, ImageType, InstantMixApi, ItemLookupApi, ItemRefreshApi, ItemsApi, ItemUpdateApi, LibraryApi, LibraryStructureApi, LiveTvApi, LocalizationApi, MediaInfoApi, MoviesApi, MusicGenresApi, NotificationsApi, PackageApi, PersonsApi, PlaylistsApi, PlaystateApi, PluginsApi, QuickConnectApi, RemoteImageApi, ScheduledTasksApi, SearchApi, SessionApi, StartupApi, StudiosApi, SubtitleApi, SuggestionsApi, SyncPlayApi, SystemApi, TimeSyncApi, TrailersApi, TvShowsApi, UniversalAudioApi, UserApi, UserLibraryApi, UserViewsApi, VideoAttachmentsApi, VideoHlsApi, VideosApi, YearsApi } from './generated-client';
import { ClientInfo, DeviceInfo } from './models';
import { ImageRequestParameters } from './models/image-request-parameters';
import { getAuthorizationHeader } from './utils';
// HACK: Axios does not export types for axios/lib. This is a workaround.
type BuildFullPathFunction = (baseURL?: string, requestedURL?: string) => string;
// eslint-disable-next-line @typescript-eslint/no-var-requires
const buildFullPath: BuildFullPathFunction = require('axios/lib/core/buildFullPath');
/** The authorization header field name. */
export const AUTHORIZATION_HEADER = 'X-Emby-Authorization';
@ -43,7 +49,8 @@ export class Api {
/**
* Convenience method for authenticating a user by name and updating the internal state.
* @param authenticateUserByNameParam The authentication parameters object.
* @param username The username.
* @param password The user password if required.
*/
authenticateUserByName(username: string, password?: string): Promise<AxiosResponse<AuthenticationResult>> {
return this.userApi.authenticateUserByName(
@ -68,6 +75,24 @@ export class Api {
});
}
/**
* Get an item image URL.
* @param itemId The Item ID.
* @param imageType An optional Image Type (Primary by default).
* @param params Additional request parameters.
* @returns The image URL.
*/
getItemImageUrl(itemId: string, imageType = ImageType.Primary, params: ImageRequestParameters = {}): string | undefined {
// TODO: We could probably use ImageApiAxiosParamCreator to make this more robust
const uri = this.axiosInstance.getUri({
url: `/Items/${itemId}/Images/${imageType}`,
params
});
// NOTE: This behavior will probably be the default in axios in the future
// https://github.com/axios/axios/issues/2468
return buildFullPath(this.basePath, uri);
}
get authorizationHeader(): string {
return getAuthorizationHeader(this.clientInfo, this.deviceInfo, this.accessToken);
}

View File

@ -0,0 +1,31 @@
/**
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
import { ImageFormat } from '../generated-client';
/**
* Interface representing supported request parameters for the getItemImage API.
* (May also work for other image APIs)
*/
export interface ImageRequestParameters {
maxWidth?: number,
maxHeight?: number,
width?: number,
height?: number,
quality?: number,
fillWidth?: number,
fillHeight?: number,
tag?: string,
cropWhitespace?: boolean,
format?: ImageFormat,
addPlayedIndicator?: boolean,
percentPlayed?: number,
unplayedCount?: number,
blur?: number,
backgroundColor?: string,
foregroundLayer?: string,
imageIndex?: number
}