private async Task <User> DoTheNeedful(AthleteSummary arg, IEnumerable <StravaAuth> authedAthletes) { var activities = new List <ActivitySummary>(); var authToken = authedAthletes.ToList().FirstOrDefault(c => c.Athlete.Id == arg.Id); if (authToken != null) { var userActivities = await GetActivityByUser(arg.Id, authToken.AccessToken); activities.AddRange(userActivities); } var athleteClient = new AthleteClient(StaticAuthentication); var athlete = await athleteClient.GetAthleteAsync(arg.Id.ToString()); var user = new User { ProfilePic = athlete.ProfileMedium, StravaId = arg.Id, FirstName = arg.FirstName, LastName = arg.LastName, ActivityCount = activities.Count(), TotalMiles = activities.Sum(e => Convert.ToDecimal(e.Distance * 0.000621371)), Stats = GetStatsForUser(activities).ToList() }; return(user); }
/// <summary> /// Gets some stats of the currently authenticated user. /// </summary> /// <returns>Strava statistics of the currently authenticated user.</returns> public async Task<Stats> GetStatsAsync() { // Get the athlete AthleteClient client = new AthleteClient(base.Authentication); Athlete a = await client.GetAthleteAsync(); return await GetStatsAsync(a.Id.ToString()); }
/// <summary> /// Initializes a new instance of the StravaClient class. /// </summary> /// <param name="authenticator">The IAuthentication object that holds a valid Access Token.</param> /// <seealso cref="WebAuthentication"/> /// <seealso cref="StaticAuthentication"/> public StravaClient(IAuthentication authenticator) { if (authenticator != null) { _authenticator = authenticator; Activities = new ActivityClient(authenticator); Athletes = new AthleteClient(authenticator); Clubs = new ClubClient(authenticator); Gear = new GearClient(authenticator); Segments = new SegmentClient(authenticator); Streams = new StreamClient(authenticator); Uploads = new UploadClient(authenticator); Efforts = new EffortClient(authenticator); Stats = new StatsClient(authenticator); } else { throw new ArgumentException("The IAuthentication object must not be null."); } }
public GetAllAthletesTests(CustomWebApplicationFactory <Startup> factory) { _client = new AthleteClient(factory); }
public string InitSession() { HttpStatusCode httpStatus; // No idea why from time to time the login fails. It does // not appear that it is a failed login, but instead // strava is ignoring the post and is just re-rendering the // login page. - must have something to do with the authenticity token or something. var loggedIn = false; for (var tryCount = 0; tryCount < 3; tryCount++) { this._cookieHolder = new CookieContainer(); var loginPage = this.HttpGet("https://www.strava.com/login", out httpStatus); if (httpStatus != HttpStatusCode.OK) { return("Unable to contact the Strava server."); } // Attempt to parse the table. var loader = new HtmlAgilityPack.HtmlDocument(); loader.LoadHtml(loginPage); AttributeFinder finder = (doc, name) => { var element = (from n in doc.DocumentNode.Descendants("input") where n.GetAttributeValue("name", string.Empty) == name select n).FirstOrDefault(); if (element == null) { return(string.Empty); } var attb = element.Attributes.FirstOrDefault(f => f.Name == "value"); if (attb == null) { return(string.Empty); } return(attb.Value); }; var authTok = finder(loader, "authenticity_token"); var utf8 = finder(loader, "utf8"); var loginResultPage = this.HttpPost( "https://www.strava.com/session", string.Format( "email={0}&password={1}&authenticity_token={2}&utf8=%E2%9C%93", HttpUtility.HtmlEncode(this._userName), HttpUtility.HtmlEncode(this._password), authTok), out httpStatus); if (httpStatus != HttpStatusCode.OK) { return("Unable to contact the Strava server."); } loader = new HtmlAgilityPack.HtmlDocument(); loader.LoadHtml(loginResultPage); var htmlObj = loader.DocumentNode.Descendants("html").FirstOrDefault(); if (htmlObj == null) { return("Unexpected result returned from Strava Server"); } if (htmlObj.GetAttributeValue("class", string.Empty).Split(' ').Any(a => a.ToLower() == "logged-in")) { loggedIn = true; break; } // Not logged in. // Need to see if an 'alert-message' exists. htmlObj = (from n in loader.DocumentNode.Descendants("div") where n.GetAttributeValue("class", string.Empty) == "alert-message" select n).FirstOrDefault(); if (htmlObj != null && htmlObj.InnerHtml.ToLower().Contains("did not match")) { // If this exists, the error is wrong pwd. break; } if (htmlObj != null && htmlObj.InnerHtml.ToLower().Contains("expired")) { // Session expired... loop around again. // Is this a .net thing? I'm creating a new cookie container...whatever, I // guess this hack seems to work by looping and just trying again. } } if (!loggedIn) { return("The login details provider do not appear to be valid."); } // We're now logged in. // Next, we need to be sure we have a valid API token. this._auth = new StaticAuthentication(this._apiKey); var athlete = new AthleteClient(this._auth); try { var details = athlete.GetAthlete(); if (details == null || details.Email == null || details.Email.ToLower() != this._userName.ToLower()) { return("The API key is not associated to the same Strava account as the user name provided."); } } catch { return("The API key provided does not appear to be valid"); } return(string.Empty); }
public DeleteAthleteTests(CustomWebApplicationFactory <Startup> factory) { _athleteClient = new AthleteClient(factory); _userClient = new UserClient(factory); }
public CreateAthleteTests(CustomWebApplicationFactory <Startup> factory) { _client = new AthleteClient(factory); }