When you create a big python application, you can began to run into challenges with longer scripts that extended beyond the expiration of a single JWT.
To elegantly solve this, You can use decorators to check the token’s expiration and request a new token if necessary. This article goes over the framework I set up so that you can apply a similar mechanism in your own scripts.
Setting the Scene
import time
import requests
class myAPI():
host = None
key = None
secret = None
access_token = None
access_token_expiration = None
def __init__(self,host,key,secret):
# the function that is executed when
# an instance of the class is created
pass
def getAccessToken(self):
# the function that is
# used to request the JWT
pass
class Decorators():
@staticmethod
def refreshToken(decorated):
# the function that is used to check
# the JWT and refresh if necessary
pass
Our class will work by requiring the fields necessary to request the JWT. Without access, there is nothing more our class will do.