SDK de C#Vista previa

Cliente tipado para .NET 8 sobre HttpClient.

El paquete está en vista previa. Mientras tanto, este cliente cubre lo esencial y se registra con IHttpClientFactory.

Terminal
dotnet add package Tinkay.Sdk --prerelease

Registro

Program.cs
builder.Services.AddHttpClient<TinkayClient>(client =>{    client.BaseAddress = new Uri("https://api.tinkay.app/v1/");    client.DefaultRequestHeaders.Authorization =        new AuthenticationHeaderValue("Bearer", builder.Configuration["Tinkay:ApiKey"]);    client.Timeout = TimeSpan.FromSeconds(15);});

Cliente

TinkayClient.cs
using System.Net.Http.Json;public sealed class TinkayClient(HttpClient http){    public async Task<Contact> CreateContactAsync(CreateContact input, CancellationToken ct = default)    {        var res = await http.PostAsJsonAsync("contacts", input, ct);        res.EnsureSuccessStatusCode();        var body = await res.Content.ReadFromJsonAsync<Envelope<Contact>>(cancellationToken: ct);        return body!.Data;    }    public async IAsyncEnumerable<Contact> ListContactsAsync([EnumeratorCancellation] CancellationToken ct = default)    {        string? cursor = null;        do        {            var url = cursor is null ? "contacts?limit=100" : $"contacts?limit=100&cursor={cursor}";            var page = await http.GetFromJsonAsync<Paged<Contact>>(url, ct);            foreach (var item in page!.Data) yield return item;            cursor = page.NextCursor;        } while (cursor is not null && !ct.IsCancellationRequested);    }    public async Task<Ticket> CreateTicketAsync(CreateTicket input, CancellationToken ct = default)    {        var res = await http.PostAsJsonAsync("tickets", input, ct);        res.EnsureSuccessStatusCode();        var body = await res.Content.ReadFromJsonAsync<Envelope<Ticket>>(cancellationToken: ct);        return body!.Data;    }}public record Envelope<T>(T Data);public record Paged<T>(IReadOnlyList<T> Data, [property: JsonPropertyName("next_cursor")] string? NextCursor, [property: JsonPropertyName("has_more")] bool HasMore);public record CreateContact(string Name, string Email, string? Company = null);public record CreateTicket(string Subject, string Description, [property: JsonPropertyName("contact_id")] string ContactId, string Priority = "normal");

Uso

C#
public class SupportService(TinkayClient tinkay){    public async Task ReportIncidentAsync(User user, Incident incident)    {        var contact = await tinkay.CreateContactAsync(new(user.FullName, user.Email, user.Company));        await tinkay.CreateTicketAsync(new(            Subject: $"Error {incident.Code} en {incident.Module}",            Description: incident.Summary,            ContactId: contact.Id,            Priority: incident.IsBlocking ? "urgent" : "normal"));    }}