Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.