using DamageAssesment.Api.SurveyResponses.Bases; using DamageAssesment.Api.SurveyResponses.Interfaces; using DamageAssesment.Api.SurveyResponses.Models; using Newtonsoft.Json; using System.Reflection; namespace DamageAssesment.Api.SurveyResponses.Providers { public class SurveyServiceProvider :ServiceProviderBase, ISurveyServiceProvider { public SurveyServiceProvider(IConfiguration configuration, HttpClient httpClient, ILogger logger) : base(configuration, httpClient, logger, "/api/Surveys", configuration.GetValue("EndPointSettings:SurveyUrlBase")) { } public async Task> getSurveysAsync() { try { httpClient.BaseAddress = new Uri(urlBase); var response = await httpClient.GetAsync(ressource); response.EnsureSuccessStatusCode(); var responseString = await response.Content.ReadAsStringAsync(); var surveys = JsonConvert.DeserializeObject>(responseString); if (surveys == null || !surveys.Any()) return null; else return surveys; } catch (Exception ex) { logger?.LogError($"Exception Found : {ex.Message} - Ref: SurveyServiceProvider.getSurveysAsync()"); return null; } } public async Task getSurveyAsync(int surveyId) { try { httpClient.BaseAddress = new Uri(urlBase); var response = await httpClient.GetAsync(ressource+"/"+ surveyId); response.EnsureSuccessStatusCode(); var responseString = await response.Content.ReadAsStringAsync(); var survey = JsonConvert.DeserializeObject(responseString); if (survey == null ) return null; else return survey; } catch (Exception ex) { logger?.LogError($"Exception Found : {ex.Message} - Ref: SurveyServiceProvider.getSurveyAsync()"); return null; } } } }