Skip to content

Commit

Permalink
Merge pull request #62 from Lan2Play/feature/httpclient
Browse files Browse the repository at this point in the history
Add HttpClient and MatchFactory
  • Loading branch information
TheR00st3r authored Nov 19, 2023
2 parents 15dfea4 + 11d7e90 commit ae27f3c
Show file tree
Hide file tree
Showing 11 changed files with 123 additions and 168 deletions.
54 changes: 5 additions & 49 deletions PugSharp.Api.G5Api/G5ApiClient.cs
Original file line number Diff line number Diff line change
@@ -1,27 +1,22 @@
using Microsoft.Extensions.Logging;
using Polly;
using Polly.Extensions.Http;
using System.Text;
using System.Text.Json;

namespace PugSharp.Api.G5Api;

public sealed class G5ApiClient : IDisposable
public sealed class G5ApiClient
{
private const int _RetryCount = 3;
private const int _RetryDelayFactor = 2;
private readonly ILogger<G5ApiClient> _Logger;

private readonly HttpClient _HttpClient = new();
private IAsyncPolicy<HttpResponseMessage>? _RetryPolicy;
private readonly HttpClient _HttpClient;

private string? _ApiUrl;
private string? _ApiHeader;
private string? _ApiHeadeValue;
private bool _DisposedValue;

public G5ApiClient(ILogger<G5ApiClient> logger)
public G5ApiClient(HttpClient httpClient,ILogger<G5ApiClient> logger)
{
_HttpClient = httpClient;
_Logger = logger;
}

Expand All @@ -31,20 +26,6 @@ public void Initialize(string g5ApiUrl, string g5ApiHeader, string g5ApiHeaderVa
_ApiUrl = g5ApiUrl;
_ApiHeader = g5ApiHeader;
_ApiHeadeValue = g5ApiHeaderValue;

if (string.IsNullOrEmpty(g5ApiUrl))
{
return;
}

_RetryPolicy = HttpPolicyExtensions
.HandleTransientHttpError()
.WaitAndRetryAsync(_RetryCount,
retryAttempt => TimeSpan.FromSeconds(Math.Pow(_RetryDelayFactor, retryAttempt)),
onRetry: (response, calculatedWaitDuration) =>
{
_Logger.LogError(response.Exception, "G5Api failed attempt. Waited for {CalculatedWaitDuration}. Retrying.", calculatedWaitDuration);
});
}

public void UpdateConfig(string g5ApiUrl, string g5ApiHeader, string g5ApiHeaderValue)
Expand All @@ -56,11 +37,6 @@ public void UpdateConfig(string g5ApiUrl, string g5ApiHeader, string g5ApiHeader

public async Task SendEventAsync(EventBase eventToSend, CancellationToken cancellationToken)
{
if (_HttpClient == null || _RetryPolicy == null)
{
return;
}

try
{
using var jsonContent = new StringContent(
Expand All @@ -78,8 +54,7 @@ public async Task SendEventAsync(EventBase eventToSend, CancellationToken cancel
httpRequest.Headers.Add(_ApiHeader, _ApiHeadeValue);
}

using var httpResponseMessage = await _RetryPolicy.ExecuteAsync(
() => _HttpClient.SendAsync(httpRequest, cancellationToken)).ConfigureAwait(false);
using var httpResponseMessage = await _HttpClient.SendAsync(httpRequest, cancellationToken).ConfigureAwait(false);

if (httpResponseMessage == null)
{
Expand All @@ -100,23 +75,4 @@ public async Task SendEventAsync(EventBase eventToSend, CancellationToken cancel
_Logger.LogError(ex, "Error sending event to G5 API. EventName {EventName}", eventToSend.EventName);
}
}

private void Dispose(bool disposing)
{
if (!_DisposedValue)
{
if (disposing)
{
_HttpClient?.Dispose();
}

_DisposedValue = true;
}
}

public void Dispose()
{
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
Dispose(disposing: true);
}
}
12 changes: 1 addition & 11 deletions PugSharp.ApiStats/ApiStats.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class ApiStats : BaseApi, IApiProvider
{
private readonly ILogger<ApiStats> _Logger;

public ApiStats(ILogger<ApiStats> logger) : base(logger)
public ApiStats(HttpClient httpClient, ILogger<ApiStats> logger) : base(httpClient, logger)
{
_Logger = logger;
}
Expand All @@ -21,11 +21,6 @@ public void Initialize(string? apiStatsUrl, string? apiStatsKey)

public async Task GoingLiveAsync(GoingLiveParams goingLiveParams, CancellationToken cancellationToken)
{
if (HttpClient == null)
{
return;
}

try
{
var queryParams = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
Expand Down Expand Up @@ -192,11 +187,6 @@ public async Task FinalizeAsync(SeriesResultParams seriesResultParams, Cancellat
return;
}

if (HttpClient == null)
{
return;
}

var queryParams = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
{
{ApiStatsConstants.StatsSeriesWinner, seriesResultParams.WinnerTeamName},
Expand Down
35 changes: 6 additions & 29 deletions PugSharp.ApiStats/BaseApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@

namespace PugSharp.ApiStats;

public class BaseApi : IDisposable
public class BaseApi
{
private readonly ILogger<BaseApi> _Logger;

private bool _DisposedValue;
protected readonly HttpClient HttpClient;

protected HttpClient? HttpClient { get; private set; }

protected BaseApi(ILogger<BaseApi> logger)
protected BaseApi(HttpClient httpClient, ILogger<BaseApi> logger)
{
HttpClient = httpClient;
_Logger = logger;
}

Expand All @@ -31,10 +30,8 @@ protected void InitializeBase(string? baseUrl, string? authKey)
}

_Logger.LogInformation("Using BaseURL : \"{url}\" and authKey \"{authKey}\"", baseUrl, authKey);
HttpClient = new HttpClient()
{
BaseAddress = new Uri(baseUrl),
};

HttpClient.BaseAddress = new Uri(baseUrl);

if (!string.IsNullOrEmpty(authKey))
{
Expand Down Expand Up @@ -78,24 +75,4 @@ protected async Task HandleResponseAsync(HttpResponseMessage? httpResponseMessag
_Logger.LogError(e, "Error handling response");
}
}

protected virtual void Dispose(bool disposing)
{
if (!_DisposedValue)
{
if (disposing)
{
HttpClient?.Dispose();
}

_DisposedValue = true;
}
}

public void Dispose()
{
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
}
2 changes: 1 addition & 1 deletion PugSharp.ApiStats/DemoUploader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public class DemoUploader : BaseApi
{
private readonly ILogger<DemoUploader> _Logger;

public DemoUploader(ILogger<DemoUploader> logger) : base(logger)
public DemoUploader(HttpClient httpClient, ILogger<DemoUploader> logger) : base(httpClient, logger)
{
_Logger = logger;
}
Expand Down
34 changes: 4 additions & 30 deletions PugSharp.Config/ConfigProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,16 @@

namespace PugSharp.Config;

public class ConfigProvider : IDisposable
public class ConfigProvider
{
private readonly HttpClient _HttpClient;
private readonly ILogger<ConfigProvider> _Logger;

private readonly HttpClient _HttpClient = new();
private string _ConfigDirectory = string.Empty;
private bool _DisposedValue;


public ConfigProvider(ILogger<ConfigProvider> logger)
public ConfigProvider(HttpClient httpClient, ILogger<ConfigProvider> logger)
{
_HttpClient = httpClient;
_Logger = logger;
}

Expand Down Expand Up @@ -68,7 +67,6 @@ public async Task<OneOf<Error<string>, MatchConfig>> LoadMatchConfigFromUrlAsync
RequestUri = new Uri(url),
};


if (!string.IsNullOrEmpty(authToken))
{
httpRequestMessage.Headers.Add(HeaderNames.Authorization, authToken);
Expand Down Expand Up @@ -115,28 +113,4 @@ public async Task<OneOf<Error<string>, MatchConfig>> LoadMatchConfigFromUrlAsync
return new Error<string>($"Failed loading config from {url}.");
}
}

#region IDisposable

protected virtual void Dispose(bool disposing)
{
if (!_DisposedValue)
{
if (disposing)
{
_HttpClient.Dispose();
}

_DisposedValue = true;
}
}

public void Dispose()
{
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
Dispose(disposing: true);
GC.SuppressFinalize(this);
}

#endregion
}
29 changes: 11 additions & 18 deletions PugSharp.Match.Tests/MatchTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
using Microsoft.Extensions.Logging;
using NSubstitute;
using PugSharp.Api.Contract;
using PugSharp.Api.Json;
using PugSharp.ApiStats;
using PugSharp.Config;
using PugSharp.Match.Contract;
using PugSharp.Server.Contract;
Expand All @@ -28,13 +26,8 @@ private static IServiceProvider CreateTestProvider()
services.AddSingleton<IApplication, Application>();

services.AddSingleton<ConfigProvider>();
services.AddTransient<Match>();

services.AddSingleton<G5ApiProvider>();
services.AddSingleton<ApiStats.ApiStats>();
services.AddSingleton<JsonApiProvider>();
services.AddTransient<MatchFactory>();
services.AddSingleton(Substitute.For<ITextHelper>());
services.AddSingleton<DemoUploader>();

// Build service provider
return services.BuildServiceProvider();
Expand All @@ -46,8 +39,8 @@ public void CreateDotGraphTest()
MatchConfig config = CreateExampleConfig();

var serviceProvider = CreateTestProvider();
var match = serviceProvider.GetRequiredService<Match>();
match.Initialize(config);
var matchFactory = serviceProvider.GetRequiredService<MatchFactory>();
var match = matchFactory.CreateMatch(config);

var dotGraphString = match.CreateDotGraph();
Assert.True(!string.IsNullOrEmpty(dotGraphString));
Expand All @@ -59,8 +52,8 @@ public void WrongPlayerConnectTest()
MatchConfig config = CreateExampleConfig();

var serviceProvider = CreateTestProvider();
var match = serviceProvider.GetRequiredService<Match>();
match.Initialize(config);
var matchFactory = serviceProvider.GetRequiredService<MatchFactory>();
var match = matchFactory.CreateMatch(config);

Assert.Equal(MatchState.WaitingForPlayersConnectedReady, match.CurrentState);

Expand All @@ -75,8 +68,8 @@ public void CorrectPlayerConnectTest()
MatchConfig config = CreateExampleConfig();

var serviceProvider = CreateTestProvider();
var match = serviceProvider.GetRequiredService<Match>();
match.Initialize(config);
var matchFactory = serviceProvider.GetRequiredService<MatchFactory>();
var match = matchFactory.CreateMatch(config);

Assert.Equal(MatchState.WaitingForPlayersConnectedReady, match.CurrentState);

Expand All @@ -96,8 +89,8 @@ public async Task MatchTest()
var csServer = serviceProvider.GetRequiredService<ICsServer>();
csServer.LoadAllPlayers().Returns(matchPlayers);

var match = serviceProvider.GetRequiredService<Match>();
match.Initialize(config);
var matchFactory = serviceProvider.GetRequiredService<MatchFactory>();
var match = matchFactory.CreateMatch(config);

Assert.Equal(MatchState.WaitingForPlayersConnectedReady, match.CurrentState);

Expand All @@ -121,8 +114,8 @@ public async Task MatchTestWithOneMap()
var csServer = serviceProvider.GetRequiredService<ICsServer>();
csServer.LoadAllPlayers().Returns(matchPlayers);

var match = serviceProvider.GetRequiredService<Match>();
match.Initialize(config);
var matchFactory = serviceProvider.GetRequiredService<MatchFactory>();
var match = matchFactory.CreateMatch(config);

Assert.Equal(MatchState.WaitingForPlayersConnectedReady, match.CurrentState);

Expand Down
Loading

0 comments on commit ae27f3c

Please sign in to comment.