Fix Que calls
This commit is contained in:
@@ -42,7 +42,7 @@ class KafkaIntegration:
|
|||||||
)
|
)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def receive_disbursement_messages(topic, timeout=1.0):
|
def receive_disbursement_messages(topic, timeout):
|
||||||
"""
|
"""
|
||||||
Receive messages from a Kafka topic.
|
Receive messages from a Kafka topic.
|
||||||
|
|
||||||
@@ -56,25 +56,44 @@ class KafkaIntegration:
|
|||||||
logger.info(
|
logger.info(
|
||||||
f"Waiting for messages from topic {topic} with this timeout: {timeout}..."
|
f"Waiting for messages from topic {topic} with this timeout: {timeout}..."
|
||||||
)
|
)
|
||||||
|
message =[]
|
||||||
try:
|
try:
|
||||||
msg = consumer.poll(timeout=timeout)
|
msg = consumer.poll(timeout=timeout)
|
||||||
|
|
||||||
|
logger.info(str(msg.value))
|
||||||
|
|
||||||
|
logger.info(
|
||||||
|
f"Received message from {msg.topic()} [{msg.partition()}] @ offset {msg.offset()}: "
|
||||||
|
)
|
||||||
|
|
||||||
if msg is None:
|
if msg is None:
|
||||||
logger.debug(f"No message received from topic {topic} within timeout")
|
logger.info(f"No message received from topic {topic} within timeout")
|
||||||
return None
|
return None
|
||||||
if msg.error():
|
if msg.error():
|
||||||
logger.error(f"Consumer error: {msg.error()}")
|
logger.info(f"Consumer error: {msg.error()}")
|
||||||
raise RuntimeError(f"Consumer error: {msg.error()}")
|
raise RuntimeError(f"Consumer error: {msg.error()}")
|
||||||
|
|
||||||
# Decode and return the message value
|
# Decode and return the message value
|
||||||
message_value = msg.value().decode("utf-8")
|
message_value= {"value":''}
|
||||||
logger.debug(
|
if msg.value() != "":
|
||||||
|
message_value = msg.value().decode("utf-8")
|
||||||
|
logger.info(
|
||||||
f"Received message from {msg.topic()} [{msg.partition()}] @ offset {msg.offset()}: {message_value}"
|
f"Received message from {msg.topic()} [{msg.partition()}] @ offset {msg.offset()}: {message_value}"
|
||||||
)
|
)
|
||||||
message = json.loads(message_value) if message_value else None
|
|
||||||
|
# Invalid Json will stop this process
|
||||||
|
try:
|
||||||
|
message = json.loads(message_value) if message_value else None
|
||||||
|
except ValueError as e:
|
||||||
|
return message # Invalid JSON JUST TURN BACK HERE
|
||||||
|
else:
|
||||||
|
pass # valid json
|
||||||
|
|
||||||
# Call the endpoint if provided
|
# Call the endpoint if provided
|
||||||
if message:
|
if message:
|
||||||
|
logger.info(
|
||||||
|
f"Received message from {msg.topic()} [{msg.partition()}] @ offset {msg.offset()}: {message}"
|
||||||
|
)
|
||||||
KafkaIntegration._call_disbursement_endpoint(message)
|
KafkaIntegration._call_disbursement_endpoint(message)
|
||||||
|
|
||||||
return message
|
return message
|
||||||
@@ -83,6 +102,8 @@ class KafkaIntegration:
|
|||||||
logger.error(f"Error while receiving message: {e}")
|
logger.error(f"Error while receiving message: {e}")
|
||||||
raise
|
raise
|
||||||
|
|
||||||
|
#return []
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def close_consumer():
|
def close_consumer():
|
||||||
"""Shutdown consumer"""
|
"""Shutdown consumer"""
|
||||||
@@ -101,5 +122,5 @@ class KafkaIntegration:
|
|||||||
f"Successfully sent message to disbursement endpoint: {response.status_code}"
|
f"Successfully sent message to disbursement endpoint: {response.status_code}"
|
||||||
)
|
)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Failed to call disbursement endpoint: {e}")
|
logger.info(f"Failed to call disbursement endpoint: {e}")
|
||||||
raise
|
#raise
|
||||||
|
|||||||
@@ -7,20 +7,50 @@ from flask import jsonify
|
|||||||
class SimbrellaClient:
|
class SimbrellaClient:
|
||||||
|
|
||||||
BASE_URL = settings.BANK_CALL_BASE_URL
|
BASE_URL = settings.BANK_CALL_BASE_URL
|
||||||
|
BANK_CALL_BASE_URL = settings.BANK_CALL_BASE_URL
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def disbursement(data):
|
def disbursement(data):
|
||||||
|
BANK_CALL_BASE_URL = "https://bank-emulator.dev.simbrellang.net"
|
||||||
api_url = f"{SimbrellaClient.BASE_URL}/Disbursement"
|
api_url = f"{BANK_CALL_BASE_URL}/Disbursement"
|
||||||
|
logger.info(f"BANK_CALL_BASE_URL = {BANK_CALL_BASE_URL}")
|
||||||
logger.info(f"Calling disbursement endpoint with data: {data}")
|
logger.info(f"Calling disbursement endpoint with data: {data}")
|
||||||
|
|
||||||
response = requests.post(
|
data1 ={
|
||||||
api_url,
|
"requestId": "7876786",
|
||||||
json=data,
|
"transactionId": "T001",
|
||||||
headers=get_headers()
|
"debtId": "273194670",
|
||||||
)
|
"customerId": "CN621868",
|
||||||
|
"accountId": "2017821799",
|
||||||
|
"productId": "101",
|
||||||
|
"provideAmount": 100000,
|
||||||
|
"collectAmountInterest": 5000,
|
||||||
|
"collectAmountMgtFee": 1000,
|
||||||
|
"collectAmountInsurance": 1000,
|
||||||
|
"collectAmountVAT": 75,
|
||||||
|
"countryId": "01",
|
||||||
|
"comment": "Testing LoanRequest"
|
||||||
|
}
|
||||||
|
try:
|
||||||
|
# response = requests.post(
|
||||||
|
# api_url,
|
||||||
|
# json=data1,
|
||||||
|
# headers=get_headers()
|
||||||
|
# )
|
||||||
|
|
||||||
logger.info(f"Disbursement response: {response.json()}")
|
# headers = {
|
||||||
|
# 'Content-Type': 'application/json',
|
||||||
|
# 'x-api_key': f'{settings.VALID_API_KEY}',
|
||||||
|
# 'App-Id': f'{settings.VALID_APP_ID}'
|
||||||
|
# }
|
||||||
|
#
|
||||||
|
response = requests.post(api_url, json=data1, timeout=10, headers=get_headers())
|
||||||
|
logger.info(f"Disbursement response: {response.json()}")
|
||||||
|
logger.info(f"Here is your disbursement data: {data1}")
|
||||||
|
except Exception as e:
|
||||||
|
logger.info(f"Failed to call disbursement endpoint: {e}")
|
||||||
|
#raise
|
||||||
|
return 0
|
||||||
|
|
||||||
return jsonify(response.json()), response.status_code
|
# return jsonify(response.json()), response.status_code
|
||||||
|
return 1
|
||||||
+8
-2
@@ -2,8 +2,14 @@ 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": settings.BANK_CALL_API_KEY,
|
"x-api-key": "test-api-key-12345",
|
||||||
"App-Id": settings.BANK_CALL_APP_ID,
|
"App-Id": "app1",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -6,19 +6,30 @@ from app.utils.logger import logger
|
|||||||
app = create_app()
|
app = create_app()
|
||||||
|
|
||||||
if __name__ != "__main__":
|
if __name__ != "__main__":
|
||||||
#
|
|
||||||
# kafka = KafkaIntegration()
|
#Expose WSGI app instance for Gunicorn
|
||||||
#
|
wsgi_app = app
|
||||||
# logger.info("Starting Kafka consumer...")
|
|
||||||
# while True:
|
kafka = KafkaIntegration()
|
||||||
# message = kafka.receive_disbursement_messages(
|
|
||||||
# topic=settings.KAFKA_PAYMENT_TOPIC, timeout=settings.KAFKA_TIMEOUT
|
logger.info("Starting Kafka consumer...")
|
||||||
# )
|
while True:
|
||||||
#
|
try:
|
||||||
# if message:
|
|
||||||
# logger.info(f"Processed message: {message}")
|
message = kafka.receive_disbursement_messages(
|
||||||
# else:
|
topic=settings.KAFKA_PAYMENT_TOPIC, timeout=settings.KAFKA_TIMEOUT
|
||||||
# logger.info("No message received within timeout")
|
)
|
||||||
|
|
||||||
|
if message:
|
||||||
|
logger.info(f"Processed message: {message}")
|
||||||
|
else:
|
||||||
|
logger.info("No message received within timeout")
|
||||||
|
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"Error while receiving message: {e}")
|
||||||
|
raise
|
||||||
|
|
||||||
|
|
||||||
# Expose WSGI app instance for Gunicorn
|
# Expose WSGI app instance for Gunicorn
|
||||||
wsgi_app = app
|
# wsgi_app = app
|
||||||
|
|||||||
Reference in New Issue
Block a user