Skip to content

Commit

Permalink
Removes async modifier where unnecessary
Browse files Browse the repository at this point in the history
  • Loading branch information
waldekmastykarz committed Dec 7, 2023
1 parent 8efce67 commit d2042e8
Show file tree
Hide file tree
Showing 14 changed files with 56 additions and 27 deletions.
11 changes: 7 additions & 4 deletions dev-proxy-plugins/Behavior/RateLimitingPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -181,26 +181,27 @@ public override void Register(IPluginEvents pluginEvents,
}

// add rate limiting headers to the response from the API
private async Task OnResponse(object? sender, ProxyResponseArgs e)
private Task OnResponse(object? sender, ProxyResponseArgs e)
{
if (_urlsToWatch is null ||
!e.HasRequestUrlMatch(_urlsToWatch))
{
return;
return Task.CompletedTask;
}

UpdateProxyResponse(e, HttpStatusCode.OK);
return Task.CompletedTask;
}

private async Task OnRequest(object? sender, ProxyRequestArgs e)
private Task OnRequest(object? sender, ProxyRequestArgs e)
{
var session = e.Session;
var state = e.ResponseState;
if (e.ResponseState.HasBeenSet ||
_urlsToWatch is null ||
!e.ShouldExecute(_urlsToWatch))
{
return;
return Task.CompletedTask;
}

// set the initial values for the first request
Expand Down Expand Up @@ -267,5 +268,7 @@ _urlsToWatch is null ||
}
}
}

return Task.CompletedTask;
}
}
5 changes: 3 additions & 2 deletions dev-proxy-plugins/Behavior/RetryAfterPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,17 @@ public override void Register(IPluginEvents pluginEvents,
pluginEvents.BeforeRequest += OnRequest;
}

private async Task OnRequest(object? sender, ProxyRequestArgs e)
private Task OnRequest(object? sender, ProxyRequestArgs e)
{
if (e.ResponseState.HasBeenSet ||
_urlsToWatch is null ||
!e.ShouldExecute(_urlsToWatch))
{
return;
return Task.CompletedTask;
}

ThrottleIfNecessary(e);
return Task.CompletedTask;
}

private void ThrottleIfNecessary(ProxyRequestArgs e)
Expand Down
7 changes: 4 additions & 3 deletions dev-proxy-plugins/Guidance/CachingGuidancePlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ public override void Register(IPluginEvents pluginEvents,
pluginEvents.BeforeRequest += BeforeRequest;
}

private async Task BeforeRequest(object? sender, ProxyRequestArgs e)
private Task BeforeRequest(object? sender, ProxyRequestArgs e)
{
if (_urlsToWatch is null || !e.HasRequestUrlMatch(_urlsToWatch))
{
return;
return Task.CompletedTask;
}

Request request = e.Session.HttpClient.Request;
Expand All @@ -43,7 +43,7 @@ private async Task BeforeRequest(object? sender, ProxyRequestArgs e)
if (!_interceptedRequests.ContainsKey(url))
{
_interceptedRequests.Add(url, now);
return;
return Task.CompletedTask;
}

var lastIntercepted = _interceptedRequests[url];
Expand All @@ -54,6 +54,7 @@ private async Task BeforeRequest(object? sender, ProxyRequestArgs e)
}

_interceptedRequests[url] = now;
return Task.CompletedTask;
}

private static string[] BuildCacheWarningMessage(Request r, int _warningSeconds, DateTime lastIntercepted) => new[] {
Expand Down
4 changes: 3 additions & 1 deletion dev-proxy-plugins/Guidance/GraphBetaSupportGuidancePlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@ public override void Register(IPluginEvents pluginEvents,
pluginEvents.AfterResponse += AfterResponse;
}

private async Task AfterResponse(object? sender, ProxyResponseArgs e) {
private Task AfterResponse(object? sender, ProxyResponseArgs e)
{
Request request = e.Session.HttpClient.Request;
if (_urlsToWatch is not null &&
e.HasRequestUrlMatch(_urlsToWatch) &&
ProxyUtils.IsGraphBetaRequest(request))
_logger?.LogRequest(BuildBetaSupportMessage(request), MessageType.Warning, new LoggingContext(e.Session));
return Task.CompletedTask;
}

private static string GetBetaSupportGuidanceUrl() => "https://aka.ms/devproxy/guidance/beta-support";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public override void Register(IPluginEvents pluginEvents,
pluginEvents.BeforeRequest += BeforeRequest;
}

private async Task BeforeRequest(object? sender, ProxyRequestArgs e)
private Task BeforeRequest(object? sender, ProxyRequestArgs e)
{
Request request = e.Session.HttpClient.Request;
if (_urlsToWatch is not null && e.HasRequestUrlMatch(_urlsToWatch) && WarnNoClientRequestId(request))
Expand All @@ -33,6 +33,8 @@ private async Task BeforeRequest(object? sender, ProxyRequestArgs e)
_logger?.LogRequest(MessageUtils.BuildUseSdkMessage(request), MessageType.Tip, new LoggingContext(e.Session));
}
}

return Task.CompletedTask;
}

private static bool WarnNoClientRequestId(Request request) =>
Expand Down
5 changes: 4 additions & 1 deletion dev-proxy-plugins/Guidance/GraphSdkGuidancePlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ public override void Register(IPluginEvents pluginEvents,
pluginEvents.AfterResponse += OnAfterResponse;
}

private async Task OnAfterResponse(object? sender, ProxyResponseArgs e) {
private Task OnAfterResponse(object? sender, ProxyResponseArgs e)
{
Request request = e.Session.HttpClient.Request;
// only show the message if there is an error.
if (e.Session.HttpClient.Response.StatusCode >= 400
Expand All @@ -28,6 +29,8 @@ private async Task OnAfterResponse(object? sender, ProxyResponseArgs e) {
&& WarnNoSdk(request)) {
_logger?.LogRequest(MessageUtils.BuildUseSdkForErrorsMessage(request), MessageType.Tip, new LoggingContext(e.Session));
}

return Task.CompletedTask;
}

private static bool WarnNoSdk(Request request) => ProxyUtils.IsGraphRequest(request) && !ProxyUtils.IsSdkRequest(request);
Expand Down
4 changes: 3 additions & 1 deletion dev-proxy-plugins/Guidance/GraphSelectGuidancePlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ public override void Register(IPluginEvents pluginEvents,
pluginEvents.AfterResponse += AfterResponse;
}

private async Task AfterResponse(object? sender, ProxyResponseArgs e)
private Task AfterResponse(object? sender, ProxyResponseArgs e)
{
Request request = e.Session.HttpClient.Request;
if (_urlsToWatch is not null && e.HasRequestUrlMatch(_urlsToWatch) && WarnNoSelect(request))
_logger?.LogRequest(BuildUseSelectMessage(request), MessageType.Warning, new LoggingContext(e.Session));

return Task.CompletedTask;
}

private bool WarnNoSelect(Request request)
Expand Down
4 changes: 3 additions & 1 deletion dev-proxy-plugins/Guidance/ODSPSearchGuidancePlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ public override void Register(IPluginEvents pluginEvents,
pluginEvents.BeforeRequest += BeforeRequest;
}

private async Task BeforeRequest(object sender, ProxyRequestArgs e)
private Task BeforeRequest(object sender, ProxyRequestArgs e)
{
Request request = e.Session.HttpClient.Request;
if (_urlsToWatch is not null && e.HasRequestUrlMatch(_urlsToWatch) && WarnDeprecatedSearch(request))
_logger?.LogRequest(BuildUseGraphSearchMessage(), MessageType.Warning, new LoggingContext(e.Session));

return Task.CompletedTask;
}

private bool WarnDeprecatedSearch(Request request)
Expand Down
6 changes: 4 additions & 2 deletions dev-proxy-plugins/Guidance/ODataPagingGuidancePlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,22 @@ public override void Register(IPluginEvents pluginEvents,
pluginEvents.BeforeResponse += OnBeforeResponse;
}

private async Task OnBeforeRequest(object? sender, ProxyRequestArgs e)
private Task OnBeforeRequest(object? sender, ProxyRequestArgs e)
{
if (_urlsToWatch is null ||
e.Session.HttpClient.Request.Method != "GET" ||
!e.HasRequestUrlMatch(_urlsToWatch))
{
return;
return Task.CompletedTask;
}

if (IsODataPagingUrl(e.Session.HttpClient.Request.RequestUri) &&
!pagingUrls.Contains(e.Session.HttpClient.Request.Url))
{
_logger?.LogRequest(BuildIncorrectPagingUrlMessage(), MessageType.Warning, new LoggingContext(e.Session));
}

return Task.CompletedTask;
}

private async Task OnBeforeResponse(object? sender, ProxyResponseArgs e)
Expand Down
5 changes: 4 additions & 1 deletion dev-proxy-plugins/MockResponses/MockResponsePlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ private void OnOptionsLoaded(object? sender, OptionsLoadedArgs e) {
_loader?.InitResponsesWatcher();
}

protected virtual async Task OnRequest(object? sender, ProxyRequestArgs e) {
protected virtual Task OnRequest(object? sender, ProxyRequestArgs e)
{
Request request = e.Session.HttpClient.Request;
ResponseState state = e.ResponseState;
if (!_configuration.NoMocks && _urlsToWatch is not null && e.ShouldExecute(_urlsToWatch)) {
Expand All @@ -115,6 +116,8 @@ protected virtual async Task OnRequest(object? sender, ProxyRequestArgs e) {
state.HasBeenSet = true;
}
}

return Task.CompletedTask;
}

private MockResponse? GetMatchingMockResponse(Request request) {
Expand Down
8 changes: 5 additions & 3 deletions dev-proxy-plugins/RandomErrors/GenericRandomErrorPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@ private void OnInit(object? sender, InitArgs e) {
_loader?.InitResponsesWatcher();
}

private async Task OnRequest(object? sender, ProxyRequestArgs e) {
var session = e.Session;
private Task OnRequest(object? sender, ProxyRequestArgs e)
{
var state = e.ResponseState;
if (!e.ResponseState.HasBeenSet
&& _urlsToWatch is not null
Expand All @@ -129,10 +129,12 @@ private async Task OnRequest(object? sender, ProxyRequestArgs e) {

if (failMode == GenericRandomErrorFailMode.PassThru && _proxyConfiguration?.Rate != 100) {
_logger?.LogRequest(new[] { "Passed through" }, MessageType.PassedThrough, new LoggingContext(e.Session));
return;
return Task.CompletedTask;
}
FailResponse(e, failMode);
state.HasBeenSet = true;
}

return Task.CompletedTask;
}
}
8 changes: 5 additions & 3 deletions dev-proxy-plugins/RandomErrors/GraphRandomErrorPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -237,16 +237,16 @@ private void OnOptionsLoaded(object? sender, OptionsLoadedArgs e) {
}
}

private async Task OnRequest(object? sender, ProxyRequestArgs e) {
var session = e.Session;
private Task OnRequest(object? sender, ProxyRequestArgs e)
{
var state = e.ResponseState;
if (!e.ResponseState.HasBeenSet
&& _urlsToWatch is not null
&& e.ShouldExecute(_urlsToWatch)) {
var failMode = ShouldFail(e);

if (failMode == GraphRandomErrorFailMode.PassThru && _proxyConfiguration?.Rate != 100) {
return;
return Task.CompletedTask;
}
if (ProxyUtils.IsGraphBatchUrl(e.Session.HttpClient.Request.RequestUri)) {
FailBatch(e);
Expand All @@ -256,5 +256,7 @@ private async Task OnRequest(object? sender, ProxyRequestArgs e) {
}
state.HasBeenSet = true;
}

return Task.CompletedTask;
}
}
6 changes: 4 additions & 2 deletions dev-proxy-plugins/RequestLogs/ExecutionSummaryPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,11 @@ private void OnOptionsLoaded(object? sender, OptionsLoadedArgs e)
}
}

private async Task AfterRecordingStop(object? sender, RecordingArgs e)
private Task AfterRecordingStop(object? sender, RecordingArgs e)
{
if (!e.RequestLogs.Any())
{
return;
return Task.CompletedTask;
}

var report = _configuration.GroupBy switch
Expand All @@ -128,6 +128,8 @@ private async Task AfterRecordingStop(object? sender, RecordingArgs e)
{
File.WriteAllLines(_configuration.FilePath, report);
}

return Task.CompletedTask;
}

private string[] GetGroupedByUrlReport(IEnumerable<RequestLog> requestLogs)
Expand Down
6 changes: 4 additions & 2 deletions dev-proxy-plugins/RequestLogs/MockGeneratorPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ public override void Register(IPluginEvents pluginEvents,
pluginEvents.AfterRecordingStop += AfterRecordingStop;
}

private async Task AfterRecordingStop(object? sender, RecordingArgs e)
private Task AfterRecordingStop(object? sender, RecordingArgs e)
{
_logger?.LogInfo("Creating mocks from recorded requests...");

if (!e.RequestLogs.Any())
{
_logger?.LogDebug("No requests to process");
return;
return Task.CompletedTask;
}

var methodAndUrlComparer = new MethodAndUrlComparer();
Expand Down Expand Up @@ -86,6 +86,8 @@ request.Context is null ||
File.WriteAllText(fileName, mocksFileJson);

_logger?.LogInfo($"Created mock file {fileName} with {mocks.Count} mocks");

return Task.CompletedTask;
}

/// <summary>
Expand Down

0 comments on commit d2042e8

Please sign in to comment.