Files
FsToolbox/Cli/Tasks/RegionTasks.cs
2025-12-12 08:23:19 +01:00

50 lines
1.8 KiB
C#

using System.Text.Json;
using System.Text.Json.Nodes;
using FsToolbox.Cli.Helper;
namespace FsToolbox.Cli.Tasks
{
public static partial class RegionTasks
{
#region Public Method GetStoresInRegionAsync
/// <summary>
/// Retrieves all stores within the specified region, ensuring the request is authenticated.
/// </summary>
/// <param name="httpClient">The HTTP client used to perform the request.</param>
/// <param name="regionId">The region identifier to query.</param>
/// <returns>A list of stores, or an empty list when the call fails or returns no data.</returns>
public static async Task<List<Store>> GetStoresInRegionAsync(HttpClient httpClient, int regionId)
{
await AuthHelper.EnsureAuthenticationAsync(httpClient);
var uri = string.Format(Endpoints.RegionStores, regionId);
var response = await httpClient.GetAsync(uri);
var responseBody = await response.Content.ReadAsStringAsync();
// handle unsuccessful response
if (!response.IsSuccessStatusCode)
{
await Console.Error.WriteLineAsync($"Region stores retrieval failed ({(int)response.StatusCode} {response.ReasonPhrase}): {responseBody}");
return [];
}
Console.WriteLine($"Stores in region {regionId}:");
Console.WriteLine(responseBody);
var root = JsonNode.Parse(responseBody);
if (root == null) return [];
// Deserialize JsonNode to List<Pickup>
var opts = new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true,
PropertyNamingPolicy = null // <── WICHTIG
};
return root["stores"].Deserialize<List<Store>>(opts) ?? [];
}
#endregion
}
}