using BeWo.Data.Entities; using BS.Shared.DataContracts; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Configuration; using System.Linq; using System.Net.Http; using System.Text; using System.Threading.Tasks; using System.Web; namespace BeWo.Service.ServiceUtils.DistanceCalculator { public class GoogleDistanceMatrixApi : IDistanceAPI { public class Response { public string Status { get; set; } [JsonProperty(PropertyName = "origin_addresses")] public string[] OriginAddresses { get; set; } [JsonProperty(PropertyName = "destination_addresses")] public string[] DestinationAddresses { get; set; } public Row[] Rows { get; set; } public class Data { public int Value { get; set; } public string Text { get; set; } } public class Element { public string Status { get; set; } public Data Duration { get; set; } public Data Distance { get; set; } } public class Row { public Element[] Elements { get; set; } } } private string Key { get; set; } private string Url { get; set; } private AddressRouteMatrix Matrix { get; set; } private Address[] OriginAddresses { get; set; } private Address[] DestinationAddresses { get; set; } public GoogleDistanceMatrixApi() { var appSettings = ConfigurationManager.AppSettings; if (string.IsNullOrEmpty(appSettings["GoogleDistanceMatrixApiUrl"])) { throw new Exception("GoogleDistanceMatrixApiUrl is not set in AppSettings."); } Url = appSettings["GoogleDistanceMatrixApiUrl"]; if (string.IsNullOrEmpty(appSettings["GoogleDistanceMatrixApiKey"])) { throw new Exception("GoogleDistanceMatrixApiKey is not set in AppSettings."); } Key = appSettings["GoogleDistanceMatrixApiKey"]; } public void RequestMissingData(ref AddressRouteMatrix matrix, IEnumerable
origins, IEnumerable destinations) { Matrix = matrix; OriginAddresses = origins.ToArray(); DestinationAddresses = destinations.ToArray(); var task = GetMatrix(); task.Wait(); if (task.Exception is object) { throw task.Exception; } } public async Task