Popcorn.Services.Movie.MovieService.LoadSubtitlesAsync C# (CSharp) Метод

LoadSubtitlesAsync() публичный Метод

Get the movie's subtitles according to a language
public LoadSubtitlesAsync ( MovieFull movie, CancellationToken ct ) : Task
movie Popcorn.Models.Movie.Full.MovieFull The movie of which to retrieve its subtitles
ct System.Threading.CancellationToken Cancellation token
Результат Task
        public async Task LoadSubtitlesAsync(MovieFull movie,
            CancellationToken ct)
        {
            var watch = Stopwatch.StartNew();

            var restClient = new RestClient(Constants.YifySubtitlesApi);
            var request = new RestRequest("/{segment}", Method.GET);
            request.AddUrlSegment("segment", movie.ImdbCode);

            try
            {
                var response = await restClient.ExecuteGetTaskAsync<SubtitlesWrapper>(request, ct);
                if (response.ErrorException != null)
                    throw response.ErrorException;

                var wrapper = response.Data;

                var subtitles = new ObservableCollection<Subtitle>();
                Dictionary<string, List<Subtitle>> movieSubtitles;
                if (wrapper.Subtitles == null)
                {
                    await Task.CompletedTask;
                    return;
                }
                if (wrapper.Subtitles.TryGetValue(movie.ImdbCode, out movieSubtitles))
                {
                    foreach (var subtitle in movieSubtitles)
                    {
                        var sub = subtitle.Value.Aggregate((sub1, sub2) => sub1.Rating > sub2.Rating ? sub1 : sub2);
                        subtitles.Add(new Subtitle
                        {
                            Id = sub.Id,
                            Language = new CustomLanguage
                            {
                                Culture = string.Empty,
                                EnglishName = subtitle.Key,
                                LocalizedName = string.Empty
                            },
                            Hi = sub.Hi,
                            Rating = sub.Rating,
                            Url = sub.Url
                        });
                    }
                }

                subtitles.Sort();
                movie.AvailableSubtitles = subtitles;
            }
            catch (Exception exception) when (exception is TaskCanceledException)
            {
                Logger.Debug(
                    "LoadSubtitlesAsync cancelled.");
            }
            catch (Exception exception)
            {
                Logger.Error(
                    $"LoadSubtitlesAsync: {exception.Message}");
                throw;
            }
            finally
            {
                watch.Stop();
                var elapsedMs = watch.ElapsedMilliseconds;
                Logger.Debug(
                    $"LoadSubtitlesAsync ({movie.ImdbCode}) in {elapsedMs} milliseconds.");
            }
        }