63 lines
1.7 KiB
C#
63 lines
1.7 KiB
C#
using System.Xml.Linq;
|
|
using Microsoft.Extensions.Options;
|
|
using Serilog;
|
|
using USPS.Parsers;
|
|
using USPS.ApiCalls;
|
|
using USPS.Configuration;
|
|
using Landmark.OLA.Models;
|
|
using Landmark.OLA.Models.Vendor;
|
|
using Landmark.OLA.Models.Addresses;
|
|
|
|
namespace USPS.Communication
|
|
{
|
|
public class UspsVendorClient : IUspsVendorClient
|
|
{
|
|
private readonly IUspsService _uspsService;
|
|
private readonly IResponseParser<Address> _addressParser;
|
|
|
|
private readonly UspsSettings _settings;
|
|
|
|
public UspsVendorClient(IUspsService uspsService,
|
|
IResponseParser<Address> addressParser,
|
|
IOptions<UspsSettings> settings)
|
|
{
|
|
_uspsService = uspsService;
|
|
_addressParser = addressParser;
|
|
_settings = settings.Value;
|
|
}
|
|
|
|
|
|
public async Task<Address> CompareAddress(Address address)
|
|
{
|
|
|
|
ModelValidator.Validate(address);
|
|
|
|
AddressValidationRequestBuilder api = new(_settings, address);
|
|
|
|
string createXml = api.BuildGET();
|
|
|
|
XElement response = await _uspsService.SendMessage(createXml);
|
|
|
|
try
|
|
{
|
|
if (_addressParser.CheckForError(response) )
|
|
{
|
|
throw new ArgumentException("USPS rejected address");
|
|
}
|
|
|
|
return _addressParser.Parse(response);
|
|
}
|
|
catch (ArgumentException)
|
|
{
|
|
// Do not log; this is thrown when a bad address is submitted and it just spams the logs.
|
|
throw;
|
|
}
|
|
catch (Exception)
|
|
{
|
|
Log.Fatal("Error parsing response.");
|
|
throw;
|
|
}
|
|
}
|
|
}
|
|
}
|