Getting a list of activities of the authenticated user

In the Strava API, the Authlete endpoint may be used to get various information about the authenticated user. The example below illustrates how we can use an StravaHttpService instance to get the most recent activities of the auhenticated user:

@using Skybrud.Social.Strava
@using Skybrud.Social.Strava.Models.Activities
@using Skybrud.Social.Strava.Responses.Activities

@inherits Microsoft.AspNetCore.Mvc.Razor.RazorPage<StravaHttpService>

@{

    // Make the request to the Strava API
    StravaActivityListResponse response = await Model.Athlete.GetActivitiesAsync();

    // Iterate through the activies in the response
    foreach (StravaActivitySummary activity in response.Body) {
        <hr />
        <div>
            <strong>@activity.Name</strong><br />
            @activity.Type
            -
            @(activity.Distance / 1000d) km
            -
            @(activity.AverageSpeed * 3600 / 1000) kph
        </div>
    }

}

The distance of each activity is returned in metres, which we can convert to kilometres for better readability. In a similar way, the average speed is returned in metres per second, which we can convert to kilometres per hour as well for better readability.

If you wish to control what activies are returned, the GetActivitiesAsync method has an overload taking an instance of StravaGetAthleteActiviesOptions. For one, this allows you to specify a lower date and an upper date that the returned activies should match. By default, the Strava API will return 30 activities, but you can also specify how many activities that should be returned (the maximum seems to be 200):

@using Skybrud.Social.Strava
@using Skybrud.Social.Strava.Models.Activities
@using Skybrud.Social.Strava.Options.Athletes
@using Skybrud.Social.Strava.Responses.Activities
@using Skybrud.Essentials.Time

@inherits Microsoft.AspNetCore.Mvc.Razor.RazorPage<StravaHttpService>

@{

    // Declare the options for the request to the API
    StravaGetAthleteActiviesOptions options = new() {
        After = new EssentialsTime(2024, 3, 1, 0, 0, 0, TimeZoneInfo.Local),
        Before = new EssentialsTime(2024, 3, 30, 23, 59, 59, TimeZoneInfo.Local),
        Page = 1,
        PerPage = 200
    };

    // Make the request to the Strava API
    StravaActivityListResponse response = await Model.Athlete.GetActivitiesAsync(options);

    // Iterate through the activies in the response
    foreach (StravaActivitySummary activity in response.Body) {
        <hr />
        <div>
            <strong>@activity.Name</strong><br />
            @activity.Type
            -
            @($"{activity.Distance / 1000d:N2}") km
            -
            @($"{activity.AverageSpeed * 3600 / 1000:N2}") kph
        </div>
    }

}