@@ -12,51 +12,46 @@ namespace GitHubConnector.Middleware;
1212/// <summary>
1313/// Middleware handler for asynchronous creation of connector schema.
1414/// </summary>
15- public partial class PollForSchemaStatusHandler : DelegatingHandler
15+ /// <remarks>
16+ /// Initializes a new instance of the <see cref="PollForSchemaStatusHandler"/> class.
17+ /// </remarks>
18+ /// <param name="delay">The number of milliseconds to wait between poll requests.</param>
19+ public partial class PollForSchemaStatusHandler ( int delay = 60000 ) : DelegatingHandler
1620{
17- // Match URL like:
18- // /external/connections/{connection-id}/schema
19- private static readonly Regex PostSchemaRegex =
20- new ( "\\ /external\\ /connections\\ /[0-9a-zA-Z]+\\ /schema" , RegexOptions . IgnoreCase ) ;
21-
22- // Match URL like:
23- // /external/connections/{connection-id}/operations/{operation-id}
24- private static readonly Regex GetOperationRegex =
25- new ( "\\ /external\\ /connections\\ /[0-9a-zA-Z]+\\ /operations\\ /.*" , RegexOptions . IgnoreCase ) ;
26-
27- /// <summary>
28- /// Initializes a new instance of the <see cref="PollForSchemaStatusHandler"/> class.
29- /// </summary>
30- /// <param name="delay">The number of milliseconds to wait between poll requests.</param>
31- public PollForSchemaStatusHandler ( int delay = 60000 )
32- {
33- Delay = delay ;
34- }
35-
3621 /// <summary>
3722 /// Gets the delay setting in milliseconds.
3823 /// </summary>
39- public int Delay { get ; private set ; }
24+ public int Delay { get ; private set ; } = delay ;
4025
4126 /// <inheritdoc/>
4227 protected override Task < HttpResponseMessage > SendAsync (
4328 HttpRequestMessage request , CancellationToken cancellationToken )
4429 {
4530 if ( request . Method == HttpMethod . Patch &&
46- PostSchemaRegex . IsMatch ( request . RequestUri ? . AbsolutePath ?? string . Empty ) )
31+ PostSchemaRegex ( ) . IsMatch ( request . RequestUri ? . AbsolutePath ?? string . Empty ) )
4732 {
4833 return HandlePatchSchemaRequestAsync ( request , cancellationToken ) ;
4934 }
5035
5136 if ( request . Method == HttpMethod . Get &&
52- GetOperationRegex . IsMatch ( request . RequestUri ? . AbsolutePath ?? string . Empty ) )
37+ GetOperationRegex ( ) . IsMatch ( request . RequestUri ? . AbsolutePath ?? string . Empty ) )
5338 {
5439 return HandleGetOperationStatusRequestAsync ( request , cancellationToken ) ;
5540 }
5641
5742 return base . SendAsync ( request , cancellationToken ) ;
5843 }
5944
45+ // Match URL like:
46+ // /external/connections/{connection-id}/operations/{operation-id}
47+ [ GeneratedRegex ( "\\ /external\\ /connections\\ /[0-9a-zA-Z]+\\ /operations\\ /.*" , RegexOptions . IgnoreCase ) ]
48+ private static partial Regex GetOperationRegex ( ) ;
49+
50+ // Match URL like:
51+ // /external/connections/{connection-id}/schema
52+ [ GeneratedRegex ( "\\ /external\\ /connections\\ /[0-9a-zA-Z]+\\ /schema" , RegexOptions . IgnoreCase ) ]
53+ private static partial Regex PostSchemaRegex ( ) ;
54+
6055 private async Task < HttpResponseMessage > HandlePatchSchemaRequestAsync (
6156 HttpRequestMessage request , CancellationToken cancellationToken )
6257 {
@@ -69,7 +64,7 @@ private async Task<HttpResponseMessage> HandlePatchSchemaRequestAsync(
6964 if ( location is not null )
7065 {
7166 Console . WriteLine ( $ "Waiting { Delay } ms to poll { location . AbsoluteUri } ") ;
72- await Task . Delay ( Delay ) ;
67+ await Task . Delay ( Delay , cancellationToken ) ;
7368
7469 request . RequestUri = location ;
7570 request . Method = HttpMethod . Get ;
@@ -96,17 +91,17 @@ private async Task<HttpResponseMessage> HandleGetOperationStatusRequestAsync(
9691 if ( response . IsSuccessStatusCode )
9792 {
9893 // Use Graph SDK's parsers to deserialize the body
99- var body = await response . Content . ReadAsStringAsync ( ) ;
94+ var body = await response . Content . ReadAsStringAsync ( cancellationToken ) ;
10095 using var responseBody = new MemoryStream ( System . Text . Encoding . UTF8 . GetBytes ( body ) ) ;
101- var parseNode = ParseNodeFactoryRegistry . DefaultInstance . GetRootParseNode ( "application/json" , responseBody ) ;
96+ var parseNode = await ParseNodeFactoryRegistry . DefaultInstance . GetRootParseNodeAsync ( "application/json" , responseBody , cancellationToken ) ;
10297 var operation = parseNode . GetObjectValue ( ConnectionOperation . CreateFromDiscriminatorValue ) ??
10398 throw new ServiceException ( "Could not get operation from API." ) ;
10499
105100 if ( operation . Status == ConnectionOperationStatus . Inprogress )
106101 {
107102 // Schema registration is in progress, poll again
108103 Console . WriteLine ( $ "Waiting { Delay } ms to poll { request . RequestUri ? . AbsoluteUri } ") ;
109- await Task . Delay ( Delay ) ;
104+ await Task . Delay ( Delay , cancellationToken ) ;
110105 return await SendAsync ( request , cancellationToken ) ;
111106 }
112107
0 commit comments