public FoodBanksModel GetFoodBanks( int start = 0, int total = 5 ) { var retVal = new FoodBanksModel(); try { var client = new RestClient( ConfigurationManager.AppSettings[ "WebApiBaseUrlV1" ] ); var request = new RestRequest( "api/v1/FoodBanks/", Method.GET ); request.AddParameter( "Authorization", AuthorizationInformation, ParameterType.HttpHeader ); request.AddParameter( "offset", start ); request.AddParameter( "limit", total ); var response = client.Execute<JsonReturn>( request ); if ( response != null && response.Data != null ) { JsonReturn jReturn = response.Data; var data = ( RestSharp.JsonArray )jReturn.Result; //Main data. var banks = new List<FoodBank>(); foreach ( var item in data ) { var result = ( System.Collections.Generic.Dictionary<string, object> )item; var bank = new FoodBank(); if ( result[ "addr1" ] != null ) bank.Addr1 = result[ "addr1" ].ToString(); if ( result[ "addr2" ] != null ) bank.Addr2 = result[ "addr2" ].ToString(); if ( result[ "city" ] != null ) bank.City = result[ "city" ].ToString(); if ( result[ "created" ] != null ) bank.Created = DateTime.Parse( result[ "created" ].ToString() ); if ( result[ "donationCount" ] != null ) bank.DonationCount = int.Parse( result[ "donationCount" ].ToString() ); if ( result[ "id" ] != null ) bank.Id = int.Parse( result[ "id" ].ToString() ); if ( result[ "name" ] != null ) bank.Name = result[ "name" ].ToString(); if ( result[ "postal" ] != null ) bank.Postal = result[ "postal" ].ToString(); if ( result[ "state" ] != null ) bank.State = result[ "state" ].ToString(); if ( result[ "volunteerCount" ] != null ) bank.VolunteerCount = int.Parse( result[ "volunteerCount" ].ToString() ); banks.Add( bank ); } retVal.Links = ( List<Link> )jReturn.Links;//Links. //Get header(s). foreach(var header in response.Headers) { if (header.Name != "X-Total-Count") continue; retVal.TotalFoodBanks = int.Parse( header.Value.ToString() ); break; } retVal.FoodBanks = banks; } } catch ( Exception ex ) { retVal = new FoodBanksModel(); } return retVal; }
public FoodBanksModel GetFoodBank( int id ) { var retVal = new FoodBanksModel(); try { var client = new RestClient( ConfigurationManager.AppSettings[ "WebApiBaseUrlV1" ] ); var request = new RestRequest( "api/v1/FoodBanks/" + id, Method.GET ); request.AddParameter( "Authorization", AuthorizationInformation, ParameterType.HttpHeader ); var response = client.Execute<JsonReturn>( request ); if ( response != null && response.Data != null ) { JsonReturn jReturn = response.Data; var result = ( System.Collections.Generic.Dictionary<string, object> )jReturn.Result; retVal.FoodBank = new FoodBank(); if ( result[ "addr1" ] != null ) retVal.FoodBank.Addr1 = string.IsNullOrEmpty( result[ "addr1" ].ToString() ) ? "" : result[ "addr1" ].ToString(); if ( result[ "addr2" ] != null ) retVal.FoodBank.Addr2 = result[ "addr2" ].ToString(); if ( result[ "city" ] != null ) retVal.FoodBank.City = result[ "city" ].ToString(); if ( result[ "created" ] != null ) retVal.FoodBank.Created = DateTime.Parse( result[ "created" ].ToString() ); if ( result[ "donationCount" ] != null ) retVal.FoodBank.DonationCount = int.Parse( result[ "donationCount" ].ToString() ); if ( result[ "id" ] != null ) retVal.FoodBank.Id = int.Parse( result[ "id" ].ToString() ); if ( result[ "name" ] != null ) retVal.FoodBank.Name = result[ "name" ].ToString(); if ( result[ "postal" ] != null ) retVal.FoodBank.Postal = result[ "postal" ].ToString(); if ( result[ "state" ] != null ) retVal.FoodBank.State = result[ "state" ].ToString(); if ( result[ "volunteerCount" ] != null ) retVal.FoodBank.VolunteerCount = int.Parse( result[ "volunteerCount" ].ToString() ); } } catch ( Exception ex ) { retVal = new FoodBanksModel(); } return retVal; }