Skip to content

Instantly share code, notes, and snippets.

@dzhavat
Created February 23, 2021 20:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dzhavat/ea1f11f61edb2a7a4a4abc83af44edbb to your computer and use it in GitHub Desktop.
Save dzhavat/ea1f11f61edb2a7a4a4abc83af44edbb to your computer and use it in GitHub Desktop.
HTTP triggered Azure Function for getting Strava activities
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