private async Task <IEnumerable <DeliveryLocation> > GetLocationsFromServiceAsync(string city, string postalCode, StreetAddress parsedAddress)
 {
     try
     {
         return((await
                 _postNordClient.FindNearestByAddress(new RegionInfo("NO"), city, postalCode, parsedAddress.Street,
                                                      parsedAddress.Number)).
                Select(CreateDeliveryLocationFrom));
     }
     catch (Exception ex)
     {
         Log.Error("Error occured when getting service points from Postnord.", ex);
         return(Enumerable.Empty <DeliveryLocation>());
     }
 }
Exemple #2
0
        public async Task <JsonResult> GetDeliveryLocations(string streetAddress, string city, string postalCode)
        {
            string streetNumber = string.Empty;

            if (!string.IsNullOrEmpty(streetAddress))
            {
                streetAddress = streetAddress.TrimEnd();


                // Parse the street name and number (if any) from the street address

                var  i        = streetAddress.Length - 1;
                bool hasDigit = false;

                for (; 0 <= i; --i)
                {
                    if (char.IsDigit(streetAddress[i]))
                    {
                        hasDigit = true;
                        continue;
                    }

                    if (!char.IsLetter(streetAddress[i]))
                    {
                        break;
                    }
                }

                ++i;



                if (hasDigit && i < streetAddress.Length)
                {
                    streetNumber  = streetAddress.Substring(i);
                    streetAddress = streetAddress.Substring(0, i).Trim();
                }
                else
                {
                    streetNumber = null;
                }
            }


            if (string.IsNullOrEmpty(postalCode))
            {
                return(Json(new { success = false }, JsonRequestBehavior.AllowGet));
            }

            try
            {
                var x = (await _postNordClient.FindNearestByAddress(new RegionInfo("NO"), city, postalCode, streetAddress, streetNumber)).
                        Select(sp => new DeliveryLocation(
                                   new ServicePoint()
                {
                    Id = sp.Id, Name = sp.Name, Address = sp.DeliveryAddress.StreetName + ' ' + sp.DeliveryAddress.StreetNumber, City = sp.DeliveryAddress.City, PostalCode = sp.DeliveryAddress.PostalCode
                },
                                   sp.Name));
                return(Json(x, JsonRequestBehavior.AllowGet));
            }
            catch (Exception)
            {
            }
            return(Json(new { success = false }, JsonRequestBehavior.AllowGet));
        }