📄 MozillaDeveloperNetworkSearchProvider.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.MozillaDeveloperNetwork;

internal sealed class MozillaDeveloperNetworkSearchProvider(HttpClient httpClient) : ISearchProvider
{
    public async IAsyncEnumerable<SearchResult> Search(
        SearchQuery query,
        [EnumeratorCancellation] CancellationToken cancellationToken
    )
    {
        var response = await httpClient.GetFromJsonAsync(
            $"https://developer.mozilla.org/api/v1/search?q={Uri.EscapeDataString(query.Term)}",
            MozillaDeveloperNetworkJsonSerializerContext.Default.MozillaDeveloperNetworkSearchResponse,
            cancellationToken
        );
        if (response is null)
        {
            yield break;
        }
        foreach (var result in response.Documents)
        {
            yield return Map(result);
        }
    }

    private static SearchResult Map(MozillaDeveloperNetworkSearchResult result) =>
        new(result.Title, result.Title, new(result.Url));
}