Created
December 22, 2024 17:25
-
-
Save ajepe/154e93151f48242f97e9b00373dc9bfb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
class LoanEligibilityCheck: | |
""" | |
Base class for loan eligibility checks. Extend this to create custom eligibility checks. | |
""" | |
def is_eligible(self, sale_order): | |
raise NotImplementedError("Subclasses must implement the 'is_eligible' method.") | |
class LoanService: | |
""" | |
A service class for interacting with an external loan creation API. | |
""" | |
def __init__(self, api_url, api_key, eligibility_checks=None): | |
self.api_url = api_url | |
self.api_key = api_key | |
self.eligibility_checks = eligibility_checks or [] | |
def add_eligibility_check(self, check): | |
""" | |
Add a new eligibility check to the service. | |
""" | |
self.eligibility_checks.append(check) | |
def is_eligible(self, sale_order): | |
""" | |
Run all eligibility checks for the given sale order. | |
""" | |
for check in self.eligibility_checks: | |
if not check(sale_order): | |
return False | |
return True | |
def create_loan(self, sale_order): | |
""" | |
Send a POST request to the API to create a loan. | |
""" | |
headers = { | |
"Authorization": f"Bearer {self.api_key}", | |
"Content-Type": "application/json", | |
} | |
response = requests.post(self.api_url, json=sale_order, headers=headers) | |
response.raise_for_status() | |
return response.json() | |
def process_sale_order(self, sale_order): | |
""" | |
Process the sale order by checking eligibility, creating the loan, and confirming the order. | |
Args: | |
sale_order: The sale order data. | |
Returns: | |
bool: True if the sale order is confirmed, False otherwise. | |
""" | |
if not self.is_eligible(sale_order): | |
print("Sale order is not eligible for a loan.") | |
return False | |
try: | |
loan_response = self.create_loan(sale_order) | |
if loan_response.get("status") == "success": | |
print("Sale order confirmed.") | |
return True | |
else: | |
print("Loan creation failed:", loan_response.get("message", "Unknown error")) | |
except requests.RequestException as e: | |
print("Error communicating with the loan API:", e) | |
return False | |
# Example usage | |
if __name__ == "__main__": | |
def check_minimum_amount(sale_order): | |
return sale_order.get("amount", 0) >= 5000 | |
def check_maximum_amount(sale_order): | |
return sale_order.get("amount", 0) <= 50000 | |
def check_customer_credit_score(sale_order): | |
# Assume credit score is included in sale order for simplicity | |
return sale_order.get("customer_credit_score", 0) >= 700 | |
# Instantiate the service | |
loan_service = LoanService( | |
api_url="https://api.example.com/loans", | |
api_key="your_api_key_here", | |
) | |
# Add custom eligibility checks | |
loan_service.add_eligibility_check(check_minimum_amount) | |
loan_service.add_eligibility_check(check_maximum_amount) | |
loan_service.add_eligibility_check(check_customer_credit_score) | |
# Example sale order data | |
sale_order_data = { | |
"order_id": "SO1234", | |
"amount": 6000, | |
"customer_credit_score": 750, | |
"customer_id": "CUST5678", | |
} | |
# Process the sale order | |
loan_service.process_sale_order(sale_order_data) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
LOL What Is This