geocoder
GeocoderService.cs
using Microsoft.AspNetCore.WebUtilities;
using Microsoft.Extensions.Options;
using Pims.Core.Http;
using Pims.Geocoder.Configuration;
using Pims.Geocoder.Extensions;
using Pims.Geocoder.Models;
using Pims.Geocoder.Parameters;
using System;
using System.Net;
using System.Threading.Tasks;
namespace Pims.Geocoder
{
///
/// GeocoderService clast, provides a service for integration with Geocoder API services.
///
public clast GeocoderService : IGeocoderService
{
#region Properties
protected IHttpRequestClient Client { get; }
public GeocoderOptions Options { get; }
#endregion
#region Constructors
///
/// Creates a new instance of a GeocoderService, initializes with specified arguments.
///
///
///
public GeocoderService(IOptions options, IHttpRequestClient client)
{
this.Options = options.Value;
this.Client = client;
if (!String.IsNullOrWhiteSpace(this.Options.Key))
{
client.Client.DefaultRequestHeaders.Add("apikey", this.Options.Key);
}
}
#endregion
#region Methods
///
/// Generates the full URL including the host.
///
///
///
///
private string GenerateUrl(string endpoint, string outputFormat = "json")
{
var host = this.Options.HostUri;
return $"{host}{endpoint.Replace("{outputFormat}", outputFormat)}";
}
///
/// Sends an HTTP request to Geocoder for addresses that match the specified 'address'.
///
/// The address to geocode
/// The output format. Defaults to "json"
///
public async Task GetSiteAddressesAsync(string address, string outputFormat = "json")
{
var parameters = new AddressesParameters()
{
AddressString = WebUtility.UrlEncode(address)
};
return await GetSiteAddressesAsync(parameters, outputFormat);
}
///
/// Sends an HTTP request to Geocoder for addresses that match the specified 'parameters'.
///
/// The address search paramenters
/// The output format. Defaults to "json"
///
public async Task GetSiteAddressesAsync(AddressesParameters parameters, string outputFormat = "json")
{
var uri = new Uri(GenerateUrl(this.Options.Sites.AddressesUrl, outputFormat));
var url = QueryHelpers.AddQueryString(uri.AbsoluteUri, parameters.ToQueryStringDictionary());
return await this.Client.GetAsync(url);
}
///
/// Sends an HTTP request to Geocoder for all parcel identifiers (PIDs) astociated with an individual site.
/// A 'siteId' is a unique identifier astigned to every site in B.C.
/// Valid 'siteId' values for an address are returned by GetSiteAddressesAsync.
///
/// The site identifier
/// The output format. Defaults to "json"
///
public async Task GetPids(Guid siteId, string outputFormat = "json")
{
var endpoint = this.Options.Parcels.PidsUrl.Replace("{siteId}", siteId.ToString());
var uri = new Uri(GenerateUrl(endpoint, outputFormat));
return await this.Client.GetAsync(uri);
}
#endregion
}
}