From f408da5a7553b5ec31decf6acbf8d8c73a0e9aad Mon Sep 17 00:00:00 2001 From: lennyaiko Date: Thu, 10 Apr 2025 21:39:29 +0100 Subject: [PATCH 1/3] progress on auth --- app/config.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) 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() From 6b272eb1073c9f33043ca32b8f6c3143635bb6ce Mon Sep 17 00:00:00 2001 From: lennyaiko Date: Thu, 10 Apr 2025 21:50:25 +0100 Subject: [PATCH 2/3] progress on disbursement endpoint --- app/integrations/kafka.py | 15 +++++++-------- app/routes/loan.py | 20 ++++++++++++++------ app/utils/auth.py | 5 +++-- 3 files changed, 24 insertions(+), 16 deletions(-) diff --git a/app/integrations/kafka.py b/app/integrations/kafka.py index 85bee8b..6aa1d95 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: @@ -86,15 +87,13 @@ 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""" 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..64f82fd 100644 --- a/app/routes/loan.py +++ b/app/routes/loan.py @@ -153,14 +153,22 @@ 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 - - # 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(), + auth=( + settings.BANK_CALL_BASIC_AUTH_USERNAME, + settings.BANK_CALL_BASIC_AUTH_PASSWORD, + ), + ) + 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..5666957 100644 --- a/app/utils/auth.py +++ b/app/utils/auth.py @@ -1,8 +1,9 @@ import requests from flask import current_app + def get_headers(): return { - "Authorization": f"Bearer {current_app.config['JWT_SECRET_KEY']}", - "Content-Type": "application/json" + "Content-Type": "application/json", + "Accept": "application/json", } From c242e53eaadc4dcce5be0e5cc2b550fbee4b25bd Mon Sep 17 00:00:00 2001 From: lennyaiko Date: Thu, 10 Apr 2025 22:07:06 +0100 Subject: [PATCH 3/3] worked oon disbursement endpoint and added more things to log --- app/integrations/kafka.py | 6 ++++++ app/routes/loan.py | 10 ++++++---- app/utils/auth.py | 5 +++-- wsgi.py | 1 + 4 files changed, 16 insertions(+), 6 deletions(-) diff --git a/app/integrations/kafka.py b/app/integrations/kafka.py index 6aa1d95..0afc813 100644 --- a/app/integrations/kafka.py +++ b/app/integrations/kafka.py @@ -53,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: @@ -89,6 +93,8 @@ class KafkaIntegration: @staticmethod def _call_disbursement_endpoint(message): """Call the disbursement endpoint with the received message""" + logger.info(f"Calling disbursement endpoint with message: {message}") + try: response = disbursement_endpoint(message) logger.info( diff --git a/app/routes/loan.py b/app/routes/loan.py index 64f82fd..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__) @@ -159,15 +160,16 @@ def disbursement(data=None): api_url = f"{BASE_URL}/Disbursement" + logger.info(f"Calling disbursement endpoint with data: {data}") + response = requests.post( api_url, json=data, headers=get_headers(), - auth=( - settings.BANK_CALL_BASIC_AUTH_USERNAME, - settings.BANK_CALL_BASIC_AUTH_PASSWORD, - ), ) + + logger.info(f"Disbursement response: {response.json()}") + return jsonify(response.json()), response.status_code diff --git a/app/utils/auth.py b/app/utils/auth.py index 5666957..4792796 100644 --- a/app/utils/auth.py +++ b/app/utils/auth.py @@ -1,9 +1,10 @@ -import requests -from flask import current_app +from app.config import settings def get_headers(): return { "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