Code/API/TablebaseAPI.cs

Client API method to query Lichess tablebase service. It validates supported variants, remaps Chess960 to Standard, builds a URL to tablebase.lichess.ovh, performs an HTTP GET via Sandbox.Http.RequestStringAsync, and deserializes the JSON into a TablebaseLookup.

NetworkingHttp Calls
🌐 https://tablebase.lichess.ovh/{variant.GetApiName()}?fen={Uri.EscapeDataString(fen)}
#nullable enable annotations

using LichessNET.Entities.Analysis;
using LichessNET.Entities.Enumerations;
using LichessNET.Extensions;

namespace LichessNET.API;

public partial class LichessApiClient
{
    /// <summary>
    /// Checks if a position is avaliable in the tablebases on lichess.
    /// </summary>
    /// <param name="fen">The position to check</param>
    /// <param name="variant">The variant for which a position should be looked up. Currently supported are
    /// Standard, Chess960, Atomic and Antichess
    /// </param>
    /// <returns>A TablebaseLookup object</returns>
    /// <exception cref="ArgumentException">Thrown when an invalid variant was requested</exception>
    public Task<TablebaseLookup> GetTablebaseLookupAsync(string fen,
        ChessVariant variant = ChessVariant.Standard)
    {
        return GetTablebaseLookupAsync(fen, variant, CancellationToken.None);
    }

    public async Task<TablebaseLookup> GetTablebaseLookupAsync(string fen, ChessVariant variant,
        CancellationToken cancellationToken)
    {
        if (variant != ChessVariant.Standard && variant != ChessVariant.Chess960 &&
            variant != ChessVariant.Atomic && variant != ChessVariant.Antichess)
        {
            throw new ArgumentException(
                "Tablebase lookups are only supported for standard, Chess960, Atomic and Antichess");
        }

        //Beacuse the API does not support Chess960, we need to convert it to standard, as the moves are just the same as in normal chess.
        if (variant == ChessVariant.Chess960) variant = ChessVariant.Standard;


        var url = $"https://tablebase.lichess.ovh/{variant.GetApiName()}?fen={Uri.EscapeDataString(fen)}";
        var jsonResponse = await Sandbox.Http.RequestStringAsync(url, "GET", null, null,
            cancellationToken);
        return LichessJson.Deserialize<TablebaseLookup>(jsonResponse);
    }
}