📄
HackerNewsSearchProvider.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.HackerNews; internal sealed class HackerNewsSearchProvider(HttpClient httpClient) : ISearchProvider { public async IAsyncEnumerable<SearchResult> Search( SearchQuery query, [EnumeratorCancellation] CancellationToken cancellationToken ) { var response = await httpClient.GetFromJsonAsync( $"https://hn.algolia.com/api/v1/search?query={Uri.EscapeDataString(query.Term)}", HackerNewsJsonSerializerContext.Default.HackerNewsSearchResponse, cancellationToken ); if (response is null) { yield break; } foreach (var result in response.Hits) { yield return Map(result); } } private static SearchResult Map(HackerNewsHit hit) => new(hit.Title, Summary: null, new($"https://news.ycombinator.com/item?id={hit.Id}")); }