Skip to content

Commit

Permalink
Merge branch 'gateway'
Browse files Browse the repository at this point in the history
  • Loading branch information
dlidstrom committed Dec 29, 2024
2 parents 1d40e3b + 218657a commit a2a73c6
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 49 deletions.
9 changes: 7 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ permissions: write-all
jobs:
build-windows:
runs-on: windows-2022

concurrency:
group: ${{ github.workflow }}-build
cancel-in-progress: true

steps:
- uses: actions/checkout@v2

Expand All @@ -33,11 +38,11 @@ jobs:
echo ("BUILD_VERSION=$v") >> $env:GITHUB_ENV
echo $v
- uses: actions/cache@v2
- uses: actions/cache@v4
id: cache
with:
path: packages
key: ${{ runner.os }}-nuget-${{ hashFiles('**/packages.lock.json') }}
key: ${{ runner.os }}-nuget-${{ hashFiles('**/*.csproj') }}
restore-keys: |
${{ runner.os }}-nuget-
Expand Down
4 changes: 2 additions & 2 deletions Snittlistan.Queue/LoggingHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ public class LoggingHandler : DelegatingHandler
{
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();

public LoggingHandler(HttpMessageHandler innerHandler)
: base(innerHandler)
public LoggingHandler()
: base(new HttpClientHandler())
{
}

Expand Down
2 changes: 1 addition & 1 deletion Snittlistan.Queue/TaskQueueListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Snittlistan.Queue;

public class TaskQueueListener : MessageQueueListenerBase
{
private readonly HttpClient client = new(new LoggingHandler(new HttpClientHandler()))
private readonly HttpClient client = new(new LoggingHandler())
{
Timeout = TimeSpan.FromSeconds(600)
};
Expand Down
6 changes: 4 additions & 2 deletions Snittlistan.Test/BitsGateway.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ namespace Snittlistan.Test;
public static class BitsGateway
{
private static readonly IBitsClient Client = new BitsClient(
Environment.GetEnvironmentVariable("ApiKey"),
new HttpClient(),
new HttpClient()
{
BaseAddress = new Uri(Environment.GetEnvironmentVariable("GatewayUrl"))
},
MemoryCache.Default);

public static async Task<HeadInfo> GetHeadInfo(int matchId)
Expand Down
2 changes: 1 addition & 1 deletion Snittlistan.Tool/Tasks/CommandLineTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Snittlistan.Tool.Tasks;

public abstract class CommandLineTask : ICommandLineTask
{
private readonly HttpClient client = new(new LoggingHandler(new HttpClientHandler()))
private readonly HttpClient client = new(new LoggingHandler())
{
Timeout = TimeSpan.FromSeconds(600)
};
Expand Down
22 changes: 16 additions & 6 deletions Snittlistan.Web/Global.asax.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,15 @@ protected void Application_BeginRequest()
{
Response.Clear();
Response.Status = "301 Moved Permanently";
Response.AddHeader("Location", Context.Request.Url.ToString().Insert(4, "s"));
try
{
Response.AddHeader("Location", Context.Request.Url.ToString().Insert(4, "s"));
}
catch (Exception ex)
{
throw new Exception($"Failed to redirect to {Context.Request.Url}", ex);
}

Response.End();
}
}
Expand Down Expand Up @@ -233,17 +241,19 @@ private static void InitializeContainer(
}

Container.Kernel.AddHandlerSelector(new HostBasedComponentSelector());
string apiKey = Environment.GetEnvironmentVariable("ApiKey");
Log.Info($"ApiKey: {apiKey}");
string gatewayUrl = Environment.GetEnvironmentVariable("GatewayUrl");
Log.Info($"gatewayUrl: {gatewayUrl}");
HttpClient httpClient = new(
new RateHandler(rate: 1.0, per: 1.0, maxTries: 60,
new LoggingHandler(
new HttpClientHandler())));
new LoggingHandler()))
{
BaseAddress = new Uri(gatewayUrl)
};
_ = Container
.AddFacility<LoggingFacility>(f => f.LogUsing<NLogFactory>())
.Install(
new ApiControllerInstaller(),
new BitsClientInstaller(apiKey, httpClient),
new BitsClientInstaller(httpClient),
CommandHandlerInstaller.PerWebRequest(),
new ControllerInstaller(),
new DatabaseContextInstaller(databasesFactory),
Expand Down
30 changes: 9 additions & 21 deletions Snittlistan.Web/Infrastructure/Bits/BitsClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,60 +9,49 @@

namespace Snittlistan.Web.Infrastructure.Bits;

public class BitsClient : IBitsClient
public class BitsClient(HttpClient client, MemoryCache memoryCache) : IBitsClient
{
private readonly string apiKey;
private readonly HttpClient client;
private readonly MemoryCache memoryCache;

public BitsClient(string apiKey, HttpClient client, MemoryCache memoryCache)
{
this.apiKey = apiKey;
this.client = client;
this.memoryCache = memoryCache;
}

public ILogger Logger { get; set; } = NullLogger.Instance;

public async Task<HeadInfo> GetHeadInfo(int matchId)
{
HeadInfo result = await Get<HeadInfo>($"https://api.swebowl.se/api/v1/matchResult/GetHeadInfo?APIKey={apiKey}&id={matchId}");
HeadInfo result = await Get<HeadInfo>($"/api/v1/matchResult/GetHeadInfo?id={matchId}");
return result;
}

public async Task<HeadResultInfo> GetHeadResultInfo(int matchId)
{
HeadResultInfo result = await Get<HeadResultInfo>($"https://api.swebowl.se/api/v1/matchResult/GetHeadResultInfo?APIKey={apiKey}&id={matchId}");
HeadResultInfo result = await Get<HeadResultInfo>($"/api/v1/matchResult/GetHeadResultInfo?id={matchId}");
return result;
}

public async Task<MatchResults> GetMatchResults(int matchId)
{
MatchResults result = await Get<MatchResults>($"https://api.swebowl.se/api/v1/matchResult/GetMatchResults?APIKey={apiKey}&matchId={matchId}&matchSchemeId=8M8BA");
MatchResults result = await Get<MatchResults>($"/api/v1/matchResult/GetMatchResults?matchId={matchId}&matchSchemeId=8M8BA");
return result;
}

public async Task<MatchScores> GetMatchScores(int matchId)
{
MatchScores result = await Get<MatchScores>($"https://api.swebowl.se/api/v1/matchResult/GetMatchScores?APIKey={apiKey}&matchId={matchId}&matchSchemeId=8M8BA");
MatchScores result = await Get<MatchScores>($"/api/v1/matchResult/GetMatchScores?matchId={matchId}&matchSchemeId=8M8BA");
return result;
}

public async Task<TeamResult[]> GetTeam(int clubId, int seasonId)
{
TeamResult[] result = await Get<TeamResult[]>($"https://api.swebowl.se/api/v1/Team?APIKey={apiKey}&clubId={clubId}&seasonId={seasonId}");
TeamResult[] result = await Get<TeamResult[]>($"/api/v1/Team?clubId={clubId}&seasonId={seasonId}");
return result;
}

public async Task<DivisionResult[]> GetDivisions(int teamId, int seasonId)
{
DivisionResult[] result = await Get<DivisionResult[]>($"https://api.swebowl.se/api/v1/Division?APIKey={apiKey}&teamId={teamId}&seasonId={seasonId}");
DivisionResult[] result = await Get<DivisionResult[]>($"/api/v1/Division?teamId={teamId}&seasonId={seasonId}");
return result;
}

public async Task<MatchRound[]> GetMatchRounds(int teamId, int divisionId, int seasonId)
{
MatchRound[] result = await Get<MatchRound[]>($"https://api.swebowl.se/api/v1/Match/?APIKey={apiKey}&teamId={teamId}&divisionId={divisionId}&seasonId={seasonId}");
MatchRound[] result = await Get<MatchRound[]>($"/api/v1/Match/?teamId={teamId}&divisionId={divisionId}&seasonId={seasonId}");
return result;
}

Expand All @@ -79,7 +68,7 @@ public async Task<PlayerResult> GetPlayers(int clubId)
pageSize = "250",
sort = new object[0]
},
$"https://api.swebowl.se/api/v1/player/GetAll?APIKey={apiKey}");
$"/api/v1/player/GetAll");
return result;
}

Expand Down Expand Up @@ -163,7 +152,6 @@ private async Task<string> Request(HttpMethod method, string url, Action<HttpReq
"Requesting {url}",
url);
HttpRequestMessage request = new(method, url);
request.Headers.Referrer = new Uri("https://bits.swebowl.se");
action.Invoke(request);
HttpResponseMessage result = await client.SendAsync(request);
string content = await result.EnsureSuccessStatusCode().Content.ReadAsStringAsync();
Expand Down
18 changes: 4 additions & 14 deletions Snittlistan.Web/Infrastructure/Installers/BitsClientInstaller.cs
Original file line number Diff line number Diff line change
@@ -1,29 +1,19 @@

#nullable enable

using System.Net.Http;
using System.Runtime.Caching;
using Snittlistan.Web.Infrastructure.Bits;
using Castle.MicroKernel.Registration;
using Castle.MicroKernel.SubSystems.Configuration;
using Castle.Windsor;

#nullable enable

namespace Snittlistan.Web.Infrastructure.Installers;
public class BitsClientInstaller : IWindsorInstaller
{
private readonly string apiKey;
private readonly HttpClient httpClient;

public BitsClientInstaller(string apiKey, HttpClient httpClient)
{
this.apiKey = apiKey;
this.httpClient = httpClient;
}

public class BitsClientInstaller(HttpClient httpClient) : IWindsorInstaller
{
public void Install(IWindsorContainer container, IConfigurationStore store)
{
BitsClient bitsClient = new(
apiKey,
httpClient,
MemoryCache.Default);
_ = container.Register(Component.For<IBitsClient>().Instance(bitsClient));
Expand Down

0 comments on commit a2a73c6

Please sign in to comment.