diff --git a/app/config.py b/app/config.py index d68ea4f..2a418f5 100644 --- a/app/config.py +++ b/app/config.py @@ -1,11 +1,11 @@ import os +from datetime import timedelta class Config: """Base configuration for Flask app""" SECRET_KEY = os.getenv("SECRET_KEY", "supersecretkey") - # BANK_CALL_AUTH_BASE_URL = "https://coreapi.dev.simbrellang.net/v1/api/auth" BANK_CALL_BASE_URL = "https://bank-emulator.dev.simbrellang.net" JWT_SECRET_KEY = os.getenv("JWT_SECRET_KEY", "your_jwt_secret") DEBUG = True @@ -14,5 +14,20 @@ class Config: KAFKA_PAYMENT_TOPIC = "PROCESS_PAYMENT" KAFKA_TIMEOUT = 5.0 + JWT_SECRET_KEY = os.getenv("JWT_SECRET_KEY", "secret-key") + JWT_ACCESS_TOKEN_EXPIRES = os.getenv("JWT_ACCESS_TOKEN_EXPIRES", timedelta(hours=1)) + JWT_REFRESH_TOKEN_EXPIRES = os.getenv( + "JWT_REFRESH_TOKEN_EXPIRES", timedelta(days=30) + ) + + BANK_CALL_APP_ID = os.getenv("BANK_CALL_APP_ID", "app1") + BANK_CALL_API_KEY = os.getenv("BANK_CALL_API_KEY", "test-api-key-12345") + BANK_CALL_BASIC_AUTH_USERNAME = os.environ.get( + "BANK_CALL_BASIC_AUTH_USERNAME", "user" + ) + BANK_CALL_BASIC_AUTH_PASSWORD = os.environ.get( + "BANK_CALL_BASIC_AUTH_PASSWORD", "password" + ) + settings = Config() diff --git a/app/integrations/kafka.py b/app/integrations/kafka.py index 85bee8b..0afc813 100644 --- a/app/integrations/kafka.py +++ b/app/integrations/kafka.py @@ -3,6 +3,7 @@ import json from app.utils.logger import logger from app.config import settings import requests +from app.routes.loan import disbursement as disbursement_endpoint class KafkaIntegration: @@ -52,6 +53,10 @@ class KafkaIntegration: consumer = KafkaIntegration._get_consumer() consumer.subscribe([topic]) + logger.info( + f"Waiting for messages from topic {topic} with this timeout: {timeout}..." + ) + try: msg = consumer.poll(timeout=timeout) if msg is None: @@ -86,15 +91,15 @@ class KafkaIntegration: logger.info("Kafka consumer closed") @staticmethod - def _call_disbursement_endpoint(message, endpoint_url=f"{BASE_URL}/Disbursement"): - """Call an HTTP endpoint with the received message""" + def _call_disbursement_endpoint(message): + """Call the disbursement endpoint with the received message""" + logger.info(f"Calling disbursement endpoint with message: {message}") + try: - response = requests.post(endpoint_url, json=message, timeout=5) - response.raise_for_status() + response = disbursement_endpoint(message) logger.info( - f"Successfully sent message to {endpoint_url}: {response.status_code}" + f"Successfully sent message to disbursement endpoint: {response.status_code}" ) - print(response.json()) - except requests.exceptions.RequestException as e: - logger.error(f"Failed to call endpoint {endpoint_url}: {e}") + except Exception as e: + logger.error(f"Failed to call disbursement endpoint: {e}") raise diff --git a/app/routes/loan.py b/app/routes/loan.py index 562ce22..caa99b6 100644 --- a/app/routes/loan.py +++ b/app/routes/loan.py @@ -2,6 +2,7 @@ from flask import Blueprint, request, jsonify, current_app import requests from app.config import settings from app.utils.auth import get_headers +from app.utils.logger import logger loan_bp = Blueprint("loan", __name__) @@ -153,14 +154,23 @@ def rac_check(): @loan_bp.route("/disbursement", methods=["POST"]) -def disbursement(): - data = request.json +def disbursement(data=None): + if data is None: + data = request.json + api_url = f"{BASE_URL}/Disbursement" - return jsonify({"requestId": data["requestId"]}), 200 + logger.info(f"Calling disbursement endpoint with data: {data}") - # response = requests.post(api_url, json=data, headers=get_headers()) - # return jsonify(response.json()), response.status_code + response = requests.post( + api_url, + json=data, + headers=get_headers(), + ) + + logger.info(f"Disbursement response: {response.json()}") + + return jsonify(response.json()), response.status_code @loan_bp.route("/collect-loan", methods=["POST"]) diff --git a/app/utils/auth.py b/app/utils/auth.py index 0ef72b2..4792796 100644 --- a/app/utils/auth.py +++ b/app/utils/auth.py @@ -1,8 +1,10 @@ -import requests -from flask import current_app +from app.config import settings + def get_headers(): return { - "Authorization": f"Bearer {current_app.config['JWT_SECRET_KEY']}", - "Content-Type": "application/json" + "Content-Type": "application/json", + "Accept": "application/json", + "api_key": settings.BANK_CALL_API_KEY, + "app_id": settings.BANK_CALL_APP_ID, } diff --git a/wsgi.py b/wsgi.py index c9745ad..1c3b9e8 100644 --- a/wsgi.py +++ b/wsgi.py @@ -9,6 +9,7 @@ if __name__ != "__main__": kafka = KafkaIntegration() + logger.info("Starting Kafka consumer...") while True: message = kafka.receive_disbursement_messages( topic=settings.KAFKA_PAYMENT_TOPIC, timeout=settings.KAFKA_TIMEOUT