22
22
using Microsoft . OpenApi . Services ;
23
23
using Microsoft . OpenApi . Validations ;
24
24
using Microsoft . OpenApi . Writers ;
25
+ using System . Threading ;
25
26
26
27
namespace Microsoft . OpenApi . Hidi
27
28
{
@@ -38,7 +39,8 @@ public static async Task ProcessOpenApiDocument(
38
39
bool resolveexternal ,
39
40
string filterbyoperationids ,
40
41
string filterbytags ,
41
- string filterbycollection
42
+ string filterbycollection ,
43
+ CancellationToken cancellationToken
42
44
)
43
45
{
44
46
var logger = ConfigureLoggerInstance ( loglevel ) ;
@@ -52,7 +54,11 @@ string filterbycollection
52
54
}
53
55
catch ( ArgumentNullException ex )
54
56
{
55
- logger . LogError ( ex . Message ) ;
57
+ #if DEBUG
58
+ logger . LogCritical ( ex , ex . Message ) ;
59
+ #else
60
+ logger . LogCritical ( ex . Message ) ;
61
+ #endif
56
62
return ;
57
63
}
58
64
try
@@ -64,19 +70,27 @@ string filterbycollection
64
70
}
65
71
catch ( ArgumentException ex )
66
72
{
67
- logger . LogError ( ex . Message ) ;
73
+ #if DEBUG
74
+ logger . LogCritical ( ex , ex . Message ) ;
75
+ #else
76
+ logger . LogCritical ( ex . Message ) ;
77
+ #endif
68
78
return ;
69
79
}
70
80
try
71
81
{
72
82
if ( output . Exists )
73
83
{
74
- throw new IOException ( "The file you're writing to already exists. Please input a new file path." ) ;
84
+ throw new IOException ( $ "The file { output } already exists. Please input a new file path.") ;
75
85
}
76
86
}
77
87
catch ( IOException ex )
78
88
{
79
- logger . LogError ( ex . Message ) ;
89
+ #if DEBUG
90
+ logger . LogCritical ( ex , ex . Message ) ;
91
+ #else
92
+ logger . LogCritical ( ex . Message ) ;
93
+ #endif
80
94
return ;
81
95
}
82
96
@@ -91,12 +105,12 @@ string filterbycollection
91
105
openApiFormat = format ?? GetOpenApiFormat ( csdl , logger ) ;
92
106
version ??= OpenApiSpecVersion . OpenApi3_0 ;
93
107
94
- stream = await GetStream ( csdl , logger ) ;
108
+ stream = await GetStream ( csdl , logger , cancellationToken ) ;
95
109
document = await ConvertCsdlToOpenApi ( stream ) ;
96
110
}
97
111
else
98
112
{
99
- stream = await GetStream ( openapi , logger ) ;
113
+ stream = await GetStream ( openapi , logger , cancellationToken ) ;
100
114
101
115
// Parsing OpenAPI file
102
116
stopwatch . Start ( ) ;
@@ -156,7 +170,7 @@ string filterbycollection
156
170
}
157
171
if ( ! string . IsNullOrEmpty ( filterbycollection ) )
158
172
{
159
- var fileStream = await GetStream ( filterbycollection , logger ) ;
173
+ var fileStream = await GetStream ( filterbycollection , logger , cancellationToken ) ;
160
174
var requestUrls = ParseJsonCollectionFile ( fileStream , logger ) ;
161
175
162
176
logger . LogTrace ( "Creating predicate based on the paths and Http methods defined in the Postman collection." ) ;
@@ -245,7 +259,7 @@ public static OpenApiDocument FixReferences(OpenApiDocument document)
245
259
return doc ;
246
260
}
247
261
248
- private static async Task < Stream > GetStream ( string input , ILogger logger )
262
+ private static async Task < Stream > GetStream ( string input , ILogger logger , CancellationToken cancellationToken )
249
263
{
250
264
var stopwatch = new Stopwatch ( ) ;
251
265
stopwatch . Start ( ) ;
@@ -263,11 +277,15 @@ private static async Task<Stream> GetStream(string input, ILogger logger)
263
277
{
264
278
DefaultRequestVersion = HttpVersion . Version20
265
279
} ;
266
- stream = await httpClient . GetStreamAsync ( input ) ;
280
+ stream = await httpClient . GetStreamAsync ( input , cancellationToken ) ;
267
281
}
268
282
catch ( HttpRequestException ex )
269
283
{
270
- logger . LogError ( $ "Could not download the file at { input } , reason{ ex } ") ;
284
+ #if DEBUG
285
+ logger . LogCritical ( ex , $ "Could not download the file at { input } , reason: { ex . Message } ") ;
286
+ #else
287
+ logger . LogCritical ( $ "Could not download the file at { input } , reason: { ex . Message } ", input , ex . Message ) ;
288
+ #endif
271
289
return null ;
272
290
}
273
291
}
@@ -286,7 +304,11 @@ ex is UnauthorizedAccessException ||
286
304
ex is SecurityException ||
287
305
ex is NotSupportedException )
288
306
{
289
- logger . LogError ( $ "Could not open the file at { input } , reason: { ex . Message } ") ;
307
+ #if DEBUG
308
+ logger . LogCritical ( ex , $ "Could not open the file at { input } , reason: { ex . Message } ") ;
309
+ #else
310
+ logger . LogCritical ( $ "Could not open the file at { input } , reason: { ex . Message } ") ;
311
+ #endif
290
312
return null ;
291
313
}
292
314
}
@@ -327,14 +349,14 @@ public static Dictionary<string, List<string>> ParseJsonCollectionFile(Stream st
327
349
return requestUrls ;
328
350
}
329
351
330
- internal static async Task ValidateOpenApiDocument ( string openapi , LogLevel loglevel )
352
+ internal static async Task ValidateOpenApiDocument ( string openapi , LogLevel loglevel , CancellationToken cancellationToken )
331
353
{
332
354
if ( string . IsNullOrEmpty ( openapi ) )
333
355
{
334
356
throw new ArgumentNullException ( nameof ( openapi ) ) ;
335
357
}
336
358
var logger = ConfigureLoggerInstance ( loglevel ) ;
337
- var stream = await GetStream ( openapi , logger ) ;
359
+ var stream = await GetStream ( openapi , logger , cancellationToken ) ;
338
360
339
361
OpenApiDocument document ;
340
362
logger . LogTrace ( "Parsing the OpenApi file" ) ;
@@ -369,16 +391,16 @@ private static OpenApiFormat GetOpenApiFormat(string input, ILogger logger)
369
391
private static ILogger ConfigureLoggerInstance ( LogLevel loglevel )
370
392
{
371
393
// Configure logger options
372
- #if DEBUG
394
+ #if DEBUG
373
395
loglevel = loglevel > LogLevel . Debug ? LogLevel . Debug : loglevel ;
374
- #endif
396
+ #endif
375
397
376
398
var logger = LoggerFactory . Create ( ( builder ) => {
377
399
builder
378
400
. AddConsole ( )
379
- #if DEBUG
401
+ #if DEBUG
380
402
. AddDebug ( )
381
- #endif
403
+ #endif
382
404
. SetMinimumLevel ( loglevel ) ;
383
405
} ) . CreateLogger < OpenApiService > ( ) ;
384
406
0 commit comments