public async Task <LatestPlayerDataModel> GetLasterPlayerById(string id) { DataResponseModel dataResponse = await GetPlayerData(); LatestPlayerDataModel latestPlayerDataModel = new LatestPlayerDataModel(); var playerReceiving = dataResponse.Receiving.SingleOrDefault(x => x.Id == id); if (playerReceiving != null) { latestPlayerDataModel.Receiving = new Receiving[1]; latestPlayerDataModel.Receiving[0] = new Receiving(); latestPlayerDataModel.Receiving[0].Player = playerReceiving; } var playerRushing = dataResponse.Rushing.SingleOrDefault(x => x.Id == id); if (playerRushing != null) { latestPlayerDataModel.Rushing = new Rushing[1]; latestPlayerDataModel.Rushing[0] = new Rushing(); latestPlayerDataModel.Rushing[0].Player = playerRushing; } return(latestPlayerDataModel); }
// Fetches the data and saves it in a cache. subsequent fetches within one week period are pulled from the cache. private async Task <DataResponseModel> GetPlayerData() { DataResponseModel dataResponseModel = new DataResponseModel(); dataResponseModel = cache.Get <DataResponseModel>(CacheEnum.PLAYERS.ToString()); if (dataResponseModel == null) { var handler = new HttpClientHandler() { AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate }; using (var client = new HttpClient(handler)) { client.Timeout = Timeout; var response = client.GetAsync(Configuration.GetSection("AppSettings")["ProviderUrl"]).Result; var stringData = response.Content.ReadAsStringAsync().Result; dataResponseModel = JsonConvert.DeserializeObject <DataResponseModel>(stringData, new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.Auto }); if (dataResponseModel != null) { cache.Set <DataResponseModel>(CacheEnum.PLAYERS.ToString(), dataResponseModel, DateTime.Now.AddMinutes(cacheTimeOutInMins)); return(dataResponseModel); } } } return(dataResponseModel); }
//Search for player by id in all categories i.e. Rushing,Passing,Receiving and Kicking public async Task <PlayerModel> GetPlayerById(string id) { DataResponseModel dataResponse = new DataResponseModel(); dataResponse = await GetPlayerData(); // Usimg Linq var player = dataResponse.Rushing.SingleOrDefault(x => x.Id == id); if (player != null) { return(player); } player = dataResponse.Passing.SingleOrDefault(x => x.Id == id); if (player != null) { return(player); } player = dataResponse.Receiving.SingleOrDefault(x => x.Id == id); if (player != null) { return(player); } player = dataResponse.Kicking.SingleOrDefault(x => x.Id == id); if (player != null) { return(player); } return(null); }
/** * <summary>asynsc/wait get the response data from Feed Providers API </summary> */ public async Task <DataResponseModel> GetResponseDataAsync() { var dataResponse = new DataResponseModel(); { var handler = new HttpClientHandler() { AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate }; using (var client = new HttpClient(handler)) { client.Timeout = Timeout; HttpResponseMessage response = await client.GetAsync(BaseURL); var stringData = response.Content.ReadAsStringAsync().Result; int statusCode = (int)response.StatusCode; //Check for Http status code if (statusCode == 200) { dataResponse = JsonConvert.DeserializeObject <DataResponseModel>(stringData, new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.Auto }); } else { dataResponse = null; } } return(dataResponse); } }
public override void OnException(ExceptionContext context) { if (IsApiController(context)) { var responseModel = new DataResponseModel { Data = context.Exception?.Message }; var dr = new ObjectResult(responseModel); if (context.Exception is KnownException knownExc) { dr.StatusCode = knownExc.Code != 0 ? knownExc.Code : 500; } else { Console.WriteLine("exception: " + context.Exception?.Message + " " + typeof(KnownException).Name); dr.StatusCode = 500; } context.Result = dr; } else { if (context.Exception is KnownException exc) { Console.WriteLine("CustomExceptionFilter.OnException: " + exc.Message); // var x = context.Result; // var vr = new ObjectResult(123);// {ViewName = "home/privacy"}; // // vr.ViewData = new ViewDataDictionary(context.cont); // context.Result = vr; } } }
public DataResponseModel ReadValues() { DataResponseModel objDataResponseModel = new DataResponseModel(); objDataResponseModel.Response_Status = "Failed"; objDataResponseModel.Response_Message = "Failed to Start the Operation"; objDataResponseModel.DataOutput = new List <DataModel>(); DataModel obj = new DataModel(); obj.Id = 1; obj.Name = "ABCD"; objDataResponseModel.DataOutput.Add(obj); DataModel obj1 = new DataModel(); obj1.Id = 2; obj1.Name = "EFG"; objDataResponseModel.DataOutput.Add(obj1); objDataResponseModel.Response_Status = "Success"; objDataResponseModel.Response_Message = "Successfully Read the Details"; return(objDataResponseModel); }
public IHttpActionResult eliminablog([FromUri] Blog Model) { DataResponseModel _oRet = new DataResponseModel(); try { // Realizo el alta de usuario. var uu = BlogData.DeleteBlog(Model); _oRet.IsSuccess = true; _oRet.Message = "Eliminación del blog de manera satisfactoria."; _oRet.StatusCode = (int)HttpStatusCode.OK; _oRet.Data = uu; } catch (Exception oEx) { _oRet.IsSuccess = false; _oRet.Message = "Error interno del servidor. Lamentamos las molestias ocasionadas. Favor de reportarlo al Administrador del Sistema. Detalle del error: " + oEx.Message.Trim(); _oRet.StatusCode = (int)HttpStatusCode.InternalServerError; } // Finalmente retornamos el resultado. // _oDb = null; return Ok(JsonConvert.SerializeObject(_oRet, Formatting.None)); return(Ok(_oRet)); }
public IHttpActionResult restaurarblog([FromUri] int id) { DataResponseModel _oRet = new DataResponseModel(); try { // Realizo el alta de usuario. var uu = BlogData.RestoreBlog(new Blog() { idBlog = id }); _oRet.IsSuccess = true; _oRet.Message = "Restauración del blog de manera satisfactoria."; _oRet.StatusCode = (int)HttpStatusCode.OK; _oRet.Data = uu; } catch (Exception oEx) { _oRet.IsSuccess = false; _oRet.Message = "Error interno del servidor. Lamentamos las molestias ocasionadas. Favor de reportarlo al Administrador del Sistema. Detalle del error: " + oEx.Message.Trim(); _oRet.StatusCode = (int)HttpStatusCode.InternalServerError; } // Finalmente retornamos el resultado. return(Ok(_oRet)); }
public void OnResultExecuting(ResultExecutingContext context) { if (context.Filters.OfType <NoResultWrapperAttribute>().Any()) { return; } if (context.Result is ObjectResult objectResult) { var oldValue = objectResult.Value; var newValue = new DataResponseModel(oldValue); objectResult.Value = newValue; } }
public override PlayerModel Load(DataResponseModel dataResponse, PlayerModel player, string id) { //create a player object if a kick skill exists with the expected player id //and if player is null, this approach ensures that even if there is no matching player id // in the any of the collections above for eg they could be empty, our code wil still run player = CreatePlayer(dataResponse.Kicking.FirstOrDefault(x => x.PlayerId == id), player, id); //if player object exists, add the receive skill entries if (player != null) { /* * The data returned for a player id contains data details of other players, this seems unusual * I assume that the request is asking for * actions(rushes, kickings, passings, receivings) of the player with the supplied id. * For this reason, before persisting this information in the redis cache, i clean out entries that do not have the player id of interest * thats the reason for the where clause */ dataResponse.Kicking.Where(x => x.PlayerId == player.Id).Distinct().ToList().ForEach(kick => { player.Kicks.Add(kick); }); } return(player); }
private PlayerModel GetPlayer(string id, DataResponseModel dataResponse) { var player = GetFirstPlayer(id, dataResponse.Rushing); if (player != null) { return(player); } player = GetFirstPlayer(id, dataResponse.Passing); if (player != null) { return(player); } player = GetFirstPlayer(id, dataResponse.Receiving); if (player != null) { return(player); } return(GetFirstPlayer(id, dataResponse.Kicking)); }
private LatestPlayersModel GetLatestPlayer(IEnumerable <string> ids, DataResponseModel dataResponse) { var rushing = new List <RushingPlayerModel>(); var passing = new List <PassingPlayerModel>(); var receiving = new List <ReceivingPlayerModel>(); var kicking = new List <KickingPlayerModel>(); foreach (var id in ids) { AppendPlayerData(id, dataResponse.Rushing, rushing); AppendPlayerData(id, dataResponse.Passing, passing); AppendPlayerData(id, dataResponse.Receiving, receiving); AppendPlayerData(id, dataResponse.Kicking, kicking); } ; return(new LatestPlayersModel() { Rushing = NullIfEmpty(rushing), Passing = NullIfEmpty(passing), Receiving = NullIfEmpty(receiving), Kicking = NullIfEmpty(kicking) }); }
public abstract PlayerModel Load(DataResponseModel dataResponse, PlayerModel playerModel, string id);
private async Task <DataResponseModel> GetDataFromEndpoint() { HttpResponseMessage response; // the number of parallel request that can be made to the data endpoint. int maxParallelism = Convert.ToInt16(_config["maxParallelism"]); var dataResponse = new DataResponseModel(); // The number of days the response from the data endpoint int timeToLiveSeconds = Convert.ToInt32(_config["DataUrlTimeToLive"]); var cachedResponse = await _iResponseCacheService.GetCachedResponseAsync(_dataUrl); if (string.IsNullOrEmpty(cachedResponse)) { // The handler for the number of requests var handler = new ThrottlingHandler(new SemaphoreSlim(maxParallelism), new HttpClientHandler { AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate, }); using (var client = new HttpClient(handler)) { client.Timeout = Timeout; response = await client.GetAsync(_dataUrl); // This checks if the request was successfull or times out. if (response.IsSuccessStatusCode) { cachedResponse = response.Content.ReadAsStringAsync().Result; dataResponse = JsonConvert.DeserializeObject <DataResponseModel>( cachedResponse, new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.Auto } ); await _iResponseCacheService.CacheResponseAsync(_dataUrl, dataResponse, TimeSpan.FromSeconds(timeToLiveSeconds)); return(dataResponse); } else { throw new CustomResponseException("Endpoint Could not be reached."); } } } else { return(JsonConvert.DeserializeObject <DataResponseModel>( cachedResponse, new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.Auto } )); } }