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 QuestionServiceProvider : ServiceProviderBase, IQuestionServiceProvider { public QuestionServiceProvider(IConfiguration configuration, HttpClient httpClient, ILogger logger) : base(configuration, httpClient, logger, "/api/Questions", configuration.GetValue("EndPointSettings:QuestionUrlBase")) { } public async Task> getQuestionsAsync() { try { httpClient.BaseAddress = new Uri(urlBase); var response = await httpClient.GetAsync(ressource); response.EnsureSuccessStatusCode(); var responseString = await response.Content.ReadAsStringAsync(); var questions = JsonConvert.DeserializeObject>(responseString); if (questions == null || !questions.Any()) return null; else return questions; } catch (Exception ex) { logger?.LogError($"Exception Found : {ex.Message} - Ref: QuestionServiceProvider.getQuestionsAsync()"); return null; } } public async Task> getSurveyQuestionsAsync(int surveyId) { try { httpClient.BaseAddress = new Uri(urlBase); var response = await httpClient.GetAsync("/api/GetSurveyQuestions/" + surveyId); response.EnsureSuccessStatusCode(); var responseString = await response.Content.ReadAsStringAsync(); var questions = JsonConvert.DeserializeObject>(responseString); if (questions == null || !questions.Any()) return null; else return questions; } catch (Exception ex) { logger?.LogError($"Exception Found : {ex.Message} - Ref: QuestionServiceProvider.getSurveyQuestionsAsync()"); return null; } } public async Task getQuestionsAsync(int questionId) { try { httpClient.BaseAddress = new Uri(urlBase); var response = await httpClient.GetAsync("/api/Questions/" + questionId); response.EnsureSuccessStatusCode(); var responseString = await response.Content.ReadAsStringAsync(); var question = JsonConvert.DeserializeObject(responseString); if (question == null) return null; else return question; } catch (Exception ex) { logger?.LogError($"Exception Found : {ex.Message} - Ref: QuestionServiceProvider.getQuestionsAsync(questionId)"); return null; } } } }