mirror of
https://github.com/Bubuni-Team/telegram-bot.git
synced 2026-07-31 00:29:24 +03:00
[core] client factory for SomeRandomApi
This commit is contained in:
@@ -0,0 +1,8 @@
|
|||||||
|
using West.TelegramBot.ContentStore.Model;
|
||||||
|
|
||||||
|
namespace West.TelegramBot.ContentStore.API;
|
||||||
|
|
||||||
|
public interface ISomeRandomApi
|
||||||
|
{
|
||||||
|
public Task<SomeRandomApiAnimalResponse> GetAnimalAsync(string animalId);
|
||||||
|
}
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
using System.Net.Http.Json;
|
||||||
|
using West.TelegramBot.ContentStore.Model;
|
||||||
|
|
||||||
|
namespace West.TelegramBot.ContentStore.API;
|
||||||
|
|
||||||
|
public class SomeRandomApi : ISomeRandomApi
|
||||||
|
{
|
||||||
|
private readonly HttpClient _client;
|
||||||
|
|
||||||
|
public SomeRandomApi(HttpClient client)
|
||||||
|
{
|
||||||
|
_client = client;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<SomeRandomApiAnimalResponse> GetAnimalAsync(string animalId)
|
||||||
|
{
|
||||||
|
return (await _client.GetFromJsonAsync<SomeRandomApiAnimalResponse>(
|
||||||
|
$"animal/{animalId}"
|
||||||
|
))!;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -39,6 +39,11 @@ public class ContentStore : Module
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
services.AddHttpClient<ISomeRandomApi, SomeRandomApi>(c =>
|
||||||
|
{
|
||||||
|
c.BaseAddress = new Uri("https://some-random-api.com");
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
foreach (var serviceType in MediaServiceTypes)
|
foreach (var serviceType in MediaServiceTypes)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,7 +1,9 @@
|
|||||||
|
using West.TelegramBot.ContentStore.API;
|
||||||
|
|
||||||
namespace West.TelegramBot.ContentStore.Service.SomeRandomApi;
|
namespace West.TelegramBot.ContentStore.Service.SomeRandomApi;
|
||||||
|
|
||||||
public class BirdService : SomeRandomApiAbstractService
|
public class BirdService : SomeRandomApiAbstractService
|
||||||
{
|
{
|
||||||
public BirdService() : base("bird", "Птица", 40, 165)
|
public BirdService(ISomeRandomApi api) : base(api, "bird", "Птица", 40, 165)
|
||||||
{ }
|
{ }
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,9 @@
|
|||||||
|
using West.TelegramBot.ContentStore.API;
|
||||||
|
|
||||||
namespace West.TelegramBot.ContentStore.Service.SomeRandomApi;
|
namespace West.TelegramBot.ContentStore.Service.SomeRandomApi;
|
||||||
|
|
||||||
public class FoxService : SomeRandomApiAbstractService
|
public class FoxService : SomeRandomApiAbstractService
|
||||||
{
|
{
|
||||||
public FoxService() : base("fox", "Лисица", 40, 170)
|
public FoxService(ISomeRandomApi api) : base(api, "fox", "Лисица", 40, 170)
|
||||||
{ }
|
{ }
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,9 @@
|
|||||||
|
using West.TelegramBot.ContentStore.API;
|
||||||
|
|
||||||
namespace West.TelegramBot.ContentStore.Service.SomeRandomApi;
|
namespace West.TelegramBot.ContentStore.Service.SomeRandomApi;
|
||||||
|
|
||||||
public class KangarooService : SomeRandomApiAbstractService
|
public class KangarooService : SomeRandomApiAbstractService
|
||||||
{
|
{
|
||||||
public KangarooService() : base("kangaroo", "Кенгуру", 40, 175)
|
public KangarooService(ISomeRandomApi api) : base(api, "kangaroo", "Кенгуру", 40, 175)
|
||||||
{ }
|
{ }
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,9 @@
|
|||||||
|
using West.TelegramBot.ContentStore.API;
|
||||||
|
|
||||||
namespace West.TelegramBot.ContentStore.Service.SomeRandomApi;
|
namespace West.TelegramBot.ContentStore.Service.SomeRandomApi;
|
||||||
|
|
||||||
public class KoalaService : SomeRandomApiAbstractService
|
public class KoalaService : SomeRandomApiAbstractService
|
||||||
{
|
{
|
||||||
public KoalaService() : base("koala", "Коала", 40, 180)
|
public KoalaService(ISomeRandomApi api) : base(api, "koala", "Коала", 40, 180)
|
||||||
{ }
|
{ }
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,9 @@
|
|||||||
|
using West.TelegramBot.ContentStore.API;
|
||||||
|
|
||||||
namespace West.TelegramBot.ContentStore.Service.SomeRandomApi;
|
namespace West.TelegramBot.ContentStore.Service.SomeRandomApi;
|
||||||
|
|
||||||
public class PandaService : SomeRandomApiAbstractService
|
public class PandaService : SomeRandomApiAbstractService
|
||||||
{
|
{
|
||||||
public PandaService() : base("panda", "Панда", 40, 160)
|
public PandaService(ISomeRandomApi api) : base(api, "panda", "Панда", 40, 160)
|
||||||
{ }
|
{ }
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,9 @@
|
|||||||
|
using West.TelegramBot.ContentStore.API;
|
||||||
|
|
||||||
namespace West.TelegramBot.ContentStore.Service.SomeRandomApi;
|
namespace West.TelegramBot.ContentStore.Service.SomeRandomApi;
|
||||||
|
|
||||||
public class RaccoonService : SomeRandomApiAbstractService
|
public class RaccoonService : SomeRandomApiAbstractService
|
||||||
{
|
{
|
||||||
public RaccoonService() : base("raccoon", "Енот", 40, 160)
|
public RaccoonService(ISomeRandomApi api) : base(api, "raccoon", "Енот", 40, 160)
|
||||||
{ }
|
{ }
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,9 @@
|
|||||||
namespace West.TelegramBot.ContentStore.Service.SomeRandomApi;
|
using West.TelegramBot.ContentStore.API;
|
||||||
|
|
||||||
|
namespace West.TelegramBot.ContentStore.Service.SomeRandomApi;
|
||||||
|
|
||||||
public class RedPandaService : SomeRandomApiAbstractService
|
public class RedPandaService : SomeRandomApiAbstractService
|
||||||
{
|
{
|
||||||
public RedPandaService() : base("red_panda", "Красная панда", 40, 150)
|
public RedPandaService(ISomeRandomApi api) : base(api, "red_panda", "Красная панда", 40, 150)
|
||||||
{ }
|
{ }
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,28 +1,24 @@
|
|||||||
using Newtonsoft.Json;
|
|
||||||
using Telegram.Bot.Types;
|
using Telegram.Bot.Types;
|
||||||
|
using West.TelegramBot.ContentStore.API;
|
||||||
using West.TelegramBot.ContentStore.Model;
|
using West.TelegramBot.ContentStore.Model;
|
||||||
|
|
||||||
namespace West.TelegramBot.ContentStore.Service.SomeRandomApi;
|
namespace West.TelegramBot.ContentStore.Service.SomeRandomApi;
|
||||||
|
|
||||||
public abstract class SomeRandomApiAbstractService : IRandomMediaService
|
public abstract class SomeRandomApiAbstractService : IRandomMediaService
|
||||||
{
|
{
|
||||||
|
private readonly ISomeRandomApi _api;
|
||||||
|
|
||||||
public StoreItem StoreItem { get; }
|
public StoreItem StoreItem { get; }
|
||||||
public string ApiId => StoreItem.Id;
|
|
||||||
|
|
||||||
private readonly HttpClient _httpClient;
|
protected SomeRandomApiAbstractService(ISomeRandomApi api, string id, string name, double price, int order)
|
||||||
|
|
||||||
public SomeRandomApiAbstractService(string id, string name, double price, int order)
|
|
||||||
{
|
{
|
||||||
StoreItem = new(id, name, price, order);
|
StoreItem = new(id, name, price, order);
|
||||||
|
_api = api;
|
||||||
// TODO: use factory or something, cause we have a lot of http clients in use
|
|
||||||
_httpClient = new HttpClient();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<RandomMedia> GetRandomMediaAsync()
|
public async Task<RandomMedia> GetRandomMediaAsync()
|
||||||
{
|
{
|
||||||
var response = await _httpClient.GetStringAsync($"https://some-random-api.com/animal/{ApiId}");
|
var image = await _api.GetAnimalAsync(StoreItem.Id);
|
||||||
var image = JsonConvert.DeserializeObject<SomeRandomApiAnimalResponse>(response)!;
|
|
||||||
|
|
||||||
return new RandomMedia(new InputFileUrl(image.Image), image.Fact);
|
return new RandomMedia(new InputFileUrl(image.Image), image.Fact);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user