1
0

[add]: token expiry

This commit is contained in:
VivianDee
2025-10-27 19:24:08 +01:00
parent bb85c8f166
commit 54e52c639b
2 changed files with 22 additions and 4 deletions
+15 -4
View File
@@ -1,8 +1,7 @@
import httpx
import json
import time
from app.utils.logger import logger
from app.config import settings
import logging
class SimbrellaIntegration:
@@ -12,6 +11,7 @@ class SimbrellaIntegration:
AUTH_ENDPOINT = settings.BANK_CALL_AUTH_ENDPOINT
_access_token = None # cache token in memory
_token_expiry = 0
@staticmethod
def generate_token():
@@ -35,7 +35,10 @@ class SimbrellaIntegration:
response.raise_for_status()
data = response.json()
expires_in = data.get("expires_in", 1800)
SimbrellaIntegration._access_token = data.get("access_token")
SimbrellaIntegration._token_expiry = time.time() + expires_in - 60
if not SimbrellaIntegration._access_token:
raise Exception("Access token not found in response")
@@ -47,6 +50,14 @@ class SimbrellaIntegration:
logger.error(f"Token generation failed: {str(e)}", exc_info=True)
raise Exception(f"Token generation failed: {str(e)}")
@staticmethod
def _get_token():
"""
Return a valid token, refreshing if expired or missing
"""
if not SimbrellaIntegration._access_token or time.time() >= SimbrellaIntegration._token_expiry:
return SimbrellaIntegration.generate_token()
return SimbrellaIntegration._access_token
@staticmethod
def rac_check(customer_id, account_id, transaction_id):
@@ -67,7 +78,7 @@ class SimbrellaIntegration:
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {SimbrellaIntegration._access_token}"
"Authorization": f"Bearer {SimbrellaIntegration._get_token()}"
}
try:
@@ -93,7 +104,7 @@ class SimbrellaIntegration:
try:
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {SimbrellaIntegration._access_token}"
"Authorization": f"Bearer {SimbrellaIntegration._get_token()}"
}
response = httpx.get(url, headers=headers, timeout=10.0)
+7
View File
@@ -46,6 +46,13 @@ class Config:
VALID_API_KEY = os.getenv("SIMBRELLA_API_KEY", "test-api-key-12345")
SIMBRELLA_BASE_URL = os.getenv("SIMBRELLA_BASE_URL", "http://127.0.0.1:6337")
SIMBRELLA_ENDPOINT_RAC_CHECKS = os.getenv("SIMBRELLA_ENDPOINT_RAC_CHECKS","api/rac-check")
SIMBRELLA_HEALTH = os.getenv("SIMBRELLA_ENDPOINT_RAC_CHECKS","api/system-health-check")
BANK_CALL_AUTH_ENDPOINT = os.getenv("BANK_CALL_AUTH_ENDPOINT", "/api/Auth/generate-token")
BANK_CALL_USERNAME = os.getenv("BANK_CALL_USERNAME", "simbrella")
BANK_CALL_PASSWORD = os.getenv("BANK_CALL_PASSWORD", "G7$k9@pL2!qR")
EVENTS_SERVICE_BASE_URL = os.getenv("EVENTS_SERVICE_BASE_URL","https://event-core.simbrellang.net")
ENDPOINT_DIRECT_LOAN = os.getenv("ENDPOINT_DIRECT_LOAN","/autocall/direct/loan")
ENDPOINT_DIRECT_REPAYMENT = os.getenv("ENDPOINT_DIRECT_REPAYMENT","/autocall/direct/repayment")