first commit
This commit is contained in:
@@ -0,0 +1,9 @@
|
||||
using System.Xml.Linq;
|
||||
|
||||
namespace USPS.Communication
|
||||
{
|
||||
public interface IUspsService
|
||||
{
|
||||
Task<XElement> SendMessage(string request);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
using System.Xml.Linq;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Serilog;
|
||||
using USPS.Configuration;
|
||||
|
||||
namespace USPS.Communication
|
||||
{
|
||||
public class UspsService : IUspsService
|
||||
{
|
||||
private readonly UspsSettings _settings;
|
||||
|
||||
public UspsService(
|
||||
IOptions<UspsSettings> settings)
|
||||
{
|
||||
|
||||
_settings = settings.Value;
|
||||
}
|
||||
|
||||
public async Task<XElement> SendMessage(string request)
|
||||
{
|
||||
try
|
||||
{
|
||||
var hc = new HttpClient();
|
||||
var result = await hc.GetAsync(request);
|
||||
|
||||
if (result.IsSuccessStatusCode)
|
||||
{
|
||||
var resultString = await result.Content.ReadAsStringAsync();
|
||||
var xdoc = XDocument.Parse(resultString);
|
||||
XElement xel = XElement.Parse(xdoc.ToString());
|
||||
return xel;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception("Bad status code from USPS");
|
||||
}
|
||||
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Log.Debug("{exception}", e.Message);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user