DamageAssessment_Backend/DamageAssesmentApi/DamageAssesment.Api.SurveyResponses/Providers/RegionServiceProvider.cs

35 lines
1.4 KiB
C#
Raw Normal View History

2023-08-15 22:52:30 -05:00
using DamageAssesment.Api.SurveyResponses.Bases;
using DamageAssesment.Api.SurveyResponses.Interfaces;
using DamageAssesment.Api.SurveyResponses.Models;
using Newtonsoft.Json;
namespace DamageAssesment.Api.SurveyResponses.Providers
{
public class RegionServiceProvider : ServiceProviderBase, IRegionServiceProvider
{
public RegionServiceProvider(IConfiguration configuration, HttpClient httpClient, ILogger<RegionServiceProvider> logger) : base(configuration, httpClient, logger, "/api/Regions", configuration.GetValue<string>("EndPointSettings:LocationUrlBase"))
{
}
public async Task<List<Region>> getRegionsAsync()
{
try
{
httpClient.BaseAddress = new Uri(urlBase);
var response = await httpClient.GetAsync(ressource);
response.EnsureSuccessStatusCode();
var responseString = await response.Content.ReadAsStringAsync();
var regions = JsonConvert.DeserializeObject<List<Region>>(responseString);
if (regions == null || !regions.Any())
return null;
else return regions;
}
catch (Exception ex)
{
logger?.LogError($"Exception Found : {ex.Message} - Ref: RegionServiceProvider.getRegionsAsync()");
return null;
}
}
}
}