Saltar al contenido principal

Inicio rápido de C#/.NET

Integre la API Annie Insights en su aplicación .NET.

Requisitos previos

  • SDK de .NET 6.0+
  • System.Net.Http (integrado) y System.Text.Json (integrado)

Ejemplo completo

using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;

namespace AnnieInsightsDemo
{
class Program
{
private static readonly HttpClient client = new HttpClient();
private const string BaseUrl = "https://api.annie-insights.com/api/v2";

static async Task Main(string[] args)
{
string apiKey = Environment.GetEnvironmentVariable("ANNIE_API_KEY")
?? "YOUR_API_KEY";

client.DefaultRequestHeaders.Add("x-api-key", apiKey);

// 1. Clinical Findings
var result = await AnalyzeClinicalImage("P-001",
"https://example.com/dental-photo.jpg");
Console.WriteLine(JsonSerializer.Serialize(result,
new JsonSerializerOptions { WriteIndented = true }));

// 2. Treatment Length Estimation
var tle = await EstimateTreatmentLength(apiKey);
Console.WriteLine($"Estimated: {tle.GetProperty("estimate_length")} months");
}

static async Task<JsonElement> AnalyzeClinicalImage(
string patientId, string imageUrl)
{
var payload = new
{
PatientID = patientId,
TimeStamp = DateTime.Now.ToString("yyyy-MM-dd"),
ImageName = imageUrl
};

var json = JsonSerializer.Serialize(payload);
var content = new StringContent(json, Encoding.UTF8, "application/json");

var response = await client.PostAsync(
$"{BaseUrl}/clinical-images", content);
response.EnsureSuccessStatusCode();

var responseBody = await response.Content.ReadAsStringAsync();
return JsonSerializer.Deserialize<JsonElement>(responseBody);
}

static async Task<JsonElement> EstimateTreatmentLength(string apiKey)
{
var queryParams = "p01=14&p02=M&p03=ClassII&p04=Moderate" +
"&p05=None&p06=5.2&p07=Deep&p08=None" +
"&p09=None&p10=No&p11=3";

var request = new HttpRequestMessage(HttpMethod.Get,
$"{BaseUrl}/tle?{queryParams}");
request.Headers.Add("x-api-key", apiKey);

var response = await client.SendAsync(request);
response.EnsureSuccessStatusCode();

var body = await response.Content.ReadAsStringAsync();
return JsonSerializer.Deserialize<JsonElement>(body);
}
}
}

Patrón de servicio ASP.NET

public class AnnieInsightsService
{
private readonly HttpClient _client;
private readonly string _baseUrl = "https://api.annie-insights.com/api/v2";

public AnnieInsightsService(HttpClient client, IConfiguration config)
{
_client = client;
_client.DefaultRequestHeaders.Add("x-api-key",
config["AnnieInsights:ApiKey"]);
}

public async Task<JsonElement> AnalyzeAsync(
string endpoint, string patientId, string imageUrl)
{
var payload = new
{
PatientID = patientId,
TimeStamp = DateTime.UtcNow.ToString("yyyy-MM-dd"),
ImageName = imageUrl
};

var response = await _client.PostAsJsonAsync(
$"{_baseUrl}/{endpoint}", payload);
response.EnsureSuccessStatusCode();
return await response.Content
.ReadFromJsonAsync<JsonElement>();
}
}

Regístrate en Program.cs:

builder.Services.AddHttpClient<AnnieInsightsService>();