Exemple #1
0
        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));
        }
Exemple #2
0
        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));
        }