feat: implement ofac file service
This commit is contained in:
@@ -1,5 +1,15 @@
|
||||
using FluentResults;
|
||||
|
||||
namespace SanctionsSearch.Worker.Interfaces;
|
||||
|
||||
interface IOfacFileService
|
||||
{
|
||||
Task<Result<Stream>> GetSdnFileAsync();
|
||||
Task<Result<Stream>> GetAddressFileAsync();
|
||||
Task<Result<Stream>> GetAltNamesFileAsync();
|
||||
Task<Result<Stream>> GetCommentsFileAsync();
|
||||
Task<Result<Stream>> GetConPrimaryNameFileAsync();
|
||||
Task<Result<Stream>> GetConAddressesFileAsync();
|
||||
Task<Result<Stream>> GetConAltNamesFileAsync();
|
||||
Task<Result<Stream>> GetConCommentsFileAsync();
|
||||
}
|
||||
|
||||
@@ -1,3 +1,6 @@
|
||||
|
||||
using FluentResults;
|
||||
|
||||
namespace SanctionsSearch.Worker.Services;
|
||||
|
||||
class OfacFileService(
|
||||
@@ -9,4 +12,37 @@ class OfacFileService(
|
||||
private readonly HttpClient _client = client ?? throw new ArgumentNullException(nameof(client));
|
||||
private readonly ILogger<OfacFileService> _logger = logger ?? throw new ArgumentNullException(nameof(logger));
|
||||
private readonly OfacFileServiceOptions _options = options?.Value ?? throw new ArgumentNullException(nameof(options));
|
||||
private async Task<Result<Stream>> GetFileAsync(Uri fileUri)
|
||||
{
|
||||
try
|
||||
{
|
||||
_logger.LogInformation("Downloading file from {FileUri}", fileUri);
|
||||
|
||||
var response = await _client.GetAsync(fileUri);
|
||||
|
||||
if (response.IsSuccessStatusCode is false)
|
||||
{
|
||||
_logger.LogError("Failed to download file from {FileUri}", fileUri);
|
||||
return Result.Fail($"Failed to download file from {fileUri}");
|
||||
}
|
||||
|
||||
_logger.LogInformation("Downloaded file from {FileUri}", fileUri);
|
||||
var stream = await response.Content.ReadAsStreamAsync();
|
||||
return Result.Ok(stream);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Failed to download file from {FileUri}", fileUri);
|
||||
return Result.Fail($"Failed to download file from {fileUri}");
|
||||
}
|
||||
}
|
||||
|
||||
public Task<Result<Stream>> GetSdnFileAsync() => GetFileAsync(_options.GetSdnFileUri());
|
||||
public Task<Result<Stream>> GetAddressFileAsync() => GetFileAsync(_options.GetAddressFileUri());
|
||||
public Task<Result<Stream>> GetAltNamesFileAsync() => GetFileAsync(_options.GetAltNamesFileUri());
|
||||
public Task<Result<Stream>> GetCommentsFileAsync() => GetFileAsync(_options.GetCommentsFileUri());
|
||||
public Task<Result<Stream>> GetConPrimaryNameFileAsync() => GetFileAsync(_options.GetConPrimaryNameFileUri());
|
||||
public Task<Result<Stream>> GetConAddressesFileAsync() => GetFileAsync(_options.GetConAddressesFileUri());
|
||||
public Task<Result<Stream>> GetConAltNamesFileAsync() => GetFileAsync(_options.GetConAltNamesFileUri());
|
||||
public Task<Result<Stream>> GetConCommentsFileAsync() => GetFileAsync(_options.GetConCommentsFileUri());
|
||||
}
|
||||
|
||||
@@ -4,5 +4,16 @@
|
||||
"Default": "Information",
|
||||
"Microsoft.Hosting.Lifetime": "Information"
|
||||
}
|
||||
},
|
||||
"OfacFileServiceOptions": {
|
||||
"Url": "https://sanctionslistservice.ofac.treas.gov/api/PublicationPreview/exports",
|
||||
"SdnFileName": "SDN.CSV",
|
||||
"AddressFileName": "ADD.CSV",
|
||||
"AltNamesFileName": "ALT.CSV",
|
||||
"CommentsFileName": "SDN_COMMENTS.CSV",
|
||||
"ConPrimaryNameFileName": "CONS_PRIM.CSV",
|
||||
"ConAddressesFileName": "CONS_ADD.CSV",
|
||||
"ConAltNamesFileName": "CONS_ALT.CSV",
|
||||
"ConCommentsFileName": "CONS_COMMENTS.CSV"
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user