Skip to content

Commit abf3b31

Browse files
Changes request-related events to async. Closes #200 (#224)
* Changes BeforeResponse to async event * Updates all request-related events to async --------- Co-authored-by: Gavin Barron <[email protected]>
1 parent c9f3311 commit abf3b31

File tree

12 files changed

+55
-18
lines changed

12 files changed

+55
-18
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
// from: https://github.com/justcoding121/titanium-web-proxy/blob/902504a324425e4e49fc5ba604c2b7fa172e68ce/src/Titanium.Web.Proxy/Extensions/FuncExtensions.cs
5+
6+
using Titanium.Web.Proxy;
7+
using Titanium.Web.Proxy.EventArguments;
8+
9+
namespace Microsoft.Graph.DeveloperProxy.Abstractions;
10+
11+
public static class FuncExtensions
12+
{
13+
internal static async Task InvokeAsync<T>(this AsyncEventHandler<T> callback, object sender, T args, ExceptionHandler? exceptionFunc)
14+
{
15+
var invocationList = callback.GetInvocationList();
16+
17+
foreach (var @delegate in invocationList)
18+
await InternalInvokeAsync((AsyncEventHandler<T>)@delegate, sender, args, exceptionFunc);
19+
}
20+
21+
private static async Task InternalInvokeAsync<T>(AsyncEventHandler<T> callback, object sender, T args, ExceptionHandler? exceptionFunc)
22+
{
23+
try
24+
{
25+
await callback(sender, args);
26+
}
27+
catch (Exception e)
28+
{
29+
exceptionFunc?.Invoke(new Exception("Exception thrown in user event", e));
30+
}
31+
}
32+
}

msgraph-developer-proxy-abstractions/PluginEvents.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -96,18 +96,18 @@ public interface IPluginEvents {
9696
/// Raised before a request is sent to the server.
9797
/// Used to intercept requests.
9898
/// </summary>
99-
event EventHandler<ProxyRequestArgs> BeforeRequest;
99+
event AsyncEventHandler<ProxyRequestArgs> BeforeRequest;
100100
/// <summary>
101101
/// Raised after the response is received from the server.
102102
/// Is not raised if a response is set during the BeforeRequest event.
103103
/// Allows plugins to modify a response received from the server.
104104
/// </summary>
105-
event EventHandler<ProxyResponseArgs> BeforeResponse;
105+
event AsyncEventHandler<ProxyResponseArgs> BeforeResponse;
106106
/// <summary>
107107
/// Raised after a response is sent to the client.
108108
/// Raised for all responses
109109
/// </summary>
110-
event EventHandler<ProxyResponseArgs>? AfterResponse;
110+
event AsyncEventHandler<ProxyResponseArgs>? AfterResponse;
111111
/// <summary>
112112
/// Raised after request message has been logged.
113113
/// </summary>
@@ -124,11 +124,11 @@ public class PluginEvents : IPluginEvents {
124124
/// <inheritdoc />
125125
public event EventHandler<OptionsLoadedArgs>? OptionsLoaded;
126126
/// <inheritdoc />
127-
public event EventHandler<ProxyRequestArgs>? BeforeRequest;
127+
public event AsyncEventHandler<ProxyRequestArgs>? BeforeRequest;
128128
/// <inheritdoc />
129-
public event EventHandler<ProxyResponseArgs>? BeforeResponse;
129+
public event AsyncEventHandler<ProxyResponseArgs>? BeforeResponse;
130130
/// <inheritdoc />
131-
public event EventHandler<ProxyResponseArgs>? AfterResponse;
131+
public event AsyncEventHandler<ProxyResponseArgs>? AfterResponse;
132132
/// <inheritdoc />
133133
public event EventHandler<RequestLogArgs>? AfterRequestLog;
134134
/// <inheritdoc />
@@ -146,8 +146,8 @@ public void RaiseProxyBeforeRequest(ProxyRequestArgs args) {
146146
BeforeRequest?.Invoke(this, args);
147147
}
148148

149-
public void RaiseProxyBeforeResponse(ProxyResponseArgs args) {
150-
BeforeResponse?.Invoke(this, args);
149+
public async Task RaiseProxyBeforeResponse(ProxyResponseArgs args) {
150+
await BeforeResponse?.InvokeAsync(this, args, null);
151151
}
152152

153153
public void RaiseProxyAfterResponse(ProxyResponseArgs args) {

msgraph-developer-proxy-plugins/Guidance/GraphBetaSupportGuidancePlugin.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public override void Register(IPluginEvents pluginEvents,
2020
pluginEvents.AfterResponse += AfterResponse;
2121
}
2222

23-
private void AfterResponse(object? sender, ProxyResponseArgs e) {
23+
private async Task AfterResponse(object? sender, ProxyResponseArgs e) {
2424
Request request = e.Session.HttpClient.Request;
2525
if (_urlsToWatch is not null &&
2626
e.HasRequestUrlMatch(_urlsToWatch) &&

msgraph-developer-proxy-plugins/Guidance/GraphClientRequestIdGuidancePlugin.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public override void Register(IPluginEvents pluginEvents,
2222
pluginEvents.BeforeRequest += BeforeRequest;
2323
}
2424

25-
private void BeforeRequest(object? sender, ProxyRequestArgs e)
25+
private async Task BeforeRequest(object? sender, ProxyRequestArgs e)
2626
{
2727
Request request = e.Session.HttpClient.Request;
2828
if (_urlsToWatch is not null && e.HasRequestUrlMatch(_urlsToWatch) && WarnNoClientRequestId(request))

msgraph-developer-proxy-plugins/Guidance/GraphSdkGuidancePlugin.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public override void Register(IPluginEvents pluginEvents,
2020
pluginEvents.AfterResponse += OnAfterResponse;
2121
}
2222

23-
private void OnAfterResponse(object? sender, ProxyResponseArgs e) {
23+
private async Task OnAfterResponse(object? sender, ProxyResponseArgs e) {
2424
Request request = e.Session.HttpClient.Request;
2525
// only show the message if there is an error.
2626
if (e.Session.HttpClient.Response.StatusCode >= 400

msgraph-developer-proxy-plugins/Guidance/GraphSelectGuidancePlugin.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public override void Register(IPluginEvents pluginEvents,
2020
pluginEvents.AfterResponse += AfterResponse;
2121
}
2222

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

msgraph-developer-proxy-plugins/Guidance/ODataPagingGuidancePlugin.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public override void Register(IPluginEvents pluginEvents,
2525
pluginEvents.BeforeResponse += OnBeforeResponse;
2626
}
2727

28-
private void OnBeforeRequest(object? sender, ProxyRequestArgs e)
28+
private async Task OnBeforeRequest(object? sender, ProxyRequestArgs e)
2929
{
3030
if (_urlsToWatch is null ||
3131
e.Session.HttpClient.Request.Method != "GET" ||
@@ -41,7 +41,7 @@ private void OnBeforeRequest(object? sender, ProxyRequestArgs e)
4141
}
4242
}
4343

44-
private async void OnBeforeResponse(object? sender, ProxyResponseArgs e)
44+
private async Task OnBeforeResponse(object? sender, ProxyResponseArgs e)
4545
{
4646
if (_urlsToWatch is null ||
4747
!e.HasRequestUrlMatch(_urlsToWatch) ||

msgraph-developer-proxy-plugins/MockResponses/MockResponsePlugin.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ private void OnOptionsLoaded(object? sender, OptionsLoadedArgs e) {
8080
_loader?.InitResponsesWatcher();
8181
}
8282

83-
private void OnRequest(object? sender, ProxyRequestArgs e) {
83+
private async Task OnRequest(object? sender, ProxyRequestArgs e) {
8484
Request request = e.Session.HttpClient.Request;
8585
ResponseState state = e.ResponseState;
8686
if (!_configuration.NoMocks && _urlsToWatch is not null && e.ShouldExecute(_urlsToWatch)) {

msgraph-developer-proxy-plugins/RandomErrors/GenericRandomErrorPlugin.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ private void OnInit(object? sender, InitArgs e) {
114114
_loader?.InitResponsesWatcher();
115115
}
116116

117-
private void OnRequest(object? sender, ProxyRequestArgs e) {
117+
private async Task OnRequest(object? sender, ProxyRequestArgs e) {
118118
var session = e.Session;
119119
var state = e.ResponseState;
120120
if (!e.ResponseState.HasBeenSet

msgraph-developer-proxy-plugins/RandomErrors/GraphRandomErrorPlugin.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ private void OnOptionsLoaded(object? sender, OptionsLoadedArgs e) {
205205
}
206206
}
207207

208-
private void OnRequest(object? sender, ProxyRequestArgs e) {
208+
private async Task OnRequest(object? sender, ProxyRequestArgs e) {
209209
var session = e.Session;
210210
var state = e.ResponseState;
211211
if (!e.ResponseState.HasBeenSet

0 commit comments

Comments
 (0)