import class from a module into another module - flask - data not transferring in server/docker

8/26/2021

app.py

class ClientInformation:
    _id = None
    _secret = None

ClientInformation = ClientInformation() ## this is not needed, just added it made it work on the container

@api.route("/test_me")
class TestMe(Resource):
    def post(self):
        parser = reqparse.RequestParser()

        ### define arguments
        parser.add_argument("client_id", type=str, required=True, )
        parser.add_argument("client_secret", type=str, required=True)

        ClientInformation._id = args["client_id"]
        ClientInformation._secret = args["client_secret"]
        
        ## Start test with threading to avoid timeout error
        x = threading.Thread(target=run_tests.run_test, kwargs=dict(pytest_pattern="test_oauth.py"))
        x.start()

        return response(f"OAuth Testing started: {datetime.now().strftime('%H:%M:%S')}", status=202)

test_oauth.py

import oauth_requests.py
oauth_requests.print_clientInformation.py

ouath_requests.py

from app import ClientInformation
def print_clientInformation():
    client_id = ClientInformation._id
    client_secret = ClientInformation._secret
    payload = {"client_id": client_id, "client_secret": client_secret}
    print(payload)

log output after the request is received

{'client_id': None, 'client_secret': None} ## should not be None

Notes:

  • this works fine locally, (using flask server) . the tests run b/c the ClientInformation is passed among modules by importing using: from app import ClientInformation

  • app deployed in docker and kubernetes

it's the ClientInformation._id and ClientInformation._secret is empty when imported into oauth_requests.py as shown by the payload.

the workflow is:

send POST request --> @api.route('/test_OAuth') receives it and parses the data from the request --> stores the data into ClientInformation --> starts the test file "test_OAuth.py" which invokes a function in oauth_requests.py to get the tokens.

in order the get the tokens, it imports ClientInformation from app. but as you can see, the payload is full of None.

This works very well when using a local server on flask to start the app.

What is wrong here?

-- tigertiger
docker
flask
kubernetes
python

0 Answers