bug fix on application context with threads

This commit is contained in:
2025-04-15 16:39:05 +01:00
parent 32e3ca5bb0
commit 4685ddc1c8
5 changed files with 36 additions and 49 deletions
+1 -1
View File
@@ -13,13 +13,13 @@ def create_app():
# Load configuration # Load configuration
app.config.from_object(Config) app.config.from_object(Config)
# Setup CORS
CORS(app) CORS(app)
# Register blueprints # Register blueprints
app.register_blueprint(auth_bp) app.register_blueprint(auth_bp)
app.register_blueprint(autocall_bp, url_prefix="/autocall") app.register_blueprint(autocall_bp, url_prefix="/autocall")
# Error Handlers # Error Handlers
app.register_error_handler(405, method_not_allowed) app.register_error_handler(405, method_not_allowed)
app.register_error_handler(415, unsupported_media_type) app.register_error_handler(415, unsupported_media_type)
+14 -25
View File
@@ -2,7 +2,9 @@ import requests
from app.config import settings from app.config import settings
from app.utils.auth import get_headers from app.utils.auth import get_headers
from app.utils.logger import logger from app.utils.logger import logger
from flask import jsonify from flask import jsonify, current_app
from app.models.transactions import Transaction
class SimbrellaClient: class SimbrellaClient:
@@ -11,9 +13,14 @@ class SimbrellaClient:
@staticmethod @staticmethod
def disbursement(data): def disbursement(data):
api_url = f"{SimbrellaClient.BANK_CALL_BASE_URL}/Disbursement" api_url = f"{SimbrellaClient.BANK_CALL_BASE_URL}/Disbursement"
logger.info(f"BANK_CALL_BASE_URL = {SimbrellaClient.BANK_CALL_BASE_URL}")
logger.info(f"Calling Disbursement endpoint with data: {data}") logger.info(f"Calling Disbursement endpoint with data: {data}")
# Check if the transaction exists
logger.info(f"Checking if transaction exists")
with current_app.app_context():
transaction = Transaction.get_transaction_by_id(transaction_id=data['transactionId'])
logger.info(f"Response from database: {transaction}")
disbursement_data ={ disbursement_data ={
"requestId": data['requestId'], "requestId": data['requestId'],
"transactionId": data['transactionId'], "transactionId": data['transactionId'],
@@ -45,7 +52,6 @@ class SimbrellaClient:
@staticmethod @staticmethod
def collect_loan(data): def collect_loan(data):
api_url = f"{SimbrellaClient.BANK_CALL_BASE_URL}/CollectLoan" api_url = f"{SimbrellaClient.BANK_CALL_BASE_URL}/CollectLoan"
logger.info(f"BANK_CALL_BASE_URL = {SimbrellaClient.BANK_CALL_BASE_URL}")
logger.info(f"Calling CollectLoan endpoint with data: {data}") logger.info(f"Calling CollectLoan endpoint with data: {data}")
collect_loan_data = { collect_loan_data = {
@@ -76,14 +82,7 @@ class SimbrellaClient:
@staticmethod @staticmethod
def verify_transaction(): def verify_transaction():
# api_url = f"{SimbrellaClient.BANK_CALL_BASE_URL}/TransactionVerify"
# logger.info(f"BANK_CALL_BASE_URL = {SimbrellaClient.BANK_CALL_BASE_URL}")
# logger.info(f"Calling TransactionVerify endpoint with data: {data}")
try: try:
# logger.info(f"Here is your TransactionVerify Request data ***** : {data}")
# response = requests.post(api_url, json=data, headers=get_headers())
# logger.info(f"TransactionVerify response: {response.json()}")
return { return {
"status": "00", "status": "00",
@@ -96,16 +95,9 @@ class SimbrellaClient:
@staticmethod @staticmethod
def refresh_disbursement(data): def refresh_disbursement(data):
# api_url = f"{SimbrellaClient.BANK_CALL_BASE_URL}/Disbursement"
# logger.info(f"BANK_CALL_BASE_URL = {SimbrellaClient.BANK_CALL_BASE_URL}")
# logger.info(f"Calling Disbursement endpoint with data: {data}")
try: try:
logger.info(f"Here is your Disbursement Request data ***** : {data}") logger.info(f"Here is your Disbursement Request data ***** : {data}")
# response = requests.post(api_url, json=data, headers=get_headers())
# logger.info(f"Disbursement response: {response.json()}")
# return response.json()
return data return data
@@ -115,19 +107,16 @@ class SimbrellaClient:
@staticmethod @staticmethod
def payment_callback(data): def payment_callback(data):
# api_url = f"{SimbrellaClient.BANK_CALL_BASE_URL}/Payment"
# logger.info(f"BANK_CALL_BASE_URL = {SimbrellaClient.BANK_CALL_BASE_URL}")
# logger.info(f"Calling Payment Callback endpoint with data: {data}")
try: try:
logger.info(f"Here is your Payment Callback Request data ***** : {data}") logger.info(f"Here is your Payment Callback Request data ***** : {data}")
# response = requests.post(api_url, json=data, headers=get_headers())
# logger.info(f"Payment Callback response: {response.json()}")
# return response.json()
return data return data
except Exception as e: except Exception as e:
logger.info(f"Failed to call Payment Callback endpoint: {e}") logger.info(f"Failed to call Payment Callback endpoint: {e}")
raise raise
@staticmethod
def check_transaction(txn_id):
return run_in_app_context(Transaction.get_transaction_by_id(txn_id))
+3 -1
View File
@@ -1 +1,3 @@
from .transactions import Transaction from .transactions import Transaction
__all__ = ['Transaction']
+2 -8
View File
@@ -2,14 +2,8 @@ from app.config import settings
def get_headers(): def get_headers():
# return {
# "Content-Type": "application/json",
# "x-api_key": settings.BANK_CALL_API_KEY,
# "App-Id": settings.BANK_CALL_APP_ID,
# }
return { return {
"Content-Type": "application/json", "Content-Type": "application/json",
"x-api-key": "test-api-key-12345", "x-api-key": settings.BANK_CALL_API_KEY,
"App-Id": "app1", "App-Id": settings.BANK_CALL_APP_ID,
} }
+16 -14
View File
@@ -7,23 +7,25 @@ from app.utils.logger import logger
app = create_app() app = create_app()
kafka = KafkaIntegration() kafka = KafkaIntegration()
def start_kafka_consumer(): def start_kafka_consumer(app):
logger.info("Starting Kafka consumer...") with app.app_context():
while True: logger.info("Starting Kafka consumer...")
try: while True:
try:
message = kafka.receive_disbursement_messages( message = kafka.receive_disbursement_messages(
topic=settings.KAFKA_PAYMENT_TOPIC, timeout=settings.KAFKA_TIMEOUT topic=settings.KAFKA_PAYMENT_TOPIC, timeout=settings.KAFKA_TIMEOUT
) )
if message: if message:
logger.info(f"Processed message: {message}") logger.info(f"Processed message: {message}")
else: else:
logger.info("No message received within timeout") logger.info("No message received within timeout")
except Exception as e: except Exception as e:
logger.error(f"Error while receiving message: {e}") logger.error(f"Error while receiving message: {e}")
if __name__ != "__main__": if __name__ != "__main__":
@@ -32,4 +34,4 @@ if __name__ != "__main__":
wsgi_app = app wsgi_app = app
# Start kafka in a thread # Start kafka in a thread
threading.Thread(target=start_kafka_consumer, daemon=True).start() threading.Thread(target=start_kafka_consumer, args=(app,), daemon=True).start()