This commit is contained in:
Zeeshaun
2026-03-13 09:11:35 -05:00
parent f004c8205a
commit c2d6bda458
197 changed files with 14970 additions and 0 deletions
@@ -0,0 +1,105 @@
using LCU.Library.Internal.Exceptions;
using Newtonsoft.Json;
using Serilog;
using System;
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using JsonSerializer = System.Text.Json.JsonSerializer;
namespace LCU.API.Internal.DocumentLib.Utilities;
public class HttpUtilities
{
public static HttpClient CreateClient(Uri uri)
{
var handler = new HttpClientHandler
{
SslProtocols = System.Security.Authentication.SslProtocols.Tls12 |
System.Security.Authentication.SslProtocols.Tls11 |
System.Security.Authentication.SslProtocols.Tls,
ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => true
};
var httpClient = new HttpClient(handler)
{
Timeout = TimeSpan.FromSeconds(60),
BaseAddress = uri
};
return httpClient;
}
public static async Task PostAsync<TRequest>(Uri uri, TRequest request)
{
try
{
var client = CreateClient(uri);
var jsonBody = JsonSerializer.Serialize(request);
var content = new StringContent(jsonBody, Encoding.UTF8, "application/json");
var response = await client.PostAsync(uri, content);
if (!response.IsSuccessStatusCode)
{
throw new ServiceException($"StatusCode:{response.StatusCode}, " +
$"Content: {response.Content.ReadAsStringAsync().Result}");
}
}
catch (Exception exception)
{
Log.Error(exception, "Failure during PostAsync.");
throw new ServiceException($"Failure during Document Upload. Exception Message: {exception.Message}");
}
}
public static async Task<TResponse> PostAsync<TRequest, TResponse>(Uri uri, TRequest request)
{
try
{
var client = CreateClient(uri);
var jsonBody = JsonSerializer.Serialize(request);
var content = new StringContent(jsonBody, Encoding.UTF8, "application/json");
var response = await client.PostAsync(uri, content);
if (!response.IsSuccessStatusCode)
{
throw new ServiceException($"StatusCode:{response.StatusCode}, " +
$"Content: {response.Content.ReadAsStringAsync().Result}");
}
var stringResponse = await response.Content.ReadAsStringAsync();
return JsonSerializer.Deserialize<TResponse>(stringResponse, new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
}
catch (Exception exception)
{
Log.Error(exception, "Failure during PostAsync.");
throw new ServiceException($"Failure during PostAsync. Exception Message: {exception.Message}");
}
}
public static async Task<TResponse> GetAsync<TResponse>(string url)
{
try
{
var uri = new Uri(url);
var client = CreateClient(uri);
var response = await client.GetAsync(uri);
if (!response.IsSuccessStatusCode)
{
throw new ServiceException($"StatusCode:{response.StatusCode}, " +
$"Content: {response.Content.ReadAsStringAsync().Result}");
}
var stringResponse = await response.Content.ReadAsStringAsync();
return JsonSerializer.Deserialize<TResponse>(stringResponse, new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
}
catch (Exception exception)
{
Log.Error(exception, "Failure during GetAsync.");
throw new ServiceException($"Failure during GetAsync. Exception Message: {exception.Message}");
}
}
}
@@ -0,0 +1,8 @@
using MongoDB.Driver;
namespace LCU.API.Internal.DocumentLib.Utilities;
public interface IMongoCollectionFactory
{
IMongoCollection<T> GetCollection<T>();
}
@@ -0,0 +1,37 @@
using System;
using LCU.API.Internal.DocumentLib.Configurations;
using LCU.API.Internal.DocumentLib.Containers;
using LCU.API.Internal.DocumentLib.Repositories;
using MongoDB.Driver;
namespace LCU.API.Internal.DocumentLib.Utilities;
public class MongoCollectionFactory : IMongoCollectionFactory
{
private readonly IDataClient _mongoDataClient;
private readonly DatabaseConfiguration _config;
public MongoCollectionFactory(IDataClient mongoDataClient, DatabaseConfiguration config)
{
_mongoDataClient = mongoDataClient;
_config = config;
}
public IMongoCollection<T> GetCollection<T>()
{
return _mongoDataClient
.GetClient()
.GetDatabase(_config.DatabaseName)
.GetCollection<T>(CollectionNameFor<T>());
}
private string CollectionNameFor<T>()
{
return typeof(T) switch
{
var t when t == typeof(DocumentInformation) => _config.DocumentContainerName,
var t when t == typeof(DocumentAudit) => _config.AuditContainerName,
_ => throw new NotImplementedException()
};
}
}