2023-08-15 22:52:30 -05:00
|
|
|
|
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;
|
2023-08-27 10:55:58 -05:00
|
|
|
|
using System.Net.Http.Headers;
|
2023-08-15 22:52:30 -05:00
|
|
|
|
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<AttachmentServiceProvider> logger) : base(configuration, httpClient, logger, "/api/Attachments", configuration.GetValue<string>("EndPointSettings:AttachmentUrlBase"))
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<List<Attachment>> 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<List<Attachment>>(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<IEnumerable<Attachment>> PostAttachmentsAsync(AttachmentInfo attachmentInfo)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2023-08-27 10:55:58 -05:00
|
|
|
|
|
|
|
|
|
var url = urlBase + ressource;
|
|
|
|
|
var request = new HttpRequestMessage(HttpMethod.Post, url);
|
|
|
|
|
request.Headers.Accept.Clear();
|
|
|
|
|
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
|
|
|
|
|
//request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token);
|
|
|
|
|
|
2023-08-15 22:52:30 -05:00
|
|
|
|
var jsonObject = JsonConvert.SerializeObject(attachmentInfo);
|
2023-08-27 10:55:58 -05:00
|
|
|
|
request.Content = new StringContent(jsonObject.ToString(), Encoding.UTF8, "application/json");
|
|
|
|
|
var response = await httpClient.SendAsync(request, CancellationToken.None);
|
2023-08-15 22:52:30 -05:00
|
|
|
|
response.EnsureSuccessStatusCode();
|
|
|
|
|
var responseString = await response.Content.ReadAsStringAsync();
|
|
|
|
|
var attachments = JsonConvert.DeserializeObject<IEnumerable<Attachment>>(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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|