68 lines
2.8 KiB
C#
68 lines
2.8 KiB
C#
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<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
|
|
{
|
|
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<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;
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
}
|