🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
DemoInstallSign in
Socket

github.com/stevenpelley/strava3golang

Package Overview
Dependencies
Alerts
File Explorer
Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

github.com/stevenpelley/strava3golang

v0.0.0-20230830002705-9f57f30f0123
Source
Go
Version published
Created
Source

Golang bindings for Strava V3 API

This is the standard golang bindings for Strava's V3 API (current API as of 2023). This is made available by Strava via Swagger with no modifications here, but it was sufficiently difficult to get working that I thoughts I'd post it on github for others, along with an explanation for why I did it this way (see the Makefile for a simple description of "what" I did).

For general use feel free to require this repos in golang. I don't intend to update it in the future, but I also make no promises. If it breaks then your recourse will be to clone and revert this repository and update your requires to your own repository.

Strava uses the same string values in multiple enums. Golang, which has no built in support for enums, uses the value of these strings to generate static variable names. The variables from these different enums then collide and produce compiler errors. You can, of course, simply rename these variables to address the conflict. I instead used openapi-generator (compatible with Swagger 2 which is what Strava requires) with its -p enumClassPrefix=true option to do this for me. Openapi-generator complains that the provided swagger input is invalid, so I silence this with --skip-validate-spec.

To use this client you will first need to perform oauth2 authorization, create an http client with the oauth2 token, and assign this client into the strava HTTPClient struct. I use github.com/stevenpelley/oauth2-accesstoken-golang to create a token for development use.

See example/athlete for an example. This assumes you have the input oauth_client_config.json used in github.com/stevenpelley/oauth2-accesstoken-golang as well as the token.json that results. These files are both listed in .gitignore to prevent commiting secrets.

What follows is the README generated from the strava swagger itself.

Go API client for strava3golang

The Swagger Playground is the easiest way to familiarize yourself with the Strava API by submitting HTTP requests and observing the responses before you write any client code. It will show what a response will look like with different endpoints depending on the authorization scope you receive from your athletes. To use the Playground, go to https://www.strava.com/settings/api and change your “Authorization Callback Domain” to developers.strava.com. Please note, we only support Swagger 2.0. There is a known issue where you can only select one scope at a time. For more information, please check the section “client code” at https://developers.strava.com/docs.

Overview

This API client was generated by the OpenAPI Generator project. By using the OpenAPI-spec from a remote server, you can easily generate an API client.

  • API version: 3.0.0
  • Package version: 1.0.0
  • Build package: org.openapitools.codegen.languages.GoClientCodegen

Installation

Install the following dependencies:

go get github.com/stretchr/testify/assert
go get golang.org/x/oauth2
go get golang.org/x/net/context

Put the package under your project folder and add the following in import:

import strava3golang "github.com/stevenpelley/strava3golang"

To use a proxy, set the environment variable HTTP_PROXY:

os.Setenv("HTTP_PROXY", "http://proxy_name:proxy_port")

Configuration of Server URL

Default configuration comes with Servers field that contains server objects as defined in the OpenAPI specification.

Select Server Configuration

For using other server than the one defined on index 0 set context value sw.ContextServerIndex of type int.

ctx := context.WithValue(context.Background(), strava3golang.ContextServerIndex, 1)

Templated Server URL

Templated server URL is formatted using default variables from configuration or from context value sw.ContextServerVariables of type map[string]string.

ctx := context.WithValue(context.Background(), strava3golang.ContextServerVariables, map[string]string{
	"basePath": "v2",
})

Note, enum values are always validated and all unused variables are silently ignored.

URLs Configuration per Operation

Each operation can use different server URL defined using OperationServers map in the Configuration. An operation is uniquely identified by "{classname}Service.{nickname}" string. Similar rules for overriding default operation server index and variables applies by using sw.ContextOperationServerIndices and sw.ContextOperationServerVariables context maps.

ctx := context.WithValue(context.Background(), strava3golang.ContextOperationServerIndices, map[string]int{
	"{classname}Service.{nickname}": 2,
})
ctx = context.WithValue(context.Background(), strava3golang.ContextOperationServerVariables, map[string]map[string]string{
	"{classname}Service.{nickname}": {
		"port": "8443",
	},
})

Documentation for API Endpoints

All URIs are relative to https://www.strava.com/api/v3

ClassMethodHTTP requestDescription
ActivitiesAPICreateActivityPost /activitiesCreate an Activity
ActivitiesAPIGetActivityByIdGet /activities/{id}Get Activity
ActivitiesAPIGetCommentsByActivityIdGet /activities/{id}/commentsList Activity Comments
ActivitiesAPIGetKudoersByActivityIdGet /activities/{id}/kudosList Activity Kudoers
ActivitiesAPIGetLapsByActivityIdGet /activities/{id}/lapsList Activity Laps
ActivitiesAPIGetLoggedInAthleteActivitiesGet /athlete/activitiesList Athlete Activities
ActivitiesAPIGetZonesByActivityIdGet /activities/{id}/zonesGet Activity Zones
ActivitiesAPIUpdateActivityByIdPut /activities/{id}Update Activity
AthletesAPIGetLoggedInAthleteGet /athleteGet Authenticated Athlete
AthletesAPIGetLoggedInAthleteZonesGet /athlete/zonesGet Zones
AthletesAPIGetStatsGet /athletes/{id}/statsGet Athlete Stats
AthletesAPIUpdateLoggedInAthletePut /athleteUpdate Athlete
ClubsAPIGetClubActivitiesByIdGet /clubs/{id}/activitiesList Club Activities
ClubsAPIGetClubAdminsByIdGet /clubs/{id}/adminsList Club Administrators
ClubsAPIGetClubByIdGet /clubs/{id}Get Club
ClubsAPIGetClubMembersByIdGet /clubs/{id}/membersList Club Members
ClubsAPIGetLoggedInAthleteClubsGet /athlete/clubsList Athlete Clubs
GearsAPIGetGearByIdGet /gear/{id}Get Equipment
RoutesAPIGetRouteAsGPXGet /routes/{id}/export_gpxExport Route GPX
RoutesAPIGetRouteAsTCXGet /routes/{id}/export_tcxExport Route TCX
RoutesAPIGetRouteByIdGet /routes/{id}Get Route
RoutesAPIGetRoutesByAthleteIdGet /athletes/{id}/routesList Athlete Routes
SegmentEffortsAPIGetEffortsBySegmentIdGet /segment_effortsList Segment Efforts
SegmentEffortsAPIGetSegmentEffortByIdGet /segment_efforts/{id}Get Segment Effort
SegmentsAPIExploreSegmentsGet /segments/exploreExplore segments
SegmentsAPIGetLoggedInAthleteStarredSegmentsGet /segments/starredList Starred Segments
SegmentsAPIGetSegmentByIdGet /segments/{id}Get Segment
SegmentsAPIStarSegmentPut /segments/{id}/starredStar Segment
StreamsAPIGetActivityStreamsGet /activities/{id}/streamsGet Activity Streams
StreamsAPIGetRouteStreamsGet /routes/{id}/streamsGet Route Streams
StreamsAPIGetSegmentEffortStreamsGet /segment_efforts/{id}/streamsGet Segment Effort Streams
StreamsAPIGetSegmentStreamsGet /segments/{id}/streamsGet Segment Streams
UploadsAPICreateUploadPost /uploadsUpload Activity
UploadsAPIGetUploadByIdGet /uploads/{uploadId}Get Upload

Documentation For Models

Documentation For Authorization

Authentication schemes defined for the API:

strava_oauth

  • Type: OAuth
  • Flow: accessCode
  • Authorization URL: https://www.strava.com/api/v3/oauth/authorize
  • Scopes:
  • read: Read public segments, public routes, public profile data, public posts, public events, club feeds, and leaderboards
  • read_all: Read private routes, private segments, and private events for the user
  • profile:read_all: Read all profile information even if the user has set their profile visibility to Followers or Only You
  • profile:write: Update the user's weight and Functional Threshold Power (FTP), and access to star or unstar segments on their behalf
  • activity:read: Read the user's activity data for activities that are visible to Everyone and Followers, excluding privacy zone data
  • activity:read_all: The same access as activity:read, plus privacy zone data and access to read the user's activities with visibility set to Only You
  • activity:write: Access to create manual activities and uploads, and access to edit any activities that are visible to the app, based on activity read access level

Example

auth := context.WithValue(context.Background(), sw.ContextAccessToken, "ACCESSTOKENSTRING")
r, err := client.Service.Operation(auth, args)

Or via OAuth2 module to automatically refresh tokens and perform user authentication.

import "golang.org/x/oauth2"

/* Perform OAuth2 round trip request and obtain a token */

tokenSource := oauth2cfg.TokenSource(createContext(httpClient), &token)
auth := context.WithValue(oauth2.NoContext, sw.ContextOAuth2, tokenSource)
r, err := client.Service.Operation(auth, args)

Documentation for Utility Methods

Due to the fact that model structure members are all pointers, this package contains a number of utility functions to easily obtain pointers to values of basic types. Each of these functions takes a value of the given basic type and returns a pointer to it:

  • PtrBool
  • PtrInt
  • PtrInt32
  • PtrInt64
  • PtrFloat
  • PtrFloat32
  • PtrFloat64
  • PtrString
  • PtrTime

Author

FAQs

Package last updated on 30 Aug 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts