public async Task <IActionResult> RequestApiData([FromBody] string urlRequest) { ApiLogger apiLog = new ApiLogger(); apiLog.Id = Guid.NewGuid(); apiLog.RequestUrl = _apiConfig.Value.apiBase + urlRequest; apiLog.RequestDate = DateTime.Now; ApiCaller apiCaller = new ApiCaller(); apiLog.ResponseContent = await apiCaller.CallApi(apiLog.RequestUrl); apiLog.ResponseDate = DateTime.Now; using (_context) { try { await _context.ApiLogger.AddAsync(apiLog); await _context.SaveChangesAsync(); } catch (Exception e) { return(BadRequest(e.Message)); } } return(Ok(apiLog.ResponseContent)); }
public async Task <IActionResult> StoreCities() { string apiBase = _apiConfig.Value.apiBase; string apiCity = _apiConfig.Value.apiCities; //there are under 10,000 cities in the DB. //this is the max records per request //it should be implemented as a paging with multuple calls to api apiCity += "?limit=10000"; ApiCaller apiCall = new ApiCaller(); var response = await apiCall.CallApi(apiBase, apiCity); if (!string.IsNullOrEmpty(response)) { //Deserializing the response recieved from web api and storing CityJson cityJson = JsonConvert.DeserializeObject <CityJson>(response); if (cityJson.results.Any()) { using (_context) { try { foreach (CityResponse result in cityJson.results) { if (await _context.City.FirstOrDefaultAsync(c => c.CountryCode == result.country && c.City1 == result.city) == null) { await _context.City.AddAsync(new City { City1 = result.city, CountryCode = result.country, Count = result.count, Locations = result.locations }); } } await _context.SaveChangesAsync(); } catch (Exception e) {//log exception return(BadRequest(e.Message)); } } } } return(Ok(true)); }
public async Task <IActionResult> StoreCountries() { string apiBase = _apiConfig.Value.apiBase; string apiCountry = _apiConfig.Value.apiCountries; ApiCaller apiCall = new ApiCaller(); var response = await apiCall.CallApi(apiBase, apiCountry); if (!string.IsNullOrEmpty(response)) { //Deserializing the response recieved from web api and storing CountryJson countryJson = JsonConvert.DeserializeObject <CountryJson>(response); if (countryJson.results.Any()) { using (_context) { try { foreach (CountryResponse result in countryJson.results) { if (await _context.Country.FirstOrDefaultAsync(c => c.Code == result.code) == null) { await _context.Country.AddAsync(new Country { Code = result.code, Name = result.name, Cities = result.cities, Count = result.count, Locations = result.locations }); } } await _context.SaveChangesAsync(); } catch (Exception e) {//log exception return(BadRequest(e.Message)); } } } } return(Ok(true)); }
public async Task <IActionResult> GetLog(string urlRequest) { ApiLogger apiLog = new ApiLogger(); apiLog.Id = Guid.NewGuid(); apiLog.RequestUrl = _apiConfig.Value.apiBase + urlRequest; apiLog.RequestDate = DateTime.Now; using (_context) { try { var dbResponse = _context.ApiLogger.Where(api => api.RequestUrl == apiLog.RequestUrl && api.ResponseDate >= DateTime.Now.AddMinutes(-60)) .OrderByDescending(api => api.ResponseDate) .FirstOrDefault(); if (dbResponse != null) { return(Ok(dbResponse.ResponseContent)); } ApiCaller apiCaller = new ApiCaller(); apiLog.ResponseContent = await apiCaller.CallApi(apiLog.RequestUrl); apiLog.ResponseDate = DateTime.Now; await _context.ApiLogger.AddAsync(apiLog); await _context.SaveChangesAsync(); } catch (Exception e) { return(BadRequest(e.Message)); } } return(Ok(apiLog.ResponseContent)); }