Skip to content

Commit

Permalink
Return an empty list if product groups are not supported (#681)
Browse files Browse the repository at this point in the history
Co-authored-by: Guy Fankam <[email protected]>
  • Loading branch information
guythetechie and Guy Fankam authored Oct 9, 2024
1 parent fedbca2 commit 167a8de
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions tools/code/common/ProductGroup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;

Expand Down Expand Up @@ -122,10 +124,19 @@ public sealed record ProductGroupDto

public static class ProductGroupModule
{
public static IAsyncEnumerable<GroupName> ListNames(this ProductGroupsUri uri, HttpPipeline pipeline, CancellationToken cancellationToken) =>
pipeline.ListJsonObjects(uri.ToUri(), cancellationToken)
public static IAsyncEnumerable<GroupName> ListNames(this ProductGroupsUri uri, HttpPipeline pipeline, CancellationToken cancellationToken)
{
var exceptionHandler = (HttpRequestException exception) =>
exception.StatusCode == HttpStatusCode.BadRequest
&& exception.Message.Contains("MethodNotAllowedInPricingTier", StringComparison.OrdinalIgnoreCase)
? AsyncEnumerable.Empty<GroupName>()
: throw exception;

return pipeline.ListJsonObjects(uri.ToUri(), cancellationToken)
.Select(jsonObject => jsonObject.GetStringProperty("name"))
.Select(GroupName.From);
.Select(GroupName.From)
.Catch(exceptionHandler);
}

public static IAsyncEnumerable<(GroupName Name, ProductGroupDto Dto)> List(this ProductGroupsUri productGroupsUri, HttpPipeline pipeline, CancellationToken cancellationToken) =>
productGroupsUri.ListNames(pipeline, cancellationToken)
Expand Down

0 comments on commit 167a8de

Please sign in to comment.