strava

package module
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 6, 2022 License: MIT Imports: 9 Imported by: 1

README

Go Reference GitHub go.mod Go version GitHub release (latest by date) Go Report Card Actions Status Strava API Version

Strava API Client

A client to access APIs provided by Strava.

API Version

This client is implemented for Strave API v3.

Authtication

Strave uses OAuth2 for their APIs. This client uses TokenSource from oauth2 for authentication. Have a look at Getting Started with the Strava API to get details about how to authenticate your app to access the API. Base of OAuth2 authentication is oauth2.Config where you specifiy your client id, client secret and scopes.

Helper

There're to helper to create required token source. Both methods expect a oauth2.Config.

TokenSourceFromAuthorizationCode

This method uses Exchange method on oauth2 config to get access and refresh token for an authorization code. You should persist your refresh token somewhere, because an authorization code can be used only once.

TokenSourceFromRefreshToken

If you already have a refresh token you can use this method to create a token source for it.

What about all the other models and endpoints?

Scope of this client is limited to models and endpoints I'm using in my project atm. Feel free to create an enhancement issue to request an extension.

Documentation

Index

Constants

View Source
const BASE_URL = "https://www.strava.com/api/v3"

BASE_URL for Strava API v3.

View Source
const TOKEN_ENDPOINT = "https://www.strava.com/oauth/token"

TOKEN_ENDPOINT is used to convert authorization code to token or retrieve a new access token.

Variables

This section is empty.

Functions

func TokenSourceFromAuthorizationCode

func TokenSourceFromAuthorizationCode(oauthConfig oauth2.Config, authCode string) (oauth2.TokenSource, error)

TokenSourceFromAuthorizationCode is a helper to get a token source for an athorization code. HAve a look at to get more details how to obtain an authorization code for your app. Please persist returned refresh token, because an auth code can be used only once.

func TokenSourceFromRefreshToken

func TokenSourceFromRefreshToken(oauthConfig oauth2.Config, refreshToken string) (oauth2.TokenSource, error)

TokenSourceFromRefreshToken is a helper to create a token source for an existing refresh token.

Types

type ActivityStats

type ActivityStats struct {

	// The longest distance ridden by the athlete.
	BiggestRideDistance float64 `json:"biggest_ride_distance"`

	// double	The highest climb ridden by the athlete.
	BiggestGlimbElevationGain float64 `json:"biggest_climb_elevation_gain"`

	// The recent (last 4 weeks) ride stats for the athlete.
	RecentRideTotals ActivityTotal `json:"recent_ride_totals"`

	// The recent (last 4 weeks) run stats for the athlete.
	RecentRunTotals ActivityTotal `json:"recent_run_totals"`

	// The recent (last 4 weeks) swim stats for the athlete.
	RecentSwimTotals ActivityTotal `json:"recent_swim_totals"`

	// The year to date ride stats for the athlete.
	YearToDateRideTotals ActivityTotal `json:"ytd_ride_totals"`

	// The year to date run stats for the athlete.
	YearToDateRunTotals ActivityTotal `json:"ytd_run_totals"`

	// The year to date swim stats for the athlete.
	YearToDateSwimTotals ActivityTotal `json:"ytd_swim_totals"`

	// The all time ride stats for the aTthlete.
	AllRideotals ActivityTotal `json:"all_ride_totals"`

	// The all time run stats for the athlete.
	AllRunTotals ActivityTotal `json:"all_run_totals"`

	// The all time swim stats for the athlete
	AllSwimTotals ActivityTotal `json:"all_swim_totals"`
}

A set of rolled-up statistics and totals for an athlete

type ActivityTotal

type ActivityTotal struct {

	// The number of activities considered in this total.
	Count int64 `json:"count"`

	// The total distance covered by the considered activities in meters.
	Distance float64 `json:"distance"`

	// The total moving time of the considered activities in seconds.
	MovingTime int64 `json:"moving_time"`

	// The total elapsed time of the considered activities in seconds.
	ElapsedTime int `json:"elapsed_time"`

	// The total elevation gain of the considered activities.
	ElevationGain float64 `json:"elevation_gain"`

	// The total number of achievements of the considered activities.
	AchievementCount int `json:"achievement_count"`
}

A roll-up of metrics pertaining to a set of activities. Values are in seconds and meters.

type Client

type Client struct {
	// contains filtered or unexported fields
}

StravaClient handles request to Strava APIs.

func New

func New(tokenSource oauth2.TokenSource) *Client

New returna an api client with BASE_URL as default.

func (*Client) AthleteActivities

func (client *Client) AthleteActivities(timeFilter *TimeFilter, pagination *Pagination) (*[]SummaryActivity, error)

AthleteActivities lists available activities for an athlete. You can use timeFilter to retrice time range activities should be requested for. Pagination param can be used if retrieve activities step by step if there're a lot of them.

func (*Client) AthleteStats

func (client *Client) AthleteStats() (*ActivityStats, error)

AthleteStats returns summarited athlete stats, related to current year or in total.

func (*Client) AuthorizedAthlete

func (client *Client) AuthorizedAthlete() (*DetailedAthlete, error)

AuthorizedAthlete try to fetch current athelete, defined by used auth tokens, from Strava.

func (*Client) WithAthleteId

func (client *Client) WithAthleteId(athleteId int64)

WithAthleteId assigns given athlete id. This id will be used for all further requests.

func (*Client) WithBaseUrl

func (client *Client) WithBaseUrl(baseUrl string)

WithBaseUrl set given url as base for all api calls.

type DetailedAthlete

type DetailedAthlete struct {

	// The unique identifier of the athlete
	Id int64 `json:"id"`
}

DetailedAthlete contains defail information of an athlete.

type Error

type Error struct {

	// The code associated with this error.
	Code string `json:"code"`

	// The specific field or aspect of the resource associated with this error.
	Field string `json:"field"`

	// The type of resource associated with this error.
	Resource string `json:"resource"`
}

Error occurred in an api call.

type Fault

type Fault struct {

	// The set of specific errors associated with this fault, if any.
	Errors []Error `json:"errors"`

	// The message of the fault.
	Message string `json:"message"`
}

Encapsulates the errors that may be returned from the API.

type Pagination

type Pagination struct {
	Page    *int
	PerPage *int
}

Pagination for api calls to define how much records should be returns in an api response or which page of a list of records should be returned.

func NewPagination

func NewPagination(page, per_page int) *Pagination

NewPagination returns a pagination struct. If page and per_page are zero it will return nil.

func (*Pagination) NextPage

func (pagination *Pagination) NextPage()

NextPage, helper to move to next page.

type SummaryActivity

type SummaryActivity struct {

	// The unique identifier of the activity
	Id int `json:"id"`

	// The name of the activity
	Name string `json:"name"`

	// The activity's distance, in meters
	Distance float64 `json:"distance"`

	// The activity's moving time, in seconds
	MovingTime int64 `json:"moving_time"`

	// An enumeration of the sport types an activity may have. Distinct from ActivityType in that it has new types (e.g. MountainBikeRide)
	// May be one of the following values:
	//		AlpineSki, BackcountrySki, Canoeing, Crossfit, EBikeRide, Elliptical, EMountainBikeRide, Golf, GravelRide,
	//		Handcycle, Hike, IceSkate, InlineSkate, Kayaking, Kitesurf, MountainBikeRide, NordicSki, Ride, RockClimbing,
	//		RollerSki, Rowing, Run, Sail, Skateboard, Snowboard, Snowshoe, Soccer, StairStepper, StandUpPaddling, Surfing,
	//		Swim, TrailRun, Velomobile, VirtualRide, VirtualRun, Walk, WeightTraining, Wheelchair, Windsurf, Workout, Yoga
	SportType string `json:"sport_type"`

	// The time at which the activity was started.
	StartDateLocal time.Time `json:"start_date_local"`
}

Description of a single activity.

type TimeFilter

type TimeFilter struct {
	Before *time.Time
	After  *time.Time
}

TimeFilter to restrict api requests with before and after values.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL