/// <summary> /// Gets an authentication token from the strava API. /// </summary> /// <returns> A Strava Authentication token</returns> public async Task <StravaAuthenticationToken> GetStravaAuthToken() { await StravaAuthServer.Start(); var exchangeToken = string.Empty; var authTokenAsString = string.Empty; // Temporary auth server lsitens for Strava callback. StravaAuthServer.AuthorizationCodeReceived += async(sender, response) => { await StravaAuthServer.Stop(); exchangeToken = response.Code; var client = new RestClient("https://www.strava.com/oauth/token?client_id=61391&client_secret=8b0eb19e37bbbeffc8b8ba75efdb1b7f9c2cfc95&grant_type=authorization_code"); var request = new RestRequest(Method.POST); request.AddParameter("code", exchangeToken); IRestResponse accessTokenResponse = client.Execute(request); stravaAuthToken = JsonConvert.DeserializeObject <StravaAuthenticationToken>(accessTokenResponse.Content); }; // Open page for login request var authTokenUri = new Uri("http://www.strava.com/oauth/authorize?client_id=61391&response_type=code&redirect_uri=http://localhost:5001/stravatoken&approval_prompt=force&scope=activity:read_all"); BrowserUtil.Open(authTokenUri); Task.Delay(20000).Wait(); return(stravaAuthToken); }
public JsonResult GetStravaRecentActivities(string access_token, DateTimeOffset?start_date = null, DateTimeOffset?end_date = null) { var tempToken = new StravaAuthenticationToken { access_token = access_token }; var activityHistoryJsonResult = this.dataSource.GetStravaActivityHistory(tempToken).Result; if (start_date == null && end_date == null) { return(activityHistoryJsonResult); } List <StravaActivity> rawActivityHistory = (List <StravaActivity>)activityHistoryJsonResult.Value; List <StravaActivity> correctActivityHistory = new List <StravaActivity>(); if (start_date == null && end_date != null) { DateTimeOffset extractedEndDate = (DateTimeOffset)end_date; foreach (var item in rawActivityHistory) { if (item.start_date <= extractedEndDate) { correctActivityHistory.Add(item); } } return(new JsonResult(correctActivityHistory)); } if (start_date != null && end_date == null) { DateTimeOffset extractedStartDate = (DateTimeOffset)start_date; foreach (var item in rawActivityHistory) { if (item.start_date >= extractedStartDate) { correctActivityHistory.Add(item); } } return(new JsonResult(correctActivityHistory)); } DateTimeOffset startDate = (DateTimeOffset)start_date; DateTimeOffset endDate = (DateTimeOffset)end_date; foreach (var activity in rawActivityHistory) { if (activity.start_date >= startDate && activity.start_date <= endDate) { correctActivityHistory.Add(activity); } } return(new JsonResult(correctActivityHistory)); }
/// <inheritdoc/> public async Task <JsonResult> GetStravaActivityHistory(StravaAuthenticationToken authToken) { await Task.Delay(0); var client = new RestClient("https://www.strava.com/api/v3/athlete/activities"); var request = new RestRequest(Method.GET); request.AddParameter("access_token", authToken.access_token); IRestResponse response = client.Execute(request); var retrievedActivites = JsonConvert.DeserializeObject <List <StravaActivity> >((string)response.Content); List <StravaActivity> listOfRuns = new List <StravaActivity>(); foreach (var activity in retrievedActivites) { if (activity.type == "Run") { listOfRuns.Add(activity); } } return(new JsonResult(listOfRuns)); }
public void GetStravaRecentActivities_RunningActivitiesRetrieved() { // Get Strava Token var stravaTokenAsJson = sut.GetStravaAuthenticationToken(); stravaTokenAsJson.Result.Should().NotBeNull(); stravaTokenAsJson.Result.Value.Should().NotBe(string.Empty); stravaAuthenticationToken = JsonConvert.DeserializeObject <StravaAuthenticationToken>((string)stravaTokenAsJson.Result.Value); stravaAuthenticationToken.access_token.Should().NotBeNullOrEmpty(); // Get activities var runningHistory = sut.GetStravaActivityHistory(stravaAuthenticationToken); runningHistory.Result.Value.Should().NotBeNull(); runningHistory.Result.Value.Should().NotBe(string.Empty); List <StravaActivity> retrievedActivites = (List <StravaActivity>)runningHistory.Result.Value; retrievedActivites.Should().NotBeEmpty().And.Should().NotBeNull(); foreach (var activity in retrievedActivites) { activity.type.Should().Be("Run"); } }
public void SetUpTests() { var now = DateTime.UtcNow; var now_local = DateTime.Now; var offset = -2; foreach (var item in PlayHistoryItems) { item.PlayedAt = now.AddDays(offset); offset++; } foreach (var item in ActivityItems) { item.start_date = now; item.start_date_local = now_local; } foreach (var item in FitBitActivityItems) { item.StartTime = now; item.OriginalStartTime = now; item.LastModified = now; } offset = -1; foreach (var item in LastFMTrackItems) { item.TimePlayed = now.AddDays(offset); offset++; } RegisterMusicHistory(PlayHistoryItems); RegisterActivityHistory(ActivityItems); RegisterFitBitHistory(FitBitActivityItems); RegisterLastFMTracks(LastFMTrackItems); sut = MakeSut(); // Get spotify auth token. var spotifyAuthTask = sut.GetSpotifyAuthenticationToken(); spotifyAuthTask.Result.Should().NotBeNull(); spotifyAuthTask.Result.Value.Should().NotBe(string.Empty); var temp = JsonConvert.SerializeObject(spotifyAuthTask.Result.Value); spotifyAuthToken = JsonConvert.DeserializeObject <SpotifyAuthenticationToken>(temp); spotifyAuthToken.AccessToken.Should().NotBeNullOrEmpty(); // Get strava auth token. var stravaAuthTask = sut.GetStravaAuthenticationToken(); stravaAuthTask.Result.Should().NotBeNull(); stravaAuthTask.Result.Value.Should().NotBeNull(); temp = JsonConvert.SerializeObject(stravaAuthTask.Result.Value); stravaAuthToken = JsonConvert.DeserializeObject <StravaAuthenticationToken>(temp); stravaAuthToken.access_token.Should().NotBeNullOrEmpty(); // Get FitBit auth token. var fitBitAuthTask = sut.GetFitBitAuthenticationToken(); fitBitAuthTask.Result.Should().NotBeNull(); fitBitAuthTask.Result.Value.Should().NotBeNull(); temp = JsonConvert.SerializeObject(fitBitAuthTask.Result.Value); fitBitAuthToken = JsonConvert.DeserializeObject <FitBitAuthenticationToken>(temp); fitBitAuthToken.AccessToken.Should().NotBeNullOrEmpty(); }
/// <inheritdoc/> public async Task <JsonResult> GetStravaActivityHistory(StravaAuthenticationToken authToken) { await Task.Delay(0); var activityHistory = externalAPICaller.Get <List <FakeResponseServer.DTO.Activity> >(new Uri("http://localhost:2222/v3/athlete/activities")); var correctActivityHistory = new List <StravaActivity>(); foreach (var item in activityHistory) { correctActivityHistory.Add(new StravaActivity { achievement_count = item.achievement_count, athlete = new StravaAthlete { badge_type_id = item.athlete.badge_type_id, city = item.athlete.city, country = item.athlete.country, created_at = item.athlete.created_at, firstname = item.athlete.firstname, follower = item.athlete.follower, friend = item.athlete.friend, id = item.athlete.id, lastname = item.athlete.lastname, premium = item.athlete.premium, profile = item.athlete.profile, profile_medium = item.athlete.profile_medium, resource_state = item.athlete.resource_state, sex = item.athlete.sex, state = item.athlete.state, summit = item.athlete.summit, updated_at = item.athlete.updated_at, username = item.athlete.username }, athlete_count = item.athlete_count, average_cadence = item.average_cadence, average_heartrate = item.average_heartrate, average_speed = item.average_speed, average_temp = item.average_temp, comment_count = item.comment_count, commute = item.commute, display_hide_heartrate_option = item.display_hide_heartrate_option, distance = item.distance, elapsed_time = item.elapsed_time, elev_high = item.elev_high, elev_low = item.elev_low, end_latlng = item.end_latlng, external_id = item.external_id, flagged = item.flagged, from_accepted_tag = item.from_accepted_tag, gear_id = item.gear_id, has_heartrate = item.has_heartrate, has_kudoed = item.has_kudoed, heartrate_opt_out = item.heartrate_opt_out, id = item.id, kudos_count = item.kudos_count, location_city = item.location_city, location_country = item.location_country, location_state = item.location_state, manual = item.manual, map = new StravaMap { id = item.map.id, resource_state = item.map.resource_state, summary_polyline = item.map.summary_polyline }, max_heartrate = item.max_heartrate, max_speed = item.max_speed, moving_time = item.moving_time, name = item.name, photo_count = item.photo_count, Private = item.Private, pr_count = item.pr_count, resource_state = item.resource_state, start_date = item.start_date, start_date_local = item.start_date_local, start_latitude = item.start_latitude, start_latlng = item.start_latlng, start_longitude = item.start_longitude, timezone = item.timezone, total_elevation_gain = item.total_elevation_gain, total_photo_count = item.total_photo_count, trainer = item.trainer, type = item.type, upload_id = item.upload_id, upload_id_str = item.upload_id_str, utc_offset = item.utc_offset, visibility = item.visibility, workout_type = item.workout_type, }); } return(new JsonResult(correctActivityHistory)); }