Skip to content

Commit 5420032

Browse files
committed
feat: change token management for users to be outside the scope of the lib.
BREAKING CHANGES: Use Services.AddHttpClient("AuthClient", (sp, httpClient) => ...).AddUserAccessTokenHandler(); or any other kinds of token management, and then use "AuthClient" to inform lib which client it should use. CgScriptOptions.Site is no longer used. Set the BaseAddress of the httpClient in the above config to decide which site to call.
1 parent 699a036 commit 5420032

File tree

11 files changed

+33
-86
lines changed

11 files changed

+33
-86
lines changed

Catglobe.CgScript.Common/Catglobe.CgScript.Common.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk" InitialTargets="ValidatePrecommitHook">
22
<PropertyGroup>
33
<PackageId>Catglobe.CgScript.Common</PackageId>
4-
<Version>0.0.3</Version>
4+
<Version>0.0.5</Version>
55
<Authors>Dennis Haney</Authors>
66
<Company>Voxmeter A/S</Company>
77
<Product>Catglobe Script Deployer</Product>
@@ -39,6 +39,7 @@
3939
<PackageReference Include="OpenTelemetry.Api" Version="1.12.0" />
4040
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="9.0.8" />
4141
<PackageReference Include="Microsoft.Extensions.Http.Resilience" Version="9.7.0" />
42+
<PackageReference Include="Microsoft.Extensions.Http" Version="9.0.8" />
4243
</ItemGroup>
4344

4445
<PropertyGroup>

Catglobe.CgScript.Common/CgScriptOptions.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,6 @@
55
/// </summary>
66
public class CgScriptOptions
77
{
8-
/// <summary>
9-
/// Which site are we running on
10-
/// </summary>
11-
public Uri Site { get; set; } = null!;
12-
138
/// <summary>
149
/// Which root folder are we running from
1510
/// </summary>

Catglobe.CgScript.Deployment/Catglobe.CgScript.Deployment.csproj

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
33
<PackageId>Catglobe.CgScript.Deployment</PackageId>
4-
<Version>0.0.3</Version>
4+
<Version>0.0.5</Version>
55
<Authors>Dennis Haney</Authors>
66
<Company>Voxmeter A/S</Company>
77
<Product>Catglobe Script Deployer</Product>
@@ -42,7 +42,6 @@
4242
<ItemGroup>
4343
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="9.0.8" />
4444
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="9.0.8" />
45-
<PackageReference Include="Microsoft.Extensions.Http" Version="9.0.8" />
4645
<PackageReference Include="Microsoft.Extensions.Options" Version="9.0.8" />
4746
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="9.0.8" />
4847
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="9.0.8" />

Catglobe.CgScript.Deployment/HostExtensions.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
using Microsoft.Extensions.DependencyInjection;
55
using Microsoft.Extensions.DependencyInjection.Extensions;
66
using Microsoft.Extensions.Options;
7-
using OpenTelemetry.Trace;
8-
using Polly;
97

108
namespace Catglobe.CgScript.Deployment;
119

@@ -41,14 +39,17 @@ private static IServiceCollection AddCommonCgScript(IServiceCollection services)
4139
{
4240
services.TryAddSingleton<IScriptProvider, FilesFromDirectoryScriptProvider>();
4341
services.AddScoped<DeploymentAuthHandler>();
42+
#pragma warning disable EXTEXP0001
4443
services.AddHttpClient<IDeployer, Deployer>((sp, httpClient) => {
4544
var site = sp.GetRequiredService<IOptions<DeploymentOptions>>().Value.Authority;
4645
httpClient.BaseAddress = new(site + "api/CgScriptDeployment/");
4746
})
4847
.AddHttpMessageHandler<DeploymentAuthHandler>()
48+
.RemoveAllResilienceHandlers()
49+
#pragma warning restore EXTEXP0001
4950
.AddStandardResilienceHandler(o => {
5051
o.AttemptTimeout.Timeout = TimeSpan.FromMinutes(10);
51-
o.CircuitBreaker.SamplingDuration = TimeSpan.FromMinutes(20);
52+
o.CircuitBreaker.SamplingDuration = TimeSpan.FromMinutes(30);
5253
o.TotalRequestTimeout.Timeout = TimeSpan.FromMinutes(30);
5354
});
5455
services.AddHttpClient<DeploymentAuthenticator>((sp, httpClient) => {

Catglobe.CgScript.Runtime/Catglobe.CgScript.Runtime.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
33
<PackageId>Catglobe.CgScript.Runtime</PackageId>
4-
<Version>0.0.3</Version>
4+
<Version>0.0.5</Version>
55
<Authors>Dennis Haney</Authors>
66
<Company>Voxmeter A/S</Company>
77
<Product>Catglobe Script Deployer</Product>

Catglobe.CgScript.Runtime/CgScriptApiClient.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ internal class CgScriptApiClient(HttpClient httpClient, IScriptMapping map, ILog
1212
protected override async ValueTask<string> GetPath(string scriptName, string? additionalParameters = null)
1313
{
1414
await map.EnsureDownloaded();
15-
return $"run/{map.GetIdOf(scriptName)}{additionalParameters ?? ""}";
15+
return $"api/CgScript/run/{map.GetIdOf(scriptName)}{additionalParameters ?? ""}";
1616
}
1717

1818
protected override Task<JsonContent?> GetJsonContent<TP>(string scriptName, TP? parameter, JsonTypeInfo<TP> callJsonTypeInfo) where TP : default =>

Catglobe.CgScript.Runtime/CgScriptAuthHandler.cs

Lines changed: 0 additions & 43 deletions
This file was deleted.

Catglobe.CgScript.Runtime/DevelopmentModeCgScriptApiClient.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99

1010
namespace Catglobe.CgScript.Runtime;
1111

12-
internal partial class DevelopmentModeCgScriptApiClient(HttpClient httpClient, IScriptProvider scriptProvider, IOptions<CgScriptOptions> options, ILogger<ICgScriptApiClient> logger) : ApiClientBase(httpClient, logger)
12+
internal partial class DevelopmentModeCgScriptApiClient(HttpClient httpClient, IScriptProvider scriptProvider, IOptions<CgScriptOptions> options, ILogger<ICgScriptApiClient> logger)
13+
: ApiClientBase(httpClient, logger)
1314
{
1415
private IReadOnlyDictionary<string, IScriptDefinition>? _scriptDefinitions;
1516
private BaseCgScriptMaker? _cgScriptMaker;
@@ -20,7 +21,7 @@ protected override async ValueTask<string> GetPath(string scriptName, string? ad
2021
_scriptDefinitions = await scriptProvider.GetAll();
2122
_cgScriptMaker = new CgScriptMakerForDevelopment(_scriptDefinitions, options.Value.ImpersonationMapping);
2223
}
23-
return $"dynamicRun{additionalParameters ?? ""}";
24+
return $"api/CgScript/dynamicRun{additionalParameters ?? ""}";
2425
}
2526

2627
protected override async Task<JsonContent?> GetJsonContent<TP>(string scriptName, TP? parameter, JsonTypeInfo<TP> callJsonTypeInfo) where TP : default =>

Catglobe.CgScript.Runtime/HostExtensions.cs

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
using System.Diagnostics.CodeAnalysis;
2-
using Catglobe.CgScript.Common;
1+
using Catglobe.CgScript.Common;
32
using Microsoft.Extensions.Configuration;
43
using Microsoft.Extensions.DependencyInjection;
54
using Microsoft.Extensions.Options;
6-
using OpenTelemetry.Trace;
75

86
namespace Catglobe.CgScript.Runtime;
97

@@ -15,46 +13,40 @@ public static class HostExtensions
1513
/// <summary>
1614
/// Add CgScript support
1715
/// </summary>
18-
public static IServiceCollection AddCgScript(this IServiceCollection services, bool isDevelopment, Action<CgScriptOptions>? configurator = null)
16+
public static IServiceCollection AddCgScript(this IServiceCollection services, bool isDevelopment, string nameOfHttpClient, Action<CgScriptOptions>? configurator = null)
1917
{
2018
if (configurator is not null) services.Configure(configurator);
21-
return AddCommonCgScript(services, isDevelopment);
19+
return AddCommonCgScript(services, isDevelopment, nameOfHttpClient);
2220
}
2321
/// <summary>
2422
/// Add CgScript support
2523
/// </summary>
26-
public static IServiceCollection AddCgScript(this IServiceCollection services, IConfiguration namedConfigurationSection, bool isDevelopment)
24+
public static IServiceCollection AddCgScript(this IServiceCollection services, IConfiguration namedConfigurationSection, bool isDevelopment, string nameOfHttpClient)
2725
{
2826
services.Configure<CgScriptOptions>(namedConfigurationSection);
29-
return AddCommonCgScript(services, isDevelopment);
27+
return AddCommonCgScript(services, isDevelopment, nameOfHttpClient);
3028
}
3129

32-
private static IServiceCollection AddCommonCgScript(IServiceCollection services, bool isDevelopment)
30+
private static IServiceCollection AddCommonCgScript(IServiceCollection services, bool isDevelopment, string nameOfHttpClient)
3331
{
3432
services.AddHttpContextAccessor();
35-
services.AddHttpClient<IScriptMapping, ScriptMapping>((sp, httpClient) => {
36-
var site = sp.GetRequiredService<IOptions<CgScriptOptions>>().Value.Site;
37-
httpClient.BaseAddress = new(site + "api/CgScriptDeployment/");
38-
});
33+
services.AddHttpClient<IScriptMapping, ScriptMapping>(nameOfHttpClient);
3934

40-
services.AddScoped<CgScriptAuthHandler>();
41-
Action<IServiceProvider, HttpClient> configureClient = (sp, httpClient) => {
42-
var site = sp.GetRequiredService<IOptions<CgScriptOptions>>().Value.Site;
43-
httpClient.BaseAddress = new(site + "api/CgScript/");
44-
};
45-
(isDevelopment
46-
? services.AddHttpClient<ICgScriptApiClient, DevelopmentModeCgScriptApiClient>(configureClient)
47-
: services.AddHttpClient<ICgScriptApiClient, CgScriptApiClient>(configureClient))
48-
.AddHttpMessageHandler<CgScriptAuthHandler>();
35+
if (isDevelopment)
36+
services.AddHttpClient<ICgScriptApiClient, DevelopmentModeCgScriptApiClient>(nameOfHttpClient);
37+
else
38+
services.AddHttpClient<ICgScriptApiClient, CgScriptApiClient>(nameOfHttpClient);
4939

40+
#pragma warning disable EXTEXP0001
5041
(isDevelopment
51-
? services.AddHttpClient<ILongRunningCgScriptApiClient, DevelopmentModeCgScriptApiClient>(configureClient)
52-
: services.AddHttpClient<ILongRunningCgScriptApiClient, CgScriptApiClient>(configureClient))
53-
.AddHttpMessageHandler<CgScriptAuthHandler>()
42+
? services.AddHttpClient<ILongRunningCgScriptApiClient, DevelopmentModeCgScriptApiClient>(nameOfHttpClient)
43+
: services.AddHttpClient<ILongRunningCgScriptApiClient, CgScriptApiClient>(nameOfHttpClient))
44+
.RemoveAllResilienceHandlers()
45+
#pragma warning restore EXTEXP0001
5446
.AddStandardResilienceHandler(o => {
5547
o.AttemptTimeout.Timeout = TimeSpan.FromMinutes(30);
5648
o.TotalRequestTimeout.Timeout = TimeSpan.FromMinutes(90);
57-
o.CircuitBreaker.SamplingDuration = TimeSpan.FromMinutes(60);
49+
o.CircuitBreaker.SamplingDuration = TimeSpan.FromMinutes(90);
5850
});
5951
return services;
6052
}

Catglobe.CgScript.Runtime/ScriptMapping.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public async ValueTask EnsureDownloaded()
1717
{
1818
if (_map is not null) return;
1919
using var activity = CgScriptTelemetry.Source.StartActivity("DownloadMap");
20-
var req = await httpClient.GetAsync($"GetMap/{options.Value.FolderResourceId}");
20+
var req = await httpClient.GetAsync($"api/CgScriptDeployment/GetMap/{options.Value.FolderResourceId}");
2121
if (!req.IsSuccessStatusCode)
2222
activity?.SetStatus(ActivityStatusCode.Error);
2323
req.EnsureSuccessStatusCode();

0 commit comments

Comments
 (0)