Exemplo n.º 1
0
def refreshVeloOAuth():
    # Check if OAuth2 token is about to expire ... if so .. issue event to refresh it
    # grab the VELO_API_ACCESSTOKENEXPIRATION from django.constance
    # if now is >= VELO_API_ACCESSTOKENEXPIRATION ... call api and update configs with access_token & expires_in
    # when updating expiration be sure to remove 5 mins (300 sec) off of expires_in
    now_int = int(time.time())
    if int(app.config['VELO_API_ACCESSTOKENEXPIRATION']) <= now_int:
        print('CALL VELO API TO REFRESH TOKEN')
        configuration = velo_payments.Configuration()
        configuration.username = os.environ.get("VELO_API_APIKEY")
        configuration.password = os.environ.get("VELO_API_APISECRET")
        configuration.host = os.environ.get("VELO_BASE_URL")
        grant_type = 'client_credentials'

        try:
            # Authentication endpoint
            api_instance = velo_payments.LoginApi(
                velo_payments.ApiClient(configuration))
            api_response = api_instance.velo_auth(grant_type=grant_type)
            print(api_response)
            app.config['VELO_API_ACCESSTOKEN'] = api_response.access_token
            app.config['VELO_API_ACCESSTOKENEXPIRATION'] = (
                now_int + int(api_response.expires_in)) - 300
            print("new velo oauth2 token expires at: %s\n" %
                  app.config['VELO_API_ACCESSTOKENEXPIRATION'])
        except ApiException as e:
            print("Exception when calling AuthApi->velo_auth: %s\n" % e)
    else:
        print("velo oauth token has not expired")
    def setUp(self):
        self.api = velo_payments.api.countries_api.CountriesApi()  # noqa: E501

        if os.environ.get('APITOKEN') == "":
            configuration = velo_payments.Configuration()
            # Configure HTTP basic authorization: basicAuth
            configuration.username = os.environ.get('KEY')
            configuration.password = os.environ.get('SECRET')

            # Defining host is optional and default to https://api.sandbox.velopayments.com
            configuration.host = os.environ.get('APIURL')
            # Create an instance of the API class
            api_instance = velo_payments.LoginApi(velo_payments.ApiClient(configuration))
            grant_type = 'client_credentials' # str | OAuth grant type. Should use 'client_credentials' (optional) (default to 'client_credentials')

            try:
                # Authentication endpoint
                api_response = api_instance.velo_auth(grant_type=grant_type)
                os.environ["APITOKEN"] = api_response.access_token
                
            except ApiException as e:
                print("Exception when calling LoginApi->velo_auth: %s\n" % e)