private static ListGateways GetListGateways()

in tools/code/extractor/Gateway.cs [69:102]


    private static ListGateways GetListGateways(IServiceProvider provider)
    {
        var findConfigurationNamesFactory = provider.GetRequiredService<FindConfigurationNamesFactory>();
        var serviceUri = provider.GetRequiredService<ManagementServiceUri>();
        var pipeline = provider.GetRequiredService<HttpPipeline>();

        var findConfigurationNames = findConfigurationNamesFactory.Create<GatewayName>();

        return cancellationToken =>
            findConfigurationNames()
                .Map(names => listFromSet(names, cancellationToken))
                .IfNone(() => listAll(cancellationToken));

        IAsyncEnumerable<(GatewayName, GatewayDto)> listFromSet(IEnumerable<GatewayName> names, CancellationToken cancellationToken) =>
            names.Select(name => GatewayUri.From(name, serviceUri))
                 .ToAsyncEnumerable()
                 .Choose(async uri =>
                 {
                     var dtoOption = await uri.TryGetDto(pipeline, cancellationToken);
                     return dtoOption.Map(dto => (uri.Name, dto));
                 })
                 // Handle scenarios where the SKU doesn't support gateways
                 .Catch((HttpRequestException exception) =>
                            exception.StatusCode == HttpStatusCode.InternalServerError
                            && exception.Message.Contains("Request processing failed", StringComparison.OrdinalIgnoreCase)
                                ? AsyncEnumerable.Empty<(GatewayName, GatewayDto)>()
                                : throw exception);

        IAsyncEnumerable<(GatewayName, GatewayDto)> listAll(CancellationToken cancellationToken)
        {
            var gatewaysUri = GatewaysUri.From(serviceUri);
            return gatewaysUri.List(pipeline, cancellationToken);
        }
    }