41 lines
1.2 KiB
C#
41 lines
1.2 KiB
C#
using System.Net.Http.Json;
|
|
using System.Text.Json;
|
|
using AnchorCli.OpenRouter;
|
|
|
|
namespace AnchorCli.Providers;
|
|
|
|
/// <summary>
|
|
/// Pricing provider for OpenRouter API.
|
|
/// </summary>
|
|
internal sealed class OpenRouterProvider : IPricingProvider
|
|
{
|
|
private const string ModelsUrl = "https://openrouter.ai/api/v1/models";
|
|
private static readonly HttpClient Http = new();
|
|
private Dictionary<string, ModelInfo>? _models;
|
|
|
|
static OpenRouterProvider()
|
|
{
|
|
OpenRouterHeaders.ApplyTo(Http);
|
|
}
|
|
|
|
public async Task<Dictionary<string, ModelInfo>> GetAllModelsAsync(CancellationToken ct = default)
|
|
{
|
|
if (_models != null) return _models;
|
|
|
|
var response = await Http.GetAsync(ModelsUrl, ct);
|
|
response.EnsureSuccessStatusCode();
|
|
|
|
var json = await response.Content.ReadAsStringAsync(ct);
|
|
var result = JsonSerializer.Deserialize(json, AppJsonContext.Default.ModelsResponse);
|
|
|
|
_models = result?.Data?.ToDictionary(m => m.Id) ?? [];
|
|
return _models;
|
|
}
|
|
|
|
public async Task<ModelInfo?> GetModelInfoAsync(string modelId, CancellationToken ct = default)
|
|
{
|
|
var models = await GetAllModelsAsync(ct);
|
|
return models.GetValueOrDefault(modelId);
|
|
}
|
|
}
|