Skip to content

Instantly share code, notes, and snippets.

@ChrisRomp
Last active October 12, 2021 19:05
Show Gist options
  • Save ChrisRomp/f546ea673b3222bde2ab7cabe8a1bf2e to your computer and use it in GitHub Desktop.
Save ChrisRomp/f546ea673b3222bde2ab7cabe8a1bf2e to your computer and use it in GitHub Desktop.
Script to pull data from the Bouncie API - https://www.bouncie.dev
{
"client_id": "",
"client_secret": "",
"grant_type": "authorization_code",
"code": "",
"redirect_uri": ""
}
#!/bin/bash
SCRIPT_DIR="$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
# Set this to true to show process output in the console
debug=false
# Ensure jq is installed
if ! command -v jq &> /dev/null
then
echo "jq not installed. Exiting."
exit 1
fi
configfile=$SCRIPT_DIR/bouncie.json
tokenfile=$SCRIPT_DIR/token.json
renew=false
# Ensure config file exists
if [[ ! -f $configfile ]]
then
echo "$configfile not found. Exiting."
exit 1
fi
# Check if tokenfile exists
if [[ ! -f $tokenfile ]]
then
[[ $debug == true ]] && echo "Bouncie token not found. Renewing."
renew=true
else
# Token exists; check expiration value
expvalue=$(jq -r '.expires' $tokenfile)
if [[ "$expvalue" -lt "$(date +%s)" ]]
then
[[ $debug == true ]] && echo "Bouncie token expired. Renewing."
renew=true
fi
fi
# Renew if needed
if [[ $renew == true ]]
then
[[ $debug == true ]] && echo "Renewing Bouncie token..."
currenttime=$(date +%s)
tempfile=$(mktemp)
# Fetch the token
curl -s -X POST -H "Content-Type: application/json" -d "@$configfile" https://auth.bouncie.com/oauth/token > $tempfile
# Get access token
token=$(jq -r '.access_token' $tempfile)
# Update token expiration value
expires_in=$(jq -r '.expires_in' $tempfile)
expires=$((currenttime + expires_in))
# Write token.json
tokenjson="{ \"access_token\":\"$token\", \"expires\":\"$expires\" }"
echo "$tokenjson" > $tokenfile
# Remove temp file
rm -f $tempfile
else
token=$(jq -r '.access_token' $tokenfile)
fi
# Get Bouncie data
[[ $debug == true ]] && echo "Fetching Bouncie data..."
curl -s -X GET -H "Content-Type: application/json" -H "Authorization: $token" https://api.bouncie.dev/v1/vehicles
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment