This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private T GetStravaData < T > (string url) { | |
try | |
{ | |
HttpWebRequest request = (HttpWebRequest) WebRequest.Create(url); | |
using(WebResponse response = request.GetResponse()) | |
{ | |
using(Stream responseStream = response.GetResponseStream()) | |
{ | |
using(StreamReader reader = new StreamReader(responseStream, Encoding.UTF8)) | |
{ | |
Task < string > result = reader.ReadToEndAsync(); | |
T rides = JsonConvert.DeserializeObject < T > (result.Result); | |
return rides; | |
} | |
} | |
} | |
} | |
catch (WebException ex) | |
{ | |
using(WebResponse errorResponse = ex.Response) | |
{ | |
using(Stream responseStream = errorResponse.GetResponseStream()) | |
{ | |
using(StreamReader reader = new StreamReader(responseStream, Encoding.GetEncoding("utf-8"))) | |
{ | |
string errorText = reader.ReadToEnd(); | |
} | |
} | |
} | |
throw; | |
} | |
catch (Exception ex) | |
{ | |
throw; | |
} | |
} | |
public IEnumerable < Activity > GetStravaRides() | |
{ | |
try | |
{ | |
Configuration accessTokenConfig = this.ConfigRepository.FindOne(x => x.Key == ConfigKeys.Strava_AccessToken); | |
string url = string.Format("https://www.strava.com/api/v3/athlete/activities?access_token={0}&per_page=200", accessTokenConfig.Value); | |
return this.GetStravaData < IEnumerable < Activity >> (url); | |
} | |
catch (Exception ex) | |
{ | |
return new List < Activity > (); | |
} | |
} | |
public Athlete GetAthlete() | |
{ | |
Configuration accessTokenConfig = this.ConfigRepository.FindOne(x => x.Key == ConfigKeys.Strava_AccessToken); | |
string url = string.Format("https://www.strava.com/api/v3/athlete?access_token={0}&per_page=200", accessTokenConfig.Value); | |
return this.GetStravaData < Athlete > (url); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment