-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathEventsExtension.cs
51 lines (46 loc) · 1.69 KB
/
EventsExtension.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
using System;
using System.Net.Http;
using System.Threading.Tasks;
namespace RingCentral.Net.Events
{
public class HttpMessages
{
public HttpRequestMessage httpRequestMessage;
public HttpResponseMessage httpResponseMessage;
public override string ToString()
{
return Utils.FormatHttpMessage(httpResponseMessage, httpRequestMessage);
}
}
public class EventsExtension : SdkExtension
{
public event EventHandler<HttpRequestMessage> BeforeRequest;
public event EventHandler<HttpMessages> RequestSuccess;
public event EventHandler<RestException> RequestError;
public override Task Install(RestClient rc)
{
var extensibleRequest = rc.extensibleRequest;
rc.extensibleRequest = async (httpRequestMessage, restRequestConfig) =>
{
if (!enabled) return await extensibleRequest(httpRequestMessage, restRequestConfig);
BeforeRequest?.Invoke(this, httpRequestMessage);
try
{
var httpResponseMessage = await extensibleRequest(httpRequestMessage, restRequestConfig);
RequestSuccess?.Invoke(this, new HttpMessages
{
httpResponseMessage = httpResponseMessage,
httpRequestMessage = httpRequestMessage
});
return httpResponseMessage;
}
catch (RestException re)
{
RequestError?.Invoke(this, re);
throw;
}
};
return Task.CompletedTask;
}
}
}