Skip to content

Refactors APIC plugins #762

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions dev-proxy-plugins/RequestLogs/ApiCenterOnboardingPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ public class ApiCenterOnboardingPlugin : BaseReportingPlugin
{
private ApiCenterOnboardingPluginConfiguration _configuration = new();
private ApiCenterClient? _apiCenterClient;
private Api[]? _apis;
private Dictionary<string, ApiDefinition>? _apiDefinitionsByUrl;

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

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

Debug.Assert(_apiCenterClient is not null);

var apis = await _apiCenterClient.GetApis();
if (apis == null || !apis.Any())
if (_apis is null)
{
_apis = await _apiCenterClient.GetApis();
}

if (_apis == null || !_apis.Any())
{
Logger.LogInformation("No APIs found in API Center");
return;
}

var apiDefinitions = await apis.GetApiDefinitionsByUrl(_apiCenterClient, Logger);
if (_apiDefinitionsByUrl is null)
{
_apiDefinitionsByUrl = await _apis.GetApiDefinitionsByUrl(_apiCenterClient, Logger);
}

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

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

var apiDefinition = apiDefinitions.FirstOrDefault(x =>
var apiDefinition = _apiDefinitionsByUrl.FirstOrDefault(x =>
url.StartsWith(x.Key, StringComparison.OrdinalIgnoreCase)).Value;
if (apiDefinition is null ||
apiDefinition.Id is null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public class ApiCenterProductionVersionPlugin : BaseReportingPlugin
{
private ApiCenterProductionVersionPluginConfiguration _configuration = new();
private ApiCenterClient? _apiCenterClient;
private Api[]? _apis;

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

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

Debug.Assert(_apiCenterClient is not null);

var apisFromApiCenter = await _apiCenterClient.GetApis();
if (apisFromApiCenter == null || !apisFromApiCenter.Any())
if (_apis is null)
{
_apis = await _apiCenterClient.GetApis();
}

if (_apis == null || !_apis.Any())
{
Logger.LogInformation("No APIs found in API Center");
return;
}

foreach (var api in apisFromApiCenter)
foreach (var api in _apis)
{
Debug.Assert(api.Id is not null);

Expand Down Expand Up @@ -173,7 +178,7 @@ private async Task AfterRecordingStop(object sender, RecordingArgs e)
continue;
}

var api = apisFromApiCenter.FindApiByUrl(url, Logger);
var api = _apis.FindApiByUrl(url, Logger);
if (api == null)
{
report.Add(new()
Expand Down