feat: implement generate command in console app

This commit is contained in:
Stevan Freeborn
2025-02-12 16:10:49 -06:00
parent 8e0a6933f7
commit ec9651e5e9
12 changed files with 296 additions and 161 deletions
@@ -0,0 +1,44 @@
namespace AltGen.Console.Generate;
sealed class AltGenService(HttpClient client) : IAltGenService
{
static readonly JsonSerializerOptions JsonOptions = new()
{
PropertyNameCaseInsensitive = true
};
readonly HttpClient _client = client;
public async Task<string> GenerateAltTextAsync(GenerateAltTextRequest req)
{
var byteContent = new ByteArrayContent(req.Image);
byteContent.Headers.ContentType = new MediaTypeHeaderValue(req.ContentType);
var request = new HttpRequestMessage(HttpMethod.Post, $"{Constants.AltGenApiUri}/generate")
{
Content = new MultipartFormDataContent
{
{ new StringContent(req.Provider), "provider" },
{ new StringContent(req.ProviderKey), "providerKey" },
{ new ByteArrayContent(req.Image), "file", req.FileName }
}
};
var response = await _client.SendAsync(request);
var content = await response.Content.ReadAsStringAsync();
if (response.IsSuccessStatusCode is false)
{
throw new AltTextException("Failed to generate alt text.");
}
var altTextResponse = JsonSerializer.Deserialize<AltTextResponse>(content, JsonOptions) ?? throw new AltTextException("Failed to deserialize response.");
return altTextResponse.AltText;
}
}
record AltTextResponse(string AltText);
class AltTextException(string message) : Exception(message)
{
}