Files
FsToolbox/Cli/Tasks/RegionTasks.cs

53 lines
2.0 KiB
C#

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
/// <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)
{
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<Pickup>
var opts = new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true,
PropertyNamingPolicy = null // <── WICHTIG
};
return root["stores"].Deserialize<List<Store>>(opts) ?? [];
}
#endregion
}
}