using System; using System.Threading; using System.Threading.Tasks; using TMDbLib.Objects.Authentication; using TMDbLib.Objects.General; using TMDbLib.Objects.Lists; using TMDbLib.Rest; namespace TMDbLib.Client { public partial class TMDbClient { private async Task GetManipulateMediaListAsyncInternal(string listId, int movieId, string method, CancellationToken cancellationToken = default) { RequireSessionId(SessionType.UserSession); if (string.IsNullOrWhiteSpace(listId)) throw new ArgumentNullException(nameof(listId)); // Movie Id is expected by the API and can not be null if (movieId <= 0) throw new ArgumentOutOfRangeException(nameof(movieId)); RestRequest req = _client.Create("list/{listId}/{method}"); req.AddUrlSegment("listId", listId); req.AddUrlSegment("method", method); AddSessionId(req, SessionType.UserSession); req.SetBody(new { media_id = movieId }); using RestResponse response = await req.Post(cancellationToken).ConfigureAwait(false); // Status code 12 = "The item/record was updated successfully" // Status code 13 = "The item/record was deleted successfully" PostReply item = await response.GetDataObject().ConfigureAwait(false); // TODO: Previous code checked for item=null return item.StatusCode == 12 || item.StatusCode == 13; } /// /// Retrieve a list by it's id /// /// The id of the list you want to retrieve /// If specified the api will attempt to return a localized result. ex: en,it,es /// A cancellation token public async Task GetListAsync(string listId, string language = null, CancellationToken cancellationToken = default) { if (string.IsNullOrWhiteSpace(listId)) throw new ArgumentNullException(nameof(listId)); RestRequest req = _client.Create("list/{listId}"); req.AddUrlSegment("listId", listId); language ??= DefaultLanguage; if (!string.IsNullOrWhiteSpace(language)) req.AddParameter("language", language); GenericList resp = await req.GetOfT(cancellationToken).ConfigureAwait(false); return resp; } /// /// Will check if the provided movie id is present in the specified list /// /// Id of the list to check in /// Id of the movie to check for in the list /// A cancellation token public async Task GetListIsMoviePresentAsync(string listId, int movieId, CancellationToken cancellationToken = default) { if (string.IsNullOrWhiteSpace(listId)) throw new ArgumentNullException(nameof(listId)); if (movieId <= 0) throw new ArgumentOutOfRangeException(nameof(movieId)); RestRequest req = _client.Create("list/{listId}/item_status"); req.AddUrlSegment("listId", listId); req.AddParameter("movie_id", movieId.ToString()); using RestResponse response = await req.Get(cancellationToken).ConfigureAwait(false); return (await response.GetDataObject().ConfigureAwait(false)).ItemPresent; } /// /// Adds a movie to a specified list /// /// The id of the list to add the movie to /// The id of the movie to add /// A cancellation token /// True if the method was able to add the movie to the list, will retrun false in case of an issue or when the movie was already added to the list /// Requires a valid user session /// Thrown when the current client object doens't have a user session assigned. public async Task ListAddMovieAsync(string listId, int movieId, CancellationToken cancellationToken = default) { return await GetManipulateMediaListAsyncInternal(listId, movieId, "add_item", cancellationToken).ConfigureAwait(false); } /// /// Clears a list, without confirmation. /// /// The id of the list to clear /// A cancellation token /// True if the method was able to remove the movie from the list, will retrun false in case of an issue or when the movie was not present in the list /// Requires a valid user session /// Thrown when the current client object doens't have a user session assigned. public async Task ListClearAsync(string listId, CancellationToken cancellationToken = default) { RequireSessionId(SessionType.UserSession); if (string.IsNullOrWhiteSpace(listId)) throw new ArgumentNullException(nameof(listId)); RestRequest request = _client.Create("list/{listId}/clear"); request.AddUrlSegment("listId", listId); request.AddParameter("confirm", "true"); AddSessionId(request, SessionType.UserSession); using RestResponse response = await request.Post(cancellationToken).ConfigureAwait(false); // Status code 12 = "The item/record was updated successfully" PostReply item = await response.GetDataObject().ConfigureAwait(false); // TODO: Previous code checked for item=null return item.StatusCode == 12; } /// /// Creates a new list for the user associated with the current session /// /// The name of the new list /// Optional description for the list /// Optional language that might indicate the language of the content in the list /// A cancellation token /// Requires a valid user session /// Thrown when the current client object doens't have a user session assigned. public async Task ListCreateAsync(string name, string description = "", string language = null, CancellationToken cancellationToken = default) { RequireSessionId(SessionType.UserSession); if (string.IsNullOrWhiteSpace(name)) throw new ArgumentNullException(nameof(name)); // Description is expected by the API and can not be null if (string.IsNullOrWhiteSpace(description)) description = ""; RestRequest req = _client.Create("list"); AddSessionId(req, SessionType.UserSession); language ??= DefaultLanguage; if (!string.IsNullOrWhiteSpace(language)) { req.SetBody(new { name = name, description = description, language = language }); } else { req.SetBody(new { name = name, description = description }); } using RestResponse response = await req.Post(cancellationToken).ConfigureAwait(false); return (await response.GetDataObject().ConfigureAwait(false)).ListId; } /// /// Deletes the specified list that is owned by the user /// /// A list id that is owned by the user associated with the current session id /// A cancellation token /// Requires a valid user session /// Thrown when the current client object doens't have a user session assigned. public async Task ListDeleteAsync(string listId, CancellationToken cancellationToken = default) { RequireSessionId(SessionType.UserSession); if (string.IsNullOrWhiteSpace(listId)) throw new ArgumentNullException(nameof(listId)); RestRequest req = _client.Create("list/{listId}"); req.AddUrlSegment("listId", listId); AddSessionId(req, SessionType.UserSession); using RestResponse response = await req.Delete(cancellationToken).ConfigureAwait(false); // Status code 13 = success PostReply item = await response.GetDataObject().ConfigureAwait(false); // TODO: Previous code checked for item=null return item.StatusCode == 13; } /// /// Removes a movie from the specified list /// /// The id of the list to add the movie to /// The id of the movie to add /// A cancellation token /// True if the method was able to remove the movie from the list, will retrun false in case of an issue or when the movie was not present in the list /// Requires a valid user session /// Thrown when the current client object doens't have a user session assigned. public async Task ListRemoveMovieAsync(string listId, int movieId, CancellationToken cancellationToken = default) { return await GetManipulateMediaListAsyncInternal(listId, movieId, "remove_item", cancellationToken).ConfigureAwait(false); } } }