Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to update a global variable from a function in Python?

I want to update an empty string value which is declared globally. I want to update that variable from inside a function. Following id the code:

import jwt
import os
from dotenv import load_dotenv
load_dotenv()
from rest_framework.exceptions import APIException
jwtPrivateKey = (os.environ.get('SECRET_KEY'))

# VARIABLE DECALRED HERE
token_value = ""
def authenticate(auth_header_value):
    try:
        
        if auth_header_value is not None:
            if len(auth_header_value.split(" ", 1)) == 2:
                authmeth, auth = auth_header_value.split(
                    " ", 1)
                if authmeth.lower() != "bearer" and authmeth.lower() != "basic":
                    return TokenNotBearer()

                # WANT TO UPDATE THE VALUE OF token_value HERE
                token_value= jwt.decode(
                    jwt=auth, key=jwtPrivateKey, algorithms=['HS256', ])     
               
                return TokenVerified()

            else:

                return TokenNotBearer()
        else:
            return TokenNotProvided()
            
    except (TokenExpiredOrInvalid, jwt.DecodeError):
        return TokenExpiredOrInvalid()

# when token is expired or invalid
class TokenExpiredOrInvalid(APIException):
    status_code = 403
    default_detail = "Invalid/Expired token"
    default_code = "forbidden"

# when token is not provided
class TokenNotProvided(APIException):
    status_code = 401
    default_detail = "Access denied. Authorization token must be provided"
    default_code = "unauthorized"

# when the token is not provided in the bearer
class TokenNotBearer(APIException):
    status_code = 401
    default_detail = "Access denied. Authorization token must be Bearer [token]"
    default_code = "token not bearer"

# when the token is valid
class TokenVerified():
    status_code = 200
    default_detail = "Token verified Successfully"
    default_code = "verified"

    # WANT TO USE THE UPDATED VALUE HERE
    data=token_value
    

I want to update the value of the variable token_value from the function authenticate and access it inside the class TokenVerified.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

# WANT TO UPDATE THE VALUE OF token_value HERE
global token_value # add this line
token_value= jwt.decode(jwt=auth, key=jwtPrivateKey, algorithms=['HS256', ])
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading