[quote] Code cleanup.

This commit is contained in:
West14
2022-01-21 14:18:56 +02:00
parent 8ad2d9968e
commit a9d284dc9d
11 changed files with 138 additions and 125 deletions
+10 -5
View File
@@ -4,12 +4,15 @@ using Microsoft.Extensions.Configuration;
using Newtonsoft.Json;
using Telegram.Bot.Types;
using West.TelegramBot.ChatQuotes.Model;
using West.TelegramBot.ChatQuotes.Model.Quote;
using Message = Telegram.Bot.Types.Message;
namespace West.TelegramBot.ChatQuotes;
public class QuoteGeneratorService
{
private readonly HttpClient _httpClient;
private readonly MediaTypeHeaderValue _mediaType;
public QuoteGeneratorService(IConfiguration configuration)
{
@@ -17,22 +20,24 @@ public class QuoteGeneratorService
{
BaseAddress = new Uri(configuration.GetValue<string>("QuoteApiUrl"))
};
_mediaType = MediaTypeHeaderValue.Parse("application/json");
}
public async Task<Stream> GenerateQuoteImage(Message msg)
public async Task<Stream?> GenerateQuoteImage(Message msg)
{
var request = new QuoteRequest(msg);
var request = new Request(msg);
var content = new ByteArrayContent(Encoding.UTF8.GetBytes(request.ToJson().ToCharArray()))
{
Headers =
{
ContentType = MediaTypeHeaderValue.Parse("application/json")
ContentType = _mediaType
}
};
var resp = await _httpClient.PostAsync("/generate", content);
var quoteResponse = JsonConvert.DeserializeObject<QuoteResponse>(await resp.Content.ReadAsStringAsync());
var quoteResponse = JsonConvert.DeserializeObject<Response>(await resp.Content.ReadAsStringAsync());
return new MemoryStream(Convert.FromBase64String(quoteResponse.Result.Image));
return quoteResponse.Ok ? new MemoryStream(Convert.FromBase64String(quoteResponse.Result.Image)) : null;
}
}