feat: work on collecting search requests
This commit is contained in:
@@ -14,10 +14,9 @@ global using Moq;
|
||||
|
||||
global using RichardSzalay.MockHttp;
|
||||
|
||||
global using SanctionsSearch.Worker.Interfaces;
|
||||
global using SanctionsSearch.Worker.Models;
|
||||
global using SanctionsSearch.Worker.Options;
|
||||
global using SanctionsSearch.Worker.Persistence;
|
||||
global using SanctionsSearch.Worker.Services;
|
||||
global using SanctionsSearch.Worker.Tests.Faker;
|
||||
global using SanctionsSearch.Worker.Interfaces;
|
||||
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
namespace SanctionsSearch.Worker.Extensions;
|
||||
|
||||
static class GetPagedRecordsResponseExtensions
|
||||
{
|
||||
public static bool HasMorePages(this GetPagedRecordsResponse response)
|
||||
{
|
||||
return response.PageNumber < response.TotalPages;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
namespace SanctionsSearch.Worker.Extensions;
|
||||
|
||||
static class RecordFieldValueExtensions
|
||||
{
|
||||
public static string GetStringValue(this RecordFieldValue recordFieldValue) =>
|
||||
recordFieldValue.Type is not ResultValueType.String
|
||||
? string.Empty
|
||||
: recordFieldValue.AsString() ?? string.Empty;
|
||||
}
|
||||
@@ -2,4 +2,5 @@ namespace SanctionsSearch.Worker.Interfaces;
|
||||
|
||||
interface IOnspringService
|
||||
{
|
||||
Task<List<SearchRequest>> GetSearchRequestsAsync();
|
||||
}
|
||||
+12
-12
@@ -4,19 +4,19 @@
|
||||
|
||||
namespace SanctionsSearch.Worker.Migrations
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public partial class add_addresses_and_comments_to_sdn : Migration
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public partial class add_addresses_and_comments_to_sdn : Migration
|
||||
protected override void Up(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override void Up(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override void Down(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override void Down(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -17,4 +17,4 @@ class SdnMap : ClassMap<Sdn>
|
||||
Map(m => m.VesselOwner).Index(10).TypeConverter<NullCharacterConverter>();
|
||||
Map(m => m.Remarks).Index(11).TypeConverter<NullCharacterConverter>();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
namespace SanctionsSearch.Worker.Models;
|
||||
|
||||
class SearchRequest
|
||||
{
|
||||
public string Name { get; set; } = string.Empty;
|
||||
public string Address { get; set; } = string.Empty;
|
||||
public string City { get; set; } = string.Empty;
|
||||
public string State { get; set; } = string.Empty;
|
||||
public string Zip { get; set; } = string.Empty;
|
||||
public string Country { get; set; } = string.Empty;
|
||||
}
|
||||
@@ -73,6 +73,14 @@ class Program
|
||||
.AddHttpClient<IOfacFileService, OfacFileService>()
|
||||
.AddStandardResilienceHandler();
|
||||
|
||||
builder.Services
|
||||
.AddHttpClient<IOnspringService, OnspringService>((sp, client) =>
|
||||
{
|
||||
var options = sp.GetRequiredService<OnspringOptions>();
|
||||
client.BaseAddress = new Uri(options.BaseUrl);
|
||||
})
|
||||
.AddStandardResilienceHandler();
|
||||
|
||||
builder.Services.AddScoped<ISdnRepository, SdnRepository>();
|
||||
builder.Services.AddScoped<IAddressRepository, AddressRepository>();
|
||||
builder.Services.AddScoped<IAliasRepository, AliasRepository>();
|
||||
|
||||
@@ -1,5 +1,85 @@
|
||||
namespace SanctionsSearch.Worker.Services;
|
||||
|
||||
class OnspringService : IOnspringService
|
||||
class OnspringService(
|
||||
HttpClient httpClient,
|
||||
OnspringOptions options,
|
||||
ILogger<OnspringService> logger
|
||||
) : IOnspringService
|
||||
{
|
||||
private readonly IOnspringClient _client = new OnspringClient(options.ApiKey, httpClient);
|
||||
private readonly ILogger<OnspringService> _logger = logger;
|
||||
|
||||
public async Task<List<SearchRequest>> GetSearchRequestsAsync()
|
||||
{
|
||||
var queryRequest = new QueryRecordsRequest()
|
||||
{
|
||||
AppId = options.SearchRequestOptions.AppId,
|
||||
FieldIds = [
|
||||
options.SearchRequestOptions.NameFieldId,
|
||||
options.SearchRequestOptions.AddressFieldId,
|
||||
options.SearchRequestOptions.CityFieldId,
|
||||
options.SearchRequestOptions.StateFieldId,
|
||||
options.SearchRequestOptions.ZipFieldId,
|
||||
options.SearchRequestOptions.CountryFieldId
|
||||
],
|
||||
};
|
||||
|
||||
var records = new ConcurrentBag<ResultRecord>();
|
||||
|
||||
var initialResponse = await _client.QueryRecordsAsync(queryRequest);
|
||||
|
||||
if (initialResponse.IsSuccessful is false)
|
||||
{
|
||||
_logger.LogError(
|
||||
"Failed to retrieve search requests: {StatusCode} - {Error}",
|
||||
initialResponse.StatusCode,
|
||||
initialResponse.Message
|
||||
);
|
||||
return [];
|
||||
}
|
||||
|
||||
initialResponse.Value.Items.ForEach(records.Add);
|
||||
|
||||
if (initialResponse.Value.HasMorePages())
|
||||
{
|
||||
// TODO: Fan out and collect remaining pages
|
||||
}
|
||||
|
||||
return records.Select(MapRecordToSearchRequest).ToList();
|
||||
}
|
||||
|
||||
private SearchRequest MapRecordToSearchRequest(ResultRecord record)
|
||||
{
|
||||
var searchRequest = new SearchRequest();
|
||||
|
||||
foreach (var field in record.FieldData)
|
||||
{
|
||||
if (field.FieldId == options.SearchRequestOptions.NameFieldId)
|
||||
{
|
||||
searchRequest.Name = field.GetStringValue();
|
||||
}
|
||||
else if (field.FieldId == options.SearchRequestOptions.AddressFieldId)
|
||||
{
|
||||
searchRequest.Address = field.GetStringValue();
|
||||
}
|
||||
else if (field.FieldId == options.SearchRequestOptions.CityFieldId)
|
||||
{
|
||||
searchRequest.City = field.GetStringValue();
|
||||
}
|
||||
else if (field.FieldId == options.SearchRequestOptions.StateFieldId)
|
||||
{
|
||||
searchRequest.State = field.GetStringValue();
|
||||
}
|
||||
else if (field.FieldId == options.SearchRequestOptions.ZipFieldId)
|
||||
{
|
||||
searchRequest.Zip = field.GetStringValue();
|
||||
}
|
||||
else if (field.FieldId == options.SearchRequestOptions.CountryFieldId)
|
||||
{
|
||||
searchRequest.Country = field.GetStringValue();
|
||||
}
|
||||
}
|
||||
|
||||
return searchRequest;
|
||||
}
|
||||
}
|
||||
@@ -1,12 +1,22 @@
|
||||
global using System.Collections.Concurrent;
|
||||
global using System.Globalization;
|
||||
global using System.Linq.Expressions;
|
||||
global using System.Reflection;
|
||||
|
||||
global using CsvHelper;
|
||||
global using CsvHelper.Configuration;
|
||||
|
||||
global using FluentResults;
|
||||
|
||||
global using Microsoft.Data.Sqlite;
|
||||
global using Microsoft.EntityFrameworkCore;
|
||||
global using Microsoft.Extensions.Options;
|
||||
|
||||
global using Onspring.API.SDK;
|
||||
global using Onspring.API.SDK.Enums;
|
||||
global using Onspring.API.SDK.Models;
|
||||
|
||||
global using SanctionsSearch.Worker.Extensions;
|
||||
global using SanctionsSearch.Worker.Interfaces;
|
||||
global using SanctionsSearch.Worker.Models;
|
||||
global using SanctionsSearch.Worker.Options;
|
||||
@@ -19,7 +29,3 @@ global using Serilog;
|
||||
global using Serilog.Context;
|
||||
global using Serilog.Exceptions;
|
||||
global using Serilog.Formatting.Compact;
|
||||
|
||||
global using System.Globalization;
|
||||
global using CsvHelper;
|
||||
global using CsvHelper.Configuration;
|
||||
Reference in New Issue
Block a user