Files
2024-05-30 15:40:13 +03:00

34 lines
1.2 KiB
C#

using System.Text.Json;
using Application.Endpoints;
using Application.Endpoints.SicknessPrediction;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace API.Controllers;
[Route("api/[controller]")]
public class SicknessPredictionController(IHttpClientFactory clientFactory, IConfiguration configuration)
: ControllerBase
{
[Authorize(Policy = Policies.DoctorPolicy)]
[HttpPost]
public async Task<IActionResult> GetPrediction(SicknessPredictionCommand command)
{
var client = clientFactory.CreateClient();
var apiKey = configuration["SicknessPredictionService:ApiKey"];
var baseUrl = configuration["SicknessPredictionService:BaseUrl"];
client.DefaultRequestHeaders.Add("x-api-key", apiKey);
var response = await client.PostAsJsonAsync($"{baseUrl}/predict", command);
if (response.IsSuccessStatusCode)
{
var responseContent = await response.Content.ReadAsStringAsync();
var baseResponse = JsonSerializer.Deserialize<BaseResponse>(responseContent);
return Ok(baseResponse);
}
return StatusCode((int)response.StatusCode, "Failed to retrieve data from the prediction service");
}
}