Skip to content

Instantly share code, notes, and snippets.

@hbulens
Created January 11, 2018 20:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hbulens/8fe4bdcc278a501d31355ae1fd7f9369 to your computer and use it in GitHub Desktop.
Save hbulens/8fe4bdcc278a501d31355ae1fd7f9369 to your computer and use it in GitHub Desktop.
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