using DamageAssesment.Api.SurveyResponses.Bases; using DamageAssesment.Api.SurveyResponses.Interfaces; using DamageAssesment.Api.SurveyResponses.Models; using Microsoft.Extensions.Logging; using Newtonsoft.Json; using System.Net.Http; using System.Runtime.Intrinsics.Arm; using System.Text; namespace DamageAssesment.Api.SurveyResponses.Providers { public class AttachmentServiceProvider : ServiceProviderBase, IAttachmentServiceProvider { public AttachmentServiceProvider(IConfiguration configuration, HttpClient httpClient, ILogger logger) : base(configuration, httpClient, logger, "/api/Attachments", configuration.GetValue("EndPointSettings:AttachmentUrlBase")) { } public async Task> getAttachmentsAsync() { try { httpClient.BaseAddress = new Uri(urlBase); var response = await httpClient.GetAsync(ressource); response.EnsureSuccessStatusCode(); var responseString = await response.Content.ReadAsStringAsync(); var attachments = JsonConvert.DeserializeObject>(responseString); if (attachments == null || !attachments.Any()) return null; else return attachments; } catch (Exception ex) { logger?.LogError($"Exception Found : {ex.Message} - Ref: AttachmentServiceProvider.getAttachmentsAsync()"); return null; } } public async Task> PostAttachmentsAsync(AttachmentInfo attachmentInfo) { try { httpClient.BaseAddress = new Uri(urlBase); var jsonObject = JsonConvert.SerializeObject(attachmentInfo); var content = new StringContent(jsonObject.ToString(), Encoding.UTF8, "application/json"); var response = await httpClient.PostAsync(ressource, content); response.EnsureSuccessStatusCode(); var responseString = await response.Content.ReadAsStringAsync(); var attachments = JsonConvert.DeserializeObject>(responseString); if (attachments == null) { logger?.LogError($"Attachments cannot be added - Ref: AttachmentServiceProvider.PostAttachmentsAsync()"); return null; } else return attachments; } catch (Exception ex) { logger?.LogError($"Exception Found : {ex.Message} - Ref: AttachmentServiceProvider.PostAttachmentsAsync()"); return null; } } } }