/// <summary> /// Retrive Method for the Loqate Service /// </summary> /// <returns></returns> public Task <LoqateRetrieveResult> Retrive() { //Build the url var url = "https://api.addressy.com/Capture/Interactive/Retrieve/v1.00/dataset.ws?"; url += "&Key=" + System.Web.HttpUtility.UrlEncode(key); url += "&Id=" + System.Web.HttpUtility.UrlEncode(id); url += "&Field1Format=" + System.Web.HttpUtility.UrlEncode(field1format); url += "&Field2Format=" + System.Web.HttpUtility.UrlEncode(field2format); url += "&Field3Format=" + System.Web.HttpUtility.UrlEncode(field3format); url += "&Field4Format=" + System.Web.HttpUtility.UrlEncode(field4format); url += "&Field5Format=" + System.Web.HttpUtility.UrlEncode(field5format); url += "&Field6Format=" + System.Web.HttpUtility.UrlEncode(field6format); url += "&Field7Format=" + System.Web.HttpUtility.UrlEncode(field7format); url += "&Field8Format=" + System.Web.HttpUtility.UrlEncode(field8format); url += "&Field9Format=" + System.Web.HttpUtility.UrlEncode(field9format); url += "&Field10Format=" + System.Web.HttpUtility.UrlEncode(field10format); url += "&Field11Format=" + System.Web.HttpUtility.UrlEncode(field11format); url += "&Field12Format=" + System.Web.HttpUtility.UrlEncode(field12format); url += "&Field13Format=" + System.Web.HttpUtility.UrlEncode(field13format); url += "&Field14Format=" + System.Web.HttpUtility.UrlEncode(field14format); url += "&Field15Format=" + System.Web.HttpUtility.UrlEncode(field15format); url += "&Field16Format=" + System.Web.HttpUtility.UrlEncode(field16format); url += "&Field17Format=" + System.Web.HttpUtility.UrlEncode(field17format); url += "&Field18Format=" + System.Web.HttpUtility.UrlEncode(field18format); url += "&Field19Format=" + System.Web.HttpUtility.UrlEncode(field19format); url += "&Field20Format=" + System.Web.HttpUtility.UrlEncode(field20format); //Create the dataset var dataSet = new DataSet(); dataSet.ReadXml(url); //Check for an error if (dataSet.Tables.Count == 1 && dataSet.Tables[0].Columns.Count == 4 && dataSet.Tables[0].Columns[0].ColumnName == "Error") { throw new Exception(dataSet.Tables[0].Rows[0].ItemArray[1].ToString()); } var result = new LoqateRetrieveResult { Id = Convert.ToString(dataSet.Tables[0].Rows[0]["id"]), Company = Convert.ToString(dataSet.Tables[0].Rows[0]["company"]), City = Convert.ToString(dataSet.Tables[0].Rows[0]["city"]), Line1 = Convert.ToString(dataSet.Tables[0].Rows[0]["line1"]), Line2 = Convert.ToString(dataSet.Tables[0].Rows[0]["line2"]), Line3 = Convert.ToString(dataSet.Tables[0].Rows[0]["line3"]), Line4 = Convert.ToString(dataSet.Tables[0].Rows[0]["line4"]), Line5 = Convert.ToString(dataSet.Tables[0].Rows[0]["line5"]), Province = Convert.ToString(dataSet.Tables[0].Rows[0]["province"]), PostalCode = Convert.ToString(dataSet.Tables[0].Rows[0]["postalCode"]) }; //Return the dataset return(Task.FromResult(result)); //FYI: The dataset contains the following columns: //Id //DomesticId //Language //LanguageAlternatives //Department //Company //SubBuilding //BuildingNumber //BuildingName //SecondaryStreet //Street //Block //Neighbourhood //District //City //Line1 //Line2 //Line3 //Line4 //Line5 //AdminAreaName //AdminAreaCode //Province //ProvinceName //ProvinceCode //PostalCode //CountryName //CountryIso2 //CountryIso3 //CountryIsoNumber //SortingNumber1 //SortingNumber2 //Barcode //POBoxNumber //Label //Type //DataLevel //Field1 //Field2 //Field3 //Field4 //Field5 //Field6 //Field7 //Field8 //Field9 //Field10 //Field11 //Field12 //Field13 //Field14 //Field15 //Field16 //Field17 //Field18 //Field19 //Field20 //throw new NotImplementedException(); }
public async Task <IActionResult> Retrieve(int tenantId, string id) { try { var result = new LoqateRetrieveResult(); var cacheKey = "Retrieve_" + tenantId + "_" + id; // Look for cache key. if (!_cache.TryGetValue(cacheKey, out result)) { // Key not in cache, so get data. var tenantWithConfig = Common.GetTenantWithConfiguration(tenantId); if (tenantWithConfig == null) { throw new Exception("Tenant not found"); } var lookupService = Common.GetLookupService(tenantWithConfig.Config?.Preference ?? ServiceType.Default); #region Dictionary var dict = new Dictionary <string, string>(); if (tenantWithConfig != null && !string.IsNullOrEmpty(tenantWithConfig.Config?.APIKEY)) { dict.Add("key", tenantWithConfig.Config?.APIKEY); } dict.Add("limit", "10"); dict.Add("id", id); #endregion String serviceFullName = "Karakoram.Address.Lookup.Services.LookupServices." + lookupService; Assembly assem = typeof(IAddressLookupService).Assembly; IAddressLookupService lookServ = (IAddressLookupService)assem.CreateInstance(serviceFullName, true); if (lookServ == null) { return(BadRequest("Something went wrong")); } lookServ.Initialise(dict); result = result = await lookServ.Retrive(); // Set cache options. var cacheEntryOptions = new MemoryCacheEntryOptions() // Keep in cache for this time, reset time if accessed. .SetSlidingExpiration(TimeSpan.FromMinutes(1)); // Save data in cache. _cache.Set(cacheKey, result, cacheEntryOptions); //// TODO Bill User //TenantService.RaiseBillingEvent(new BillingEvent { // Type: "Address Lookup", // TimeSpan: DateTime.Now.TimeOfDay, // UserId: TenantId, // Service: lookupService, // Credit: 1 // this can be different for each type of service like for example it can be 2 for phone number varification //}); } return(Ok(new { result })); } catch (Exception ex) { return(BadRequest(ex.Message)); } }