[core] client factory for SomeRandomApi

This commit is contained in:
Andriy
2023-07-28 23:13:56 +03:00
parent c19696b360
commit 42509dcacd
11 changed files with 62 additions and 18 deletions
@@ -1,28 +1,24 @@
using Newtonsoft.Json;
using Telegram.Bot.Types;
using West.TelegramBot.ContentStore.API;
using West.TelegramBot.ContentStore.Model;
namespace West.TelegramBot.ContentStore.Service.SomeRandomApi;
public abstract class SomeRandomApiAbstractService : IRandomMediaService
{
private readonly ISomeRandomApi _api;
public StoreItem StoreItem { get; }
public string ApiId => StoreItem.Id;
private readonly HttpClient _httpClient;
public SomeRandomApiAbstractService(string id, string name, double price, int order)
protected SomeRandomApiAbstractService(ISomeRandomApi api, string id, string name, double price, int order)
{
StoreItem = new(id, name, price, order);
// TODO: use factory or something, cause we have a lot of http clients in use
_httpClient = new HttpClient();
_api = api;
}
public async Task<RandomMedia> GetRandomMediaAsync()
{
var response = await _httpClient.GetStringAsync($"https://some-random-api.com/animal/{ApiId}");
var image = JsonConvert.DeserializeObject<SomeRandomApiAnimalResponse>(response)!;
var image = await _api.GetAnimalAsync(StoreItem.Id);
return new RandomMedia(new InputFileUrl(image.Image), image.Fact);
}