-
-
Save dzhavat/ea1f11f61edb2a7a4a4abc83af44edbb to your computer and use it in GitHub Desktop.
HTTP triggered Azure Function for getting Strava activities
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fetch = require("node-fetch"); | |
module.exports = function (context, req) { | |
const accessToken = context.bindings.inputTable.Value; | |
const headers = { | |
'Authorization': `Bearer ${accessToken}` | |
}; | |
return getLatestActivity() | |
.then(response => Promise.all([ | |
getActivityById(response.id), | |
getAthleteStats() | |
])) | |
.then(([activity, stats]) => { | |
const returnObject = { | |
id: activity.id, | |
name: activity.name, | |
distance: activity.distance, | |
start_date: activity.start_date, | |
moving_time: activity.moving_time, | |
year_to_date_run_total_distance: stats.ytd_run_totals.distance, | |
photoUrl: undefined | |
}; | |
if (activity.photos.count > 0) { | |
const photoUrls = Object.values(activity.photos.primary.urls); | |
returnObject.photoUrl = photoUrls.pop(); | |
} | |
return context.res.json(returnObject); | |
}); | |
function getActivityById(activityId) { | |
const url = `https://www.strava.com/api/v3/activities/${activityId}`; | |
return fetch(url, { headers }) | |
.then(response => response.json()); | |
} | |
function getLatestActivity() { | |
const url = "https://www.strava.com/api/v3/athlete/activities?per_page=1"; | |
return fetch(url, { headers }) | |
.then(response => response.json()) | |
.then(response => response[0]); | |
} | |
function getAthleteStats(athleteId = 8353180) { | |
const url = `https://www.strava.com/api/v3/athletes/${athleteId}/stats`; | |
return fetch(url, { headers }) | |
.then(response => response.json()); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment