-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWorldOfTanksBlitzClient.cs
75 lines (64 loc) · 2.59 KB
/
WorldOfTanksBlitzClient.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#nullable enable
using System;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using WargamingApi;
using WargamingApi.WorldOfTanksBlitz.Services;
using WargamingApi.WorldOfTanksBlitz.Types;
using WargamingApi.WorldOfTanksBlitz.Types.Enums;
using Extensions = WargamingApi.Types.Extensions;
namespace WargamingApi.WorldOfTanksBlitz
{
public sealed class WorldOfTanksBlitzClient
{
private readonly string m_requestForm;
public WorldOfTanksBlitzClient(WargamingApiClient client)
{
/*
* 0 - Region
* 1 - Section
* 2 - Type
* 3 - Parameters
*/
if (string.IsNullOrEmpty(client.ApplicationId) || string.IsNullOrWhiteSpace(client.ApplicationId))
throw new NullReferenceException(
$"Application id can not be null, specify it in {nameof(WargamingApiClient.ApplicationId)} parameter");
m_requestForm =
@"https://api.wotblitz.{0}/wotb/{1}/{2}/?application_id="
+ client.ApplicationId
+ "{3}";
}
public ServiceProvider Services { get; private set; } = null!;
public ServiceProvider InitServices(Service services)
{
if (services == Service.None)
return Services;
var svc = new ServiceCollection().AddSingleton(this);
if (services.HasService(Service.Accounts))
svc = svc.AddSingleton<Accounts>();
if (services.HasService(Service.Clans))
svc = svc.AddSingleton<Clans>();
if (services.HasService(Service.Encyclopedia))
svc = svc.AddSingleton<Encyclopedia>();
if (services.HasService(Service.PlayerVehicles))
svc = svc.AddSingleton<PlayerVehicles>();
if (services.HasService(Service.Tournaments))
svc = svc.AddSingleton<Tournaments>();
Services = svc.BuildServiceProvider();
return Services;
}
internal async Task<T> GetRequest<T>(RequestArguments requestArguments)
{
var uri = new Uri(
string.Format(
m_requestForm,
Extensions.EnumToString(requestArguments.Region).ToLower(),
requestArguments.Section.EnumToString().ToLower(),
requestArguments.Type.EnumToString().ToLower(),
requestArguments.Parameters
)
);
return await WargamingApiClient.GetRequest<T>(uri);
}
}
}