22using Flurl ;
33using Microsoft . Extensions . Logging ;
44using Microsoft . OpenApi ;
5- using Microsoft . OpenApi . Extensions ;
6- using Microsoft . OpenApi . Readers ;
7- using SharpYaml . Model ;
85using System ;
96using System . Collections . Generic ;
107using System . IO ;
118using System . Linq ;
12- using System . Text . Json ;
139using System . Threading ;
1410using System . Threading . Tasks ;
1511using YamlDotNet . Serialization ;
@@ -24,8 +20,17 @@ public static async ValueTask ExportAll(ServiceDirectory serviceDirectory, Servi
2420 await List ( serviceUri , listRestResources , cancellationToken )
2521 // Filter out apis that should not be exported
2622 . Where ( apiName => ShouldExport ( apiName , apiNamesToExport ) )
27- // Export APIs in parallel
28- . ForEachParallel ( async apiName => await Export ( serviceDirectory , serviceUri , apiName , defaultSpecification , listRestResources , getRestResource , downloadResource , logger , cancellationToken ) ,
23+ // Group APIs by version set (https://github.com/Azure/apiops/issues/316).
24+ // We'll process each group in parallel, but each API within a group sequentially.
25+ . SelectAwait ( async apiName =>
26+ {
27+ var model = await GetModel ( serviceUri , apiName , getRestResource , cancellationToken ) ;
28+ return ( Name : apiName , Model : model ) ;
29+ } )
30+ . GroupBy ( api => api . Model . Properties . ApiVersionSetId )
31+ // Export each group in parallel
32+ . ForEachParallel ( async group => await group . ForEachAwaitAsync ( async api => await Export ( serviceDirectory , serviceUri , api . Name , api . Model , defaultSpecification , listRestResources , getRestResource , downloadResource , logger , cancellationToken ) ,
33+ cancellationToken ) ,
2934 cancellationToken ) ;
3035 }
3136
@@ -48,17 +53,24 @@ private static bool ShouldExport(ApiName apiName, IEnumerable<string>? apiNamesT
4853 StringComparison . OrdinalIgnoreCase ) ) ;
4954 }
5055
51- private static async ValueTask Export ( ServiceDirectory serviceDirectory , ServiceUri serviceUri , ApiName apiName , DefaultApiSpecification defaultSpecification , ListRestResources listRestResources , GetRestResource getRestResource , DownloadResource downloadResource , ILogger logger , CancellationToken cancellationToken )
56+ private static async ValueTask < ApiModel > GetModel ( ServiceUri serviceUri , ApiName apiName , GetRestResource getRestResource , CancellationToken cancellationToken )
57+ {
58+ var apisUri = new ApisUri ( serviceUri ) ;
59+ var apiUri = new ApiUri ( apiName , apisUri ) ;
60+
61+ var apiResponseJson = await getRestResource ( apiUri . Uri , cancellationToken ) ;
62+
63+ return ApiModel . Deserialize ( apiName , apiResponseJson ) ;
64+ }
65+
66+ private static async ValueTask Export ( ServiceDirectory serviceDirectory , ServiceUri serviceUri , ApiName apiName , ApiModel apiModel , DefaultApiSpecification defaultSpecification , ListRestResources listRestResources , GetRestResource getRestResource , DownloadResource downloadResource , ILogger logger , CancellationToken cancellationToken )
5267 {
5368 var apisDirectory = new ApisDirectory ( serviceDirectory ) ;
5469 var apiDirectory = new ApiDirectory ( apiName , apisDirectory ) ;
5570
5671 var apisUri = new ApisUri ( serviceUri ) ;
5772 var apiUri = new ApiUri ( apiName , apisUri ) ;
5873
59- var apiResponseJson = await getRestResource ( apiUri . Uri , cancellationToken ) ;
60- var apiModel = ApiModel . Deserialize ( apiName , apiResponseJson ) ;
61-
6274 await ExportInformationFile ( apiModel , apiDirectory , logger , cancellationToken ) ;
6375 await ExportSpecification ( apiModel , apiDirectory , apiUri , defaultSpecification , getRestResource , downloadResource , logger , cancellationToken ) ;
6476 await ExportTags ( apiDirectory , apiUri , listRestResources , logger , cancellationToken ) ;
0 commit comments