using System.Text.Json;
using System.Text.Json.Nodes;
using FsToolbox.Cli.Helper;
using NLog;
namespace FsToolbox.Cli.Tasks
{
public static partial class RegionTasks
{
private static readonly Logger Logger = LoggingService.GetLogger(nameof(RegionTasks));
#region Public Method GetStoresInRegionAsync
///
/// Retrieves all stores within the specified region, ensuring the request is authenticated.
///
/// The HTTP client used to perform the request.
/// The region identifier to query.
/// A list of stores, or an empty list when the call fails or returns no data.
public static async Task> 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)
{
Logger.Error("Region stores retrieval failed ({Status} {Reason}): {Body}", (int)response.StatusCode, response.ReasonPhrase, responseBody);
return [];
}
Logger.Info("Stores in region {RegionId}:", regionId);
Logger.Info(responseBody);
var root = JsonNode.Parse(responseBody);
if (root == null) return [];
// Deserialize JsonNode to List
var opts = new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true,
PropertyNamingPolicy = null // <── WICHTIG
};
return root["stores"].Deserialize>(opts) ?? [];
}
#endregion
}
}