📄
TheMovieDbSearchProvider.cs
using System; using System.Collections.Generic; using System.Net.Http; using System.Net.Http.Json; using System.Runtime.CompilerServices; using System.Threading; using MSearch.Domain; namespace MSearch.SearchProviders.TheMovieDb; internal sealed class TheMovieDbSearchProvider(HttpClient httpClient) : ISearchProvider { public async IAsyncEnumerable<SearchResult> Search( SearchQuery query, [EnumeratorCancellation] CancellationToken cancellationToken ) { var response = await httpClient.GetFromJsonAsync( $"3/search/multi?query={Uri.EscapeDataString(query.Term)}", TheMovieDbJsonSerializerContext.Default.TheMovieDbResponse, cancellationToken ); if (response is null) { yield break; } foreach (var result in response.Results) { yield return Map(result); } } private static SearchResult Map(TheMovieDbResult result) => new(result.Title, result.Overview, new($"https://www.themoviedb.org/{result.MediaType}/{result.Id}")); }