Skip to content

Commit 4434b84

Browse files
Refactors APIC plugins (#762)
* Refactors APIC plugins * Refactors API Center plugins
1 parent bdae98f commit 4434b84

File tree

2 files changed

+24
-10
lines changed

2 files changed

+24
-10
lines changed

dev-proxy-plugins/RequestLogs/ApiCenterOnboardingPlugin.cs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ public class ApiCenterOnboardingPlugin : BaseReportingPlugin
4242
{
4343
private ApiCenterOnboardingPluginConfiguration _configuration = new();
4444
private ApiCenterClient? _apiCenterClient;
45+
private Api[]? _apis;
46+
private Dictionary<string, ApiDefinition>? _apiDefinitionsByUrl;
4547

4648
public ApiCenterOnboardingPlugin(IPluginEvents pluginEvents, IProxyContext context, ILogger logger, ISet<UrlToWatch> urlsToWatch, IConfigurationSection? configSection = null) : base(pluginEvents, context, logger, urlsToWatch, configSection)
4749
{
@@ -74,7 +76,7 @@ public override void Register()
7476
return;
7577
}
7678

77-
Logger.LogDebug("Plugin {plugin} checking Azure auth...", Name);
79+
Logger.LogInformation("Plugin {plugin} connecting to Azure...", Name);
7880
try
7981
{
8082
_ = _apiCenterClient.GetAccessToken(CancellationToken.None).Result;
@@ -101,14 +103,21 @@ private async Task AfterRecordingStop(object sender, RecordingArgs e)
101103

102104
Debug.Assert(_apiCenterClient is not null);
103105

104-
var apis = await _apiCenterClient.GetApis();
105-
if (apis == null || !apis.Any())
106+
if (_apis is null)
107+
{
108+
_apis = await _apiCenterClient.GetApis();
109+
}
110+
111+
if (_apis == null || !_apis.Any())
106112
{
107113
Logger.LogInformation("No APIs found in API Center");
108114
return;
109115
}
110116

111-
var apiDefinitions = await apis.GetApiDefinitionsByUrl(_apiCenterClient, Logger);
117+
if (_apiDefinitionsByUrl is null)
118+
{
119+
_apiDefinitionsByUrl = await _apis.GetApiDefinitionsByUrl(_apiCenterClient, Logger);
120+
}
112121

113122
var newApis = new List<(string method, string url)>();
114123
var interceptedRequests = e.RequestLogs
@@ -129,7 +138,7 @@ private async Task AfterRecordingStop(object sender, RecordingArgs e)
129138

130139
Logger.LogDebug("Processing request {method} {url}...", method, url);
131140

132-
var apiDefinition = apiDefinitions.FirstOrDefault(x =>
141+
var apiDefinition = _apiDefinitionsByUrl.FirstOrDefault(x =>
133142
url.StartsWith(x.Key, StringComparison.OrdinalIgnoreCase)).Value;
134143
if (apiDefinition is null ||
135144
apiDefinition.Id is null)

dev-proxy-plugins/RequestLogs/ApiCenterProductionVersionPlugin.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public class ApiCenterProductionVersionPlugin : BaseReportingPlugin
4040
{
4141
private ApiCenterProductionVersionPluginConfiguration _configuration = new();
4242
private ApiCenterClient? _apiCenterClient;
43+
private Api[]? _apis;
4344

4445
public ApiCenterProductionVersionPlugin(IPluginEvents pluginEvents, IProxyContext context, ILogger logger, ISet<UrlToWatch> urlsToWatch, IConfigurationSection? configSection = null) : base(pluginEvents, context, logger, urlsToWatch, configSection)
4546
{
@@ -72,7 +73,7 @@ public override void Register()
7273
return;
7374
}
7475

75-
Logger.LogDebug("Plugin {plugin} checking Azure auth...", Name);
76+
Logger.LogInformation("Plugin {plugin} connecting to Azure...", Name);
7677
try
7778
{
7879
_ = _apiCenterClient.GetAccessToken(CancellationToken.None).Result;
@@ -104,14 +105,18 @@ private async Task AfterRecordingStop(object sender, RecordingArgs e)
104105

105106
Debug.Assert(_apiCenterClient is not null);
106107

107-
var apisFromApiCenter = await _apiCenterClient.GetApis();
108-
if (apisFromApiCenter == null || !apisFromApiCenter.Any())
108+
if (_apis is null)
109+
{
110+
_apis = await _apiCenterClient.GetApis();
111+
}
112+
113+
if (_apis == null || !_apis.Any())
109114
{
110115
Logger.LogInformation("No APIs found in API Center");
111116
return;
112117
}
113118

114-
foreach (var api in apisFromApiCenter)
119+
foreach (var api in _apis)
115120
{
116121
Debug.Assert(api.Id is not null);
117122

@@ -173,7 +178,7 @@ private async Task AfterRecordingStop(object sender, RecordingArgs e)
173178
continue;
174179
}
175180

176-
var api = apisFromApiCenter.FindApiByUrl(url, Logger);
181+
var api = _apis.FindApiByUrl(url, Logger);
177182
if (api == null)
178183
{
179184
report.Add(new()

0 commit comments

Comments
 (0)