Skip to content

Commit 2c44d1f

Browse files
Fixes httpclient usage (#1350)
* Expose HttpClient to derived plugin classes * Use short-lived HTTP client controlled by DI * refactor Announcement to use with DI * refactor MSGraphDbUtils to use with DI * Fix build warning * Revert "Expose HttpClient to derived plugin classes" This reverts commit 751755e. * Revert "refactor Announcement to use with DI" This reverts commit dd4ebb0. * reuse injected http client from DI * fix: Remove httpclient disposal, it's handled by its factory * fix: Rename MSGraphDbUtils to MSGraphDb * fix: Rename MSGraphDbConnection to Connection * fix: Rename GenerateMSGraphDbAsync to GenerateDbAsync * fix: Remove unnecessary assignment * refactor: Remove MSGraphDb class and related usages; update dependencies in GraphSelectGuidancePlugin and IServiceCollectionExtensions --------- Co-authored-by: Waldek Mastykarz <waldek@mastykarz.nl>
1 parent 6a9501d commit 2c44d1f

File tree

8 files changed

+240
-227
lines changed

8 files changed

+240
-227
lines changed

DevProxy.Abstractions/Utils/MSGraphDbUtils.cs renamed to DevProxy.Abstractions/Data/MSGraphDb.cs

Lines changed: 220 additions & 213 deletions
Large diffs are not rendered by default.

DevProxy.Plugins/Guidance/GraphSelectGuidancePlugin.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,31 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5+
using DevProxy.Abstractions.Data;
56
using DevProxy.Abstractions.Proxy;
67
using DevProxy.Abstractions.Plugins;
78
using DevProxy.Abstractions.Utils;
89
using Microsoft.Extensions.Logging;
9-
using Titanium.Web.Proxy.EventArguments;
1010
using System.Globalization;
11+
using Titanium.Web.Proxy.EventArguments;
1112

1213
namespace DevProxy.Plugins.Guidance;
1314

1415
public sealed class GraphSelectGuidancePlugin(
1516
ILogger<GraphSelectGuidancePlugin> logger,
16-
ISet<UrlToWatch> urlsToWatch) : BasePlugin(logger, urlsToWatch)
17+
ISet<UrlToWatch> urlsToWatch,
18+
MSGraphDb msGraphDb) : BasePlugin(logger, urlsToWatch)
1719
{
20+
private readonly MSGraphDb _msGraphDb = msGraphDb;
21+
1822
public override string Name => nameof(GraphSelectGuidancePlugin);
1923

2024
public override async Task InitializeAsync(InitArgs e, CancellationToken cancellationToken)
2125
{
2226
await base.InitializeAsync(e, cancellationToken);
2327

2428
// let's not await so that it doesn't block the proxy startup
25-
_ = MSGraphDbUtils.GenerateMSGraphDbAsync(Logger, true, cancellationToken);
29+
_ = _msGraphDb.GenerateDbAsync(true, cancellationToken);
2630
}
2731

2832
public override Task AfterResponseAsync(ProxyResponseArgs e, CancellationToken cancellationToken)
@@ -82,7 +86,7 @@ private bool EndpointSupportsSelect(string graphVersion, string relativeUrl)
8286

8387
try
8488
{
85-
var dbConnection = MSGraphDbUtils.MSGraphDbConnection;
89+
var dbConnection = _msGraphDb.Connection;
8690
// lookup information from the database
8791
var selectEndpoint = dbConnection.CreateCommand();
8892
selectEndpoint.CommandText = "SELECT hasSelect FROM endpoints WHERE path = @path AND graphVersion = @graphVersion";

DevProxy.Plugins/Reporting/GraphMinimalPermissionsGuidancePlugin.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public sealed class GraphMinimalPermissionsGuidancePlugin(
5050
pluginConfigurationSection)
5151
{
5252
private GraphUtils? _graphUtils;
53+
private readonly HttpClient _httpClient = httpClient;
5354

5455
public override string Name => nameof(GraphMinimalPermissionsGuidancePlugin);
5556

@@ -240,11 +241,10 @@ private async Task EvaluateMinimalScopesAsync(
240241
try
241242
{
242243
var url = $"https://devxapi-func-prod-eastus.azurewebsites.net/permissions?scopeType={GraphUtils.GetScopeTypeString(scopeType)}";
243-
using var client = new HttpClient();
244244
var stringPayload = JsonSerializer.Serialize(payload, ProxyUtils.JsonSerializerOptions);
245245
Logger.LogDebug("Calling {Url} with payload{NewLine}{Payload}", url, Environment.NewLine, stringPayload);
246246

247-
var response = await client.PostAsJsonAsync(url, payload, cancellationToken);
247+
var response = await _httpClient.PostAsJsonAsync(url, payload, cancellationToken);
248248
var content = await response.Content.ReadAsStringAsync(cancellationToken);
249249

250250
Logger.LogDebug("Response:{NewLine}{Content}", Environment.NewLine, content);

DevProxy.Plugins/Reporting/GraphMinimalPermissionsPlugin.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public sealed class GraphMinimalPermissionsPlugin(
3535
pluginConfigurationSection)
3636
{
3737
private GraphUtils? _graphUtils;
38+
private readonly HttpClient _httpClient = httpClient;
3839

3940
public override string Name => nameof(GraphMinimalPermissionsPlugin);
4041

@@ -137,11 +138,10 @@ public override async Task AfterRecordingStopAsync(RecordingArgs e, Cancellation
137138
try
138139
{
139140
var url = $"https://devxapi-func-prod-eastus.azurewebsites.net/permissions?scopeType={GraphUtils.GetScopeTypeString(Configuration.Type)}";
140-
using var client = new HttpClient();
141141
var stringPayload = JsonSerializer.Serialize(payload, ProxyUtils.JsonSerializerOptions);
142142
Logger.LogDebug("Calling {Url} with payload\r\n{StringPayload}", url, stringPayload);
143143

144-
var response = await client.PostAsJsonAsync(url, payload, cancellationToken);
144+
var response = await _httpClient.PostAsJsonAsync(url, payload, cancellationToken);
145145
var content = await response.Content.ReadAsStringAsync(cancellationToken);
146146

147147
Logger.LogDebug("Response:\r\n{Content}", content);

DevProxy/Announcement.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public static async Task ShowAsync()
2222
}
2323
}
2424

25-
public static async Task<string?> GetAsync()
25+
private static async Task<string?> GetAsync()
2626
{
2727
try
2828
{

DevProxy/Commands/MsGraphDbCommand.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,19 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5-
using DevProxy.Abstractions.Utils;
5+
using DevProxy.Abstractions.Data;
66
using System.CommandLine;
77

88
namespace DevProxy.Commands;
99

1010
sealed class MsGraphDbCommand : Command
1111
{
12-
private readonly ILogger _logger;
12+
private readonly MSGraphDb _msGraphDb;
1313

14-
public MsGraphDbCommand(ILogger<MsGraphDbCommand> logger) :
14+
public MsGraphDbCommand(MSGraphDb msGraphDb) :
1515
base("msgraphdb", "Generate a local SQLite database with Microsoft Graph API metadata")
1616
{
17-
_logger = logger;
17+
_msGraphDb = msGraphDb;
1818
ConfigureCommand();
1919
}
2020

@@ -25,6 +25,6 @@ private void ConfigureCommand()
2525

2626
private async Task GenerateMsGraphDbAsync(ParseResult parseResult, CancellationToken cancellationToken)
2727
{
28-
_ = await MSGraphDbUtils.GenerateMSGraphDbAsync(_logger, false, cancellationToken);
28+
_ = await _msGraphDb.GenerateDbAsync(false, cancellationToken);
2929
}
3030
}

DevProxy/Extensions/IServiceCollectionExtensions.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// See the LICENSE file in the project root for more information.
44

55
using DevProxy;
6+
using DevProxy.Abstractions.Data;
67
using DevProxy.Abstractions.LanguageModel;
78
using DevProxy.Abstractions.Proxy;
89
using DevProxy.Commands;
@@ -45,6 +46,7 @@ static IServiceCollection AddApplicationServices(
4546
.AddSingleton<UpdateNotification>()
4647
.AddSingleton<ProxyEngine>()
4748
.AddSingleton<DevProxyCommand>()
49+
.AddSingleton<MSGraphDb>()
4850
.AddHttpClient();
4951

5052
_ = services.AddPlugins(configuration, options);

dev-proxy-plugins/Mocks/MockResponsePlugin.cs

Whitespace-only changes.

0 commit comments

Comments
 (0)