public async Task <DateSignificanceData> RetrieveDateSigificanceData(DateTime date) { var outcome = await PollyUtility.ExecuteWebRequest(async() => { var urlTemplate = _configuration.Settings["dateSignificanceUrl"]; var url = string.Format(urlTemplate, date.ToString("yyyy/M/d", CultureInfo.InvariantCulture)); var request = WebRequest.CreateHttp(url); var response = await request.GetResponseAsync(); using (var stream = response.GetResponseStream()) using (var reader = new StreamReader(stream)) { var data = JsonConvert.DeserializeObject <DateSignificanceData>(await reader.ReadToEndAsync()); return(data); } }); if (outcome.Result == null) { return(DateSignificanceData.Empty); } return(outcome.Result); }
public async Task <DepartureData> RetrieveDepartureData(int count) { var outcome = await PollyUtility.ExecuteWebRequest(async() => { var url = string.Format(_configuration.Settings["departuresDataUrl"], count); var request = WebRequest.CreateHttp(url); var response = await request.GetResponseAsync(); using (var stream = response.GetResponseStream()) using (var reader = new StreamReader(stream)) { var json = await reader.ReadToEndAsync(); var data = JsonConvert.DeserializeObject <DepartureData>(json); return(data); } }); if (outcome.Result == null) { return(DepartureData.Empty); } return(outcome.Result); }
public async Task <IEnumerable <Forecast> > RetrieveForcastWeatherData() { var outcome = await PollyUtility.ExecuteWebRequest(async() => { var url = _configuration.Settings["forcastDataUrl"]; var request = WebRequest.CreateHttp(url); var response = await request.GetResponseAsync(); using (var stream = response.GetResponseStream()) using (var reader = new StreamReader(stream)) { var data = JsonConvert.DeserializeObject <WeatherForecast>(await reader.ReadToEndAsync()); return(data.list.Select(f => new Forecast { Temperature = f.main.temp, MinTemperature = f.main.temp_min, MaxTemperature = f.main.temp_max, WeatherTypeId = f.weather.FirstOrDefault()?.id, Date = UnixTimeStampUtility.UnixTimeStampToDateTime(f.dt) })); } }); if (outcome.Result == null) { return(new List <Forecast>()); } return(outcome.Result); }
public async Task <CurrentWeatherConditions> RetrieveCurrentWeatherData() { var outcome = await PollyUtility.ExecuteWebRequest(async() => { var url = _configuration.Settings["weatherDataUrl"]; var request = WebRequest.CreateHttp(url); var response = await request.GetResponseAsync(); using (var stream = response.GetResponseStream()) using (var reader = new StreamReader(stream)) { var data = JsonConvert.DeserializeObject <CurrentWeatherConditions>(await reader.ReadToEndAsync()); return(data); } }); if (outcome.Result == null) { return(CurrentWeatherConditions.Empty); } return(outcome.Result); }
private async Task Login() { var parameters = new LoginParameters { GrantType = "password", ClientId = _configuration.Settings["clientId"], ClientSecret = _configuration.Settings["clientSecret"], Email = _configuration.Settings["email"], Password = _configuration.Settings["password"] }; var outcome = await PollyUtility.ExecuteWebRequest(async() => { return(await _api.PostAsync <Session>("oauth/token", parameters)); }); if (outcome.Outcome == OutcomeType.Successful) { _currentSession = outcome.Result; } }
public async Task <ChargeState> GetChargeState(string vehicleId) { if (!IsLoggedIn) { await Login(); } var path = $"api/1/vehicles/{vehicleId}/data_request/charge_state"; var outcome = await PollyUtility.ExecuteWebRequest(async() => { return(await _api.GetAsync <ChargeState>(path, new Dictionary <string, string> { { "Authorization", AuthorizationHeader } })); }); if (outcome.Outcome == OutcomeType.Failure) { return(ChargeState.Empty); } return(outcome.Result); }
public async Task <IReadOnlyCollection <EventData> > RetrieveCalendarData() { var outcome = await PollyUtility.ExecuteWebRequest(async() => { var startTime = DateTime.Today; var endTime = startTime.AddHours(23).AddMinutes(59).AddSeconds(59); var url = _configuration.Settings["agendaDataUrl"]; var request = WebRequest.CreateHttp(url); var response = await request.GetResponseAsync(); using (var stream = response.GetResponseStream()) using (var reader = new StreamReader(stream)) { var calendarText = reader.ReadToEnd(); var regEx = new Regex(EVENT_PATTERN); var matches = regEx.Matches(calendarText); var events = new List <EventData>(); foreach (Match match in matches) { using (var eventReader = new StringReader(match.Value)) { string line = null; var values = new Dictionary <string, string>(); do { line = eventReader.ReadLine(); if (line != null) { var parts = line.Split(new[] { ':' }, 2); if (parts.Length != 2) { continue; } var key = parts[0].Split(';')[0]; values[key] = parts[1]; } }while (line != null); var @event = new EventData { Subject = values[SUMMARY_KEY], Start = GetDate(values[START_DATE_KEY]), End = GetDate(values[END_DATE_KEY]), IsAllDay = GetIsAllDay(values) }; events.Add(@event); } } return(events); } }); if (outcome.Result == null) { return(new List <EventData>()); } return(outcome.Result); }