using MediaBrowser.Model.DTO; using MediaBrowser.Model.Entities; using System; using System.Collections.Generic; using System.IO; using System.Linq; namespace MediaBrowser.ApiInteraction { /// /// Provides api methods that are usable on all platforms /// public abstract class BaseApiClient : IDisposable { protected BaseApiClient() { DataSerializer.Configure(); } /// /// Gets or sets the server host name (myserver or 192.168.x.x) /// public string ServerHostName { get; set; } /// /// Gets or sets the port number used by the API /// public int ServerApiPort { get; set; } /// /// Gets the current api url based on hostname and port. /// protected string ApiUrl { get { return string.Format("http://{0}:{1}/mediabrowser/api", ServerHostName, ServerApiPort); } } /// /// Gets the default data format to request from the server /// protected SerializationFormats SerializationFormat { get { return SerializationFormats.Protobuf; } } /// /// Gets an image url that can be used to download an image from the api /// /// The Id of the item /// The type of image requested /// The image index, if there are multiple. Currently only applies to backdrops. Supply null or 0 for first backdrop. /// Use if a fixed width is required. Aspect ratio will be preserved. /// Use if a fixed height is required. Aspect ratio will be preserved. /// Use if a max width is required. Aspect ratio will be preserved. /// Use if a max height is required. Aspect ratio will be preserved. /// Quality level, from 0-100. Currently only applies to JPG. The default value should suffice. public string GetImageUrl(Guid itemId, ImageType imageType, int? imageIndex = null, int? width = null, int? height = null, int? maxWidth = null, int? maxHeight = null, int? quality = null) { string url = ApiUrl + "/image"; url += "?id=" + itemId.ToString(); url += "&type=" + imageType.ToString(); if (imageIndex.HasValue) { url += "&index=" + imageIndex; } if (width.HasValue) { url += "&width=" + width; } if (height.HasValue) { url += "&height=" + height; } if (maxWidth.HasValue) { url += "&maxWidth=" + maxWidth; } if (maxHeight.HasValue) { url += "&maxHeight=" + maxHeight; } if (quality.HasValue) { url += "&quality=" + quality; } return url; } /// /// Gets an image url that can be used to download an image from the api /// /// The Id of the user /// Use if a fixed width is required. Aspect ratio will be preserved. /// Use if a fixed height is required. Aspect ratio will be preserved. /// Use if a max width is required. Aspect ratio will be preserved. /// Use if a max height is required. Aspect ratio will be preserved. /// Quality level, from 0-100. Currently only applies to JPG. The default value should suffice. public string GetUserImageUrl(Guid userId, int? width = null, int? height = null, int? maxWidth = null, int? maxHeight = null, int? quality = null) { string url = ApiUrl + "/image"; url += "?userId=" + userId.ToString(); if (width.HasValue) { url += "&width=" + width; } if (height.HasValue) { url += "&height=" + height; } if (maxWidth.HasValue) { url += "&maxWidth=" + maxWidth; } if (maxHeight.HasValue) { url += "&maxHeight=" + maxHeight; } if (quality.HasValue) { url += "&quality=" + quality; } return url; } /// /// Gets an image url that can be used to download an image from the api /// /// The name of the person /// Use if a fixed width is required. Aspect ratio will be preserved. /// Use if a fixed height is required. Aspect ratio will be preserved. /// Use if a max width is required. Aspect ratio will be preserved. /// Use if a max height is required. Aspect ratio will be preserved. /// Quality level, from 0-100. Currently only applies to JPG. The default value should suffice. public string GetPersonImageUrl(string name, int? width = null, int? height = null, int? maxWidth = null, int? maxHeight = null, int? quality = null) { string url = ApiUrl + "/image"; url += "?personname=" + name; if (width.HasValue) { url += "&width=" + width; } if (height.HasValue) { url += "&height=" + height; } if (maxWidth.HasValue) { url += "&maxWidth=" + maxWidth; } if (maxHeight.HasValue) { url += "&maxHeight=" + maxHeight; } if (quality.HasValue) { url += "&quality=" + quality; } return url; } /// /// Gets an image url that can be used to download an image from the api /// /// The year /// Use if a fixed width is required. Aspect ratio will be preserved. /// Use if a fixed height is required. Aspect ratio will be preserved. /// Use if a max width is required. Aspect ratio will be preserved. /// Use if a max height is required. Aspect ratio will be preserved. /// Quality level, from 0-100. Currently only applies to JPG. The default value should suffice. public string GetYearImageUrl(int year, int? width = null, int? height = null, int? maxWidth = null, int? maxHeight = null, int? quality = null) { string url = ApiUrl + "/image"; url += "?year=" + year; if (width.HasValue) { url += "&width=" + width; } if (height.HasValue) { url += "&height=" + height; } if (maxWidth.HasValue) { url += "&maxWidth=" + maxWidth; } if (maxHeight.HasValue) { url += "&maxHeight=" + maxHeight; } if (quality.HasValue) { url += "&quality=" + quality; } return url; } /// /// Gets an image url that can be used to download an image from the api /// /// The name of the genre /// Use if a fixed width is required. Aspect ratio will be preserved. /// Use if a fixed height is required. Aspect ratio will be preserved. /// Use if a max width is required. Aspect ratio will be preserved. /// Use if a max height is required. Aspect ratio will be preserved. /// Quality level, from 0-100. Currently only applies to JPG. The default value should suffice. public string GetGenreImageUrl(string name, int? width = null, int? height = null, int? maxWidth = null, int? maxHeight = null, int? quality = null) { string url = ApiUrl + "/image"; url += "?genre=" + name; if (width.HasValue) { url += "&width=" + width; } if (height.HasValue) { url += "&height=" + height; } if (maxWidth.HasValue) { url += "&maxWidth=" + maxWidth; } if (maxHeight.HasValue) { url += "&maxHeight=" + maxHeight; } if (quality.HasValue) { url += "&quality=" + quality; } return url; } /// /// Gets an image url that can be used to download an image from the api /// /// The name of the studio /// Use if a fixed width is required. Aspect ratio will be preserved. /// Use if a fixed height is required. Aspect ratio will be preserved. /// Use if a max width is required. Aspect ratio will be preserved. /// Use if a max height is required. Aspect ratio will be preserved. /// Quality level, from 0-100. Currently only applies to JPG. The default value should suffice. public string GetStudioImageUrl(string name, int? width = null, int? height = null, int? maxWidth = null, int? maxHeight = null, int? quality = null) { string url = ApiUrl + "/image"; url += "?studio=" + name; if (width.HasValue) { url += "&width=" + width; } if (height.HasValue) { url += "&height=" + height; } if (maxWidth.HasValue) { url += "&maxWidth=" + maxWidth; } if (maxHeight.HasValue) { url += "&maxHeight=" + maxHeight; } if (quality.HasValue) { url += "&quality=" + quality; } return url; } /// /// This is a helper to get a list of backdrop url's from a given ApiBaseItemWrapper. If the actual item does not have any backdrops it will return backdrops from the first parent that does. /// /// A given item. /// Use if a fixed width is required. Aspect ratio will be preserved. /// Use if a fixed height is required. Aspect ratio will be preserved. /// Use if a max width is required. Aspect ratio will be preserved. /// Use if a max height is required. Aspect ratio will be preserved. /// Quality level, from 0-100. Currently only applies to JPG. The default value should suffice. public string[] GetBackdropImageUrls(DtoBaseItem item, int? width = null, int? height = null, int? maxWidth = null, int? maxHeight = null, int? quality = null) { Guid? backdropItemId; int backdropCount; if (item.BackdropCount == 0) { backdropItemId = item.ParentBackdropItemId; backdropCount = item.ParentBackdropCount ?? 0; } else { backdropItemId = item.Id; backdropCount = item.BackdropCount; } if (backdropItemId == null) { return new string[] { }; } var files = new string[backdropCount]; for (int i = 0; i < backdropCount; i++) { files[i] = GetImageUrl(backdropItemId.Value, ImageType.Backdrop, i, width, height, maxWidth, maxHeight, quality); } return files; } /// /// This is a helper to get the logo image url from a given ApiBaseItemWrapper. If the actual item does not have a logo, it will return the logo from the first parent that does, or null. /// /// A given item. /// Use if a fixed width is required. Aspect ratio will be preserved. /// Use if a fixed height is required. Aspect ratio will be preserved. /// Use if a max width is required. Aspect ratio will be preserved. /// Use if a max height is required. Aspect ratio will be preserved. /// Quality level, from 0-100. Currently only applies to JPG. The default value should suffice. public string GetLogoImageUrl(DtoBaseItem item, int? width = null, int? height = null, int? maxWidth = null, int? maxHeight = null, int? quality = null) { Guid? logoItemId = item.HasLogo ? item.Id : item.ParentLogoItemId; if (logoItemId.HasValue) { return GetImageUrl(logoItemId.Value, ImageType.Logo, null, width, height, maxWidth, maxHeight, quality); } return null; } /// /// Gets the url needed to stream an audio file /// /// The id of the item /// List all the output formats the decice is capable of playing. The more, the better, as it will decrease the likelyhood of having to encode, which will put a load on the server. /// The maximum number of channels that the device can play. Omit this if it doesn't matter. Phones and tablets should generally specify 2. /// The maximum sample rate that the device can play. This should generally be omitted. The server will default this to 44100, so only override if a different max is needed. public string GetAudioStreamUrl(Guid itemId, IEnumerable supportedOutputFormats, int? maxAudioChannels = null, int? maxAudioSampleRate = null) { string url = ApiUrl + "/audio?id=" + itemId; url += "&outputformats=" + string.Join(",", supportedOutputFormats.Select(s => s.ToString()).ToArray()); if (maxAudioChannels.HasValue) { url += "&audiochannels=" + maxAudioChannels.Value; } if (maxAudioSampleRate.HasValue) { url += "&audiosamplerate=" + maxAudioSampleRate.Value; } return url; } /// /// Gets the url needed to stream a video file /// /// The id of the item /// List all the output formats the decice is capable of playing. The more, the better, as it will decrease the likelyhood of having to encode, which will put a load on the server. /// The maximum number of channels that the device can play. Omit this if it doesn't matter. Phones and tablets should generally specify 2. /// The maximum sample rate that the device can play. This should generally be omitted. The server will default this to 44100, so only override if a different max is needed. /// Specify this is a fixed video width is required /// Specify this is a fixed video height is required /// Specify this is a max video width is required /// Specify this is a max video height is required public string GetVideoStreamUrl(Guid itemId, IEnumerable supportedOutputFormats, int? maxAudioChannels = null, int? maxAudioSampleRate = null, int? width = null, int? height = null, int? maxWidth = null, int? maxHeight = null) { string url = ApiUrl + "/video?id=" + itemId; url += "&outputformats=" + string.Join(",", supportedOutputFormats.Select(s => s.ToString()).ToArray()); if (maxAudioChannels.HasValue) { url += "&audiochannels=" + maxAudioChannels.Value; } if (maxAudioSampleRate.HasValue) { url += "&audiosamplerate=" + maxAudioSampleRate.Value; } if (width.HasValue) { url += "&width=" + width.Value; } if (height.HasValue) { url += "&height=" + height.Value; } if (maxWidth.HasValue) { url += "&maxWidth=" + maxWidth.Value; } if (maxHeight.HasValue) { url += "&maxHeight=" + maxHeight.Value; } return url; } protected T DeserializeFromStream(Stream stream) where T : class { return DataSerializer.DeserializeFromStream(stream, SerializationFormat); } public virtual void Dispose() { } } }