Skip to content

Commit

Permalink
build out controller and service
Browse files Browse the repository at this point in the history
  • Loading branch information
biegehydra committed Oct 22, 2023
1 parent f48fb22 commit 7b5c55e
Show file tree
Hide file tree
Showing 11 changed files with 255 additions and 157 deletions.
33 changes: 22 additions & 11 deletions LibPostalApi/Controllers/LibPostalController.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using LibPostalApi.Interfaces;
using LibPostalApi.Models;
using LibPostalNet;
using Microsoft.AspNetCore.Mvc;
using System.Net;

namespace LibPostalApi.Controllers
{
Expand All @@ -17,25 +17,36 @@ public LibPostalController(ILibPostalService libPostal)
}

[HttpPost]
public ExpandAddressesResponse ExpandAddresses([FromBody] ExpandAddressesRequest request)
public IActionResult ExpandAddresses([FromBody] ExpandAddressesRequest? request)
{
var responses = new List<AddressExpansionResponse>();
foreach (var addresses in request.Addresses)
if (request?.Addresses is not {Count: > 0})
{
responses.Add(_libPostal.ExpandAddress(addresses));
return BadRequest("No addresses in payload. Nothing to expand.");
}
return new ExpandAddressesResponse(responses);

var results = _libPostal.ExpandAddress(request.Addresses, request.ExpandOptions);
if (results.Results == null)
{
return StatusCode((int)HttpStatusCode.InternalServerError, "An internal server error occurred.");
}

return Ok(new ExpandAddressesResponse(results.Results, request.Addresses.Count, results.Successes, results.Failures));
}

[HttpPost]
public ParseAddressesResponse ParseAddresses([FromBody] ParseAddressesRequest request)
public IActionResult ParseAddresses([FromBody] ParseAddressesRequest? request)
{
var responses = new List<AddressParserResponse>();
foreach (var addresses in request.Addresses)
if (request?.Addresses is not { Count: > 0 })
{
responses.Add(_libPostal.ParseAddress(addresses));
return BadRequest("No addresses in payload. Nothing to expand.");
}
return new ParseAddressesResponse(responses);
var results = _libPostal.ParseAddress(request.Addresses, request.ParseOptions);
if (results.Results == null)
{
return StatusCode((int)HttpStatusCode.InternalServerError, "An internal server error occurred.");
}

return Ok(new ParseAddressesResponse(results.Results, request.Addresses.Count, results.Successes, results.Failures));
}
}
}
104 changes: 104 additions & 0 deletions LibPostalApi/ExtensionMethods/MappingExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
using LibPostalApi.Models;
using LibPostalNet;

namespace LibPostalApi.ExtensionMethods;

public static class MappingExtensions
{
public static void MapValuesTo(this ExpandOptions dto, AddressExpansionOptions domain)
{
if (dto.Languages is { Length: > 0 })
{
domain.Languages = dto.Languages;
}
if (dto.AddressComponents != null)
{
domain.AddressComponents = (AddressComponents)dto.AddressComponents;
}
if (dto.LatinAscii.HasValue)
{
domain.LatinAscii = dto.LatinAscii.Value;
}
if (dto.Transliterate.HasValue)
{
domain.Transliterate = dto.Transliterate.Value;
}
if (dto.StripAccents.HasValue)
{
domain.StripAccents = dto.StripAccents.Value;
}
if (dto.Decompose.HasValue)
{
domain.Decompose = dto.Decompose.Value;
}
if (dto.Lowercase.HasValue)
{
domain.Lowercase = dto.Lowercase.Value;
}
if (dto.TrimString.HasValue)
{
domain.TrimString = dto.TrimString.Value;
}
if (dto.DropParentheticals.HasValue)
{
domain.DropParentheticals = dto.DropParentheticals.Value;
}
if (dto.ReplaceNumericHyphens.HasValue)
{
domain.ReplaceNumericHyphens = dto.ReplaceNumericHyphens.Value;
}
if (dto.DeleteNumericHyphens.HasValue)
{
domain.DeleteNumericHyphens = dto.DeleteNumericHyphens.Value;
}
if (dto.SplitAlphaFromNumeric.HasValue)
{
domain.SplitAlphaFromNumeric = dto.SplitAlphaFromNumeric.Value;
}
if (dto.ReplaceWordHyphens.HasValue)
{
domain.ReplaceWordHyphens = dto.ReplaceWordHyphens.Value;
}
if (dto.DeleteWordHyphens.HasValue)
{
domain.DeleteWordHyphens = dto.DeleteWordHyphens.Value;
}
if (dto.DeleteFinalPeriods.HasValue)
{
domain.DeleteFinalPeriods = dto.DeleteFinalPeriods.Value;
}
if (dto.DeleteAcronymPeriods.HasValue)
{
domain.DeleteAcronymPeriods = dto.DeleteAcronymPeriods.Value;
}
if (dto.DropEnglishPossessives.HasValue)
{
domain.DropEnglishPossessives = dto.DropEnglishPossessives.Value;
}
if (dto.DeleteApostrophes.HasValue)
{
domain.DeleteApostrophes = dto.DeleteApostrophes.Value;
}
if (dto.ExpandNumex.HasValue)
{
domain.ExpandNumex = dto.ExpandNumex.Value;
}
if (dto.RomanNumerals.HasValue)
{
domain.RomanNumerals = dto.RomanNumerals.Value;
}
}

public static void MapValuesTo(this ParseOptions dto, AddressParserOptions domain)
{
if (!string.IsNullOrEmpty(dto.Country))
{
domain.Country = dto.Country.Trim();
}

if (!string.IsNullOrEmpty(dto.Language))
{
domain.Language = dto.Language.Trim();
}
}
}
7 changes: 4 additions & 3 deletions LibPostalApi/Interfaces/ILibPostalService.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
using LibPostalNet;
using LibPostalApi.Models;
using LibPostalNet;

namespace LibPostalApi.Interfaces;

public interface ILibPostalService
{
AddressParserResponse ParseAddress(string address);
AddressExpansionResponse ExpandAddress(string address);
(AddressParserResponse[]? Results, int Successes, int Failures) ParseAddress(List<string> addresses, ParseOptions? dtoOptions);
(AddressExpansionResponse[]? Results, int Successes, int Failures) ExpandAddress(List<string> addresses, ExpandOptions? dtoOptions);
}
5 changes: 2 additions & 3 deletions LibPostalApi/LibPostalApi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="7.0.5" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.19.1" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="7.0.10" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
</ItemGroup>

<ItemGroup>
Expand Down
32 changes: 28 additions & 4 deletions LibPostalApi/Models/Requests.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,31 @@
using LibPostalNet;

namespace LibPostalApi.Models;
public record ParseAddressesRequest(List<string> Addresses);
public record ExpandAddressesRequest(List<string> Addresses);
public record ExpandAddressesResponse(List<AddressExpansionResponse> ParseResults);
public record ParseAddressesResponse(List<AddressParserResponse> ParseResults);
public record ParseAddressesRequest(List<string>? Addresses, ParseOptions? ParseOptions);
public record ExpandAddressesRequest(List<string>? Addresses, ExpandOptions? ExpandOptions);
public record ExpandAddressesResponse(AddressExpansionResponse[]? ParseResults, int AddressesReceived, int SuccessfullyParsedCount, int UnsuccessfullyParsedCount);
public record ParseAddressesResponse(AddressParserResponse[]? ParseResults, int AddressesReceived, int SuccessfullyExpandCount, int UnsuccessfullyExpandedCount);
public record ParseOptions(string? Country, string? Language);

public record ExpandOptions(
string[]? Languages,
int? AddressComponents,
bool? LatinAscii,
bool? Transliterate,
bool? StripAccents,
bool? Decompose,
bool? Lowercase,
bool? TrimString,
bool? DropParentheticals,
bool? ReplaceNumericHyphens,
bool? DeleteNumericHyphens,
bool? SplitAlphaFromNumeric,
bool? ReplaceWordHyphens,
bool? DeleteWordHyphens,
bool? DeleteFinalPeriods,
bool? DeleteAcronymPeriods,
bool? DropEnglishPossessives,
bool? DeleteApostrophes,
bool? ExpandNumex,
bool? RomanNumerals
);
15 changes: 14 additions & 1 deletion LibPostalApi/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,19 @@
app.MapControllers();

app.MapHealthChecks("/health");

app.Use(async (context, next) =>
{
if (context.Request.Path.StartsWithSegments("/health"))
{
var libPostal = app.Services.GetRequiredService<ILibPostalService>();
var testResult = libPostal.ParseAddress(TestData.TestAddress);
if (testResult == null)
{
context.Response.StatusCode = 500;
await context.Response.WriteAsync("LibPostalApi is not healthy");
Console.WriteLine($"Health check endpoint LibPostalApi api is UNHEALTHY");
return;
}
Console.WriteLine($"Health check endpoint called at {DateTime.UtcNow}");
}
Expand All @@ -39,3 +47,8 @@
});

app.Run();

public static class TestData
{
public static readonly List<string> TestAddress = new () { "8000 Southern Breeze Dr Orlando FL 32836" };
}
Loading

0 comments on commit 7b5c55e

Please sign in to comment.