46 lines
1.6 KiB
Python
46 lines
1.6 KiB
Python
from app.config import settings
|
|
import requests
|
|
from app.utils.logger import logger
|
|
|
|
def get_headers():
|
|
BANK_CALL_BASE_URL = settings.BANK_CALL_BASE_URL
|
|
BANK_CALL_AUTH_ENDPOINT = settings.BANK_CALL_AUTH_ENDPOINT
|
|
BANK_CALL_BASIC_AUTH_USERNAME = settings.BANK_CALL_BASIC_AUTH_USERNAME
|
|
BANK_CALL_BASIC_AUTH_PASSWORD = settings.BANK_CALL_BASIC_AUTH_PASSWORD
|
|
BANK_GRANT_TYPE = settings.BANK_GRANT_TYPE
|
|
#authenticate
|
|
url = f"{BANK_CALL_BASE_URL}{BANK_CALL_AUTH_ENDPOINT}"
|
|
data = {
|
|
"grant_type": BANK_GRANT_TYPE,
|
|
"username": BANK_CALL_BASIC_AUTH_USERNAME,
|
|
"password": "G7$k9@pL2!qR"
|
|
}
|
|
logger.info(f"Calling Bank Call-Auth Endpoint: {url}")
|
|
|
|
headers = {"Content-Type": "application/json"}
|
|
|
|
try:
|
|
response = requests.post(url, json=data, headers=headers, timeout=10)
|
|
response.raise_for_status() # Raises HTTPError for 4xx/5xx
|
|
result = response.json()
|
|
|
|
|
|
# Check if access_token is present
|
|
if 'access_token' not in result:
|
|
logger.error("No access_token found in Bank Call Auth response")
|
|
return {"error": "Authentication failed: no access_token returned"}
|
|
|
|
return {
|
|
"Content-Type": "application/json",
|
|
"x-api-key": settings.BANK_CALL_API_KEY,
|
|
"App-Id": settings.BANK_CALL_APP_ID,
|
|
"Authorization": f"Bearer {result['access_token']}"
|
|
}
|
|
|
|
except requests.exceptions.RequestException as e:
|
|
logger.error(f"Failed to get auth token: {e}")
|
|
raise
|
|
except ValueError as e:
|
|
logger.error(f"Failed to parse auth response JSON: {e}")
|
|
raise
|