feat: begin working on adding search result back into Onspring

This commit is contained in:
Stevan Freeborn
2024-08-30 22:28:31 -05:00
parent 409d4e339f
commit dc4d4ff1d3
5 changed files with 80 additions and 27 deletions
@@ -3,4 +3,5 @@ namespace SanctionsSearch.Worker.Interfaces;
interface IOnspringService
{
Task<List<SearchRequest>> GetSearchRequestsAsync();
Task<Result> AddSearchResultAsync(SearchResult result);
}
@@ -2,6 +2,7 @@ namespace SanctionsSearch.Worker.Models;
class SearchRequest
{
public int Id { get; init; }
public string Name { get; set; } = string.Empty;
public string Address { get; set; } = string.Empty;
public string City { get; set; } = string.Empty;
@@ -0,0 +1,6 @@
namespace SanctionsSearch.Worker.Models;
class SearchResult
{
// TODO: Add properties to represent the search result
}
+10 -9
View File
@@ -69,17 +69,18 @@ class Program
builder.Services.AddSingleton(TimeProvider.System);
builder.Services
.AddHttpClient<IOfacFileService, OfacFileService>()
.AddStandardResilienceHandler();
builder.Services
.AddHttpClient<IOnspringService, OnspringService>((sp, client) =>
builder.Services.AddHttpClient();
builder.Services.ConfigureHttpClientDefaults(builder => builder.AddStandardResilienceHandler());
builder.Services.AddScoped<IOnspringClient>(sp =>
{
var options = sp.GetRequiredService<OnspringOptions>();
client.BaseAddress = new Uri(options.BaseUrl);
})
.AddStandardResilienceHandler();
var httpClient = sp.GetRequiredService<HttpClient>();
httpClient.BaseAddress = new Uri(options.BaseUrl);
return new OnspringClient(options.ApiKey, httpClient);
});
builder.Services.AddScoped<IOfacFileService, OfacFileService>();
builder.Services.AddScoped<IOnspringService, OnspringService>();
builder.Services.AddScoped<ISdnRepository, SdnRepository>();
builder.Services.AddScoped<IAddressRepository, AddressRepository>();
@@ -1,26 +1,39 @@
namespace SanctionsSearch.Worker.Services;
class OnspringService(
HttpClient httpClient,
IOnspringClient client,
OnspringOptions options,
ILogger<OnspringService> logger
) : IOnspringService
{
private readonly IOnspringClient _client = new OnspringClient(options.ApiKey, httpClient);
private readonly OnspringOptions _options = options;
private readonly ILogger<OnspringService> _logger = logger;
private readonly IOnspringClient _client = client;
public Task<Result> AddSearchResultAsync(SearchResult result)
{
// TODO: Implement this method
// - we should add a new record for each hit
// - each new hit record should reference the request
// - we should update the request status to processed
// - we should return a Result indicating success or failure
throw new NotImplementedException();
}
public async Task<List<SearchRequest>> GetSearchRequestsAsync()
{
var queryRequest = new QueryRecordsRequest()
{
AppId = options.SearchRequestOptions.AppId,
AppId = _options.SearchRequestOptions.AppId,
Filter = $"{_options.SearchRequestOptions.StatusFieldId} contains '{_options.SearchRequestOptions.AwaitingProcessingStatusId}'",
FieldIds = [
options.SearchRequestOptions.NameFieldId,
options.SearchRequestOptions.AddressFieldId,
options.SearchRequestOptions.CityFieldId,
options.SearchRequestOptions.StateFieldId,
options.SearchRequestOptions.ZipFieldId,
options.SearchRequestOptions.CountryFieldId
_options.SearchRequestOptions.NameFieldId,
_options.SearchRequestOptions.AddressFieldId,
_options.SearchRequestOptions.CityFieldId,
_options.SearchRequestOptions.StateFieldId,
_options.SearchRequestOptions.ZipFieldId,
_options.SearchRequestOptions.CountryFieldId
],
};
@@ -42,7 +55,35 @@ class OnspringService(
if (initialResponse.Value.HasMorePages())
{
// TODO: Fan out and collect remaining pages
var remainingPageNumbers = Enumerable.Range(initialResponse.Value.PageNumber + 1, initialResponse.Value.TotalPages - 1);
var pagingRequests = remainingPageNumbers.Select(num => new PagingRequest { PageNumber = num });
var remainingRequests = pagingRequests.Select(async pageRequest =>
{
try
{
var res = await _client.QueryRecordsAsync(queryRequest, pageRequest);
if (res.IsSuccessful is false)
{
_logger.LogError(
"Failed to retrieve search requests for page {PageNumber}: {StatusCode} - {Error}",
pageRequest.PageNumber,
res.StatusCode,
res.Message
);
return;
}
res.Value.Items.ForEach(records.Add);
}
catch (Exception ex)
{
_logger.LogError(ex, "Failed to retrieve search requests for page {PageNumber}", pageRequest.PageNumber);
}
});
await Task.WhenAll(remainingRequests);
}
return records.Select(MapRecordToSearchRequest).ToList();
@@ -50,31 +91,34 @@ class OnspringService(
private SearchRequest MapRecordToSearchRequest(ResultRecord record)
{
var searchRequest = new SearchRequest();
var searchRequest = new SearchRequest
{
Id = record.RecordId
};
foreach (var field in record.FieldData)
{
if (field.FieldId == options.SearchRequestOptions.NameFieldId)
if (field.FieldId == _options.SearchRequestOptions.NameFieldId)
{
searchRequest.Name = field.GetStringValue();
}
else if (field.FieldId == options.SearchRequestOptions.AddressFieldId)
else if (field.FieldId == _options.SearchRequestOptions.AddressFieldId)
{
searchRequest.Address = field.GetStringValue();
}
else if (field.FieldId == options.SearchRequestOptions.CityFieldId)
else if (field.FieldId == _options.SearchRequestOptions.CityFieldId)
{
searchRequest.City = field.GetStringValue();
}
else if (field.FieldId == options.SearchRequestOptions.StateFieldId)
else if (field.FieldId == _options.SearchRequestOptions.StateFieldId)
{
searchRequest.State = field.GetStringValue();
}
else if (field.FieldId == options.SearchRequestOptions.ZipFieldId)
else if (field.FieldId == _options.SearchRequestOptions.ZipFieldId)
{
searchRequest.Zip = field.GetStringValue();
}
else if (field.FieldId == options.SearchRequestOptions.CountryFieldId)
else if (field.FieldId == _options.SearchRequestOptions.CountryFieldId)
{
searchRequest.Country = field.GetStringValue();
}