0

I have a token whos expire in 30 minutes, i want do renew this token until the 30 minutes time expire, i have the following script calling an api:

function token {
        gen_bearer_token=$(curl -X POST "https://api.foo.bar/oauth2/token" -H "accept: application/json" -H "Content-Type: application/x-www-form-urlencoded" -d "client_id=foo&client_secret=bar" | cut -d '{' -f2 | cut -d '}' -f1)
        bearer_token=$(echo $gen_bearer_token | awk '{print $2}' | cut -d '"' -f2 | cut -d '"' -f1)
        token_type=$(echo $gen_bearer_token | awk '{print $6}' | cut -d '"' -f2 | cut -d '"' -f1)
    }

echo -e "HOSTNAME;LAST_SEEN" > ${file}


ids=$(curl -s -X GET 'https://api.foo.bar/devices/queries/devices-scroll/v1?limit=5000' -H  'accept: application/json' -H  'authorization: '${token_type}' '${bearer_token}'' | jq .resources[])
for id in ${ids}
do
    result=$(curl -s -X GET 'https://api.foo.bar/devices/entities/devices/v1?ids='${id}'' -H  'accept: application/json' -H  'authorization: '${token_type}' '${bearer_token}'' | jq '.resources[] | "\(.hostname) \(.last_seen)"')
    if [[ ! -z ${result} ]]
    then
        hostname=$(echo ${result} | awk '{print $1}' | cut -d '"' -f2)
        last_seen=$(echo ${result} | awk '{print $2}' | cut -d '"' -f1)
        echo -e "${hostname};${last_seen}" >> ${file}
    fi
done

in this looping, time duration is more than 2 hours (variable duration) + i want to create a timer to renew token if the times pass through 30 minutes, if passed 30 minutes the request in api will fail.

for id in ${ids}
do
    result=$(curl -s -X GET 'https://api.foo.bar/devices/entities/devices/v1?ids='${id}'' -H  'accept: application/json' -H  'authorization: '${token_type}' '${bearer_token}'' | jq '.resources[] | "\(.hostname) \(.last_seen)"')
    if [[ ! -z ${result} ]]
    then
        hostname=$(echo ${result} | awk '{print $1}' | cut -d '"' -f2)
        last_seen=$(echo ${result} | awk '{print $2}' | cut -d '"' -f1)
        echo -e "${hostname};${last_seen}" >> ${file}
    fi
done

2 Answers 2

2

bash as a suitable timer built-in: the SECONDS variable.

Its value is its initial value plus the number of seconds since the most recent assignment. At shell start-up, it is initialized to 0.

All you need to do is check the value of $SECONDS before using the token, and if its value is greater than 1800 (the number of seconds in 30 minutes), get a new one.

SECONDS=1801  # Make sure a token will be generated before the first call
for id in $ids; do
    if [ "$SECONDS" -gt 1800 ]; then
        token
        SECONDS=0
    fi
    ...
done
Sign up to request clarification or add additional context in comments.

3 Comments

For general use case I suggest adding minisleeps using the external sleep command or the loadable sleep module before the end of the loop to avoid unnecessary CPU use.
@konsolebox What unnecessary CPU use are you worried about? The value of $SECONDS isn't updated every second; when its value is needed, the shell just computes it from the current time and a timestamp recorded when the variable was last assigned.
I'm talking about the loop itself that would endlessly iterate until SECONDS is updated. Your loop however is finite that's why I said "general use case" where loop is endless. E.g. for (( ;; )); do ...; done or while :; do ...; done. This is for people who'd find your answer and reuse its concept.
1

I Solved this problem creating a check response with status code.

response=$(curl -H 'authorization: '${token_type}' '${bearer_token}'' --write-out '%{http_code}' --silent --output /dev/null https://api.foo.bar/devices/entities/devices/v1?ids='0000000000000')

if [ ${response} -eq 200 ]; then
   do something
else
   token
fi

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.