From 569cc6cd16cdfcf075cae5326b4c51e571ebb321 Mon Sep 17 00:00:00 2001 From: Arjun Dandagi Date: Fri, 2 Oct 2020 21:21:27 +0530 Subject: [PATCH] add support for python module --- .gitignore | 6 ++++ src/python/__init__.py | 0 src/python/ifsc.py | 64 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+) create mode 100644 src/python/__init__.py create mode 100644 src/python/ifsc.py diff --git a/.gitignore b/.gitignore index 42f6d68d..a2ceb61b 100644 --- a/.gitignore +++ b/.gitignore @@ -44,3 +44,9 @@ InstalledFiles # One-off scripts *.php + +#python +.idea +venv +__pycache__ +.pyc diff --git a/src/python/__init__.py b/src/python/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/src/python/ifsc.py b/src/python/ifsc.py new file mode 100644 index 00000000..b415e0c7 --- /dev/null +++ b/src/python/ifsc.py @@ -0,0 +1,64 @@ +import requests +import json + + +class IFSC(object): + """ A client for accessing the Razorpay API. """ + + """ + Initializes the Twilio Client + sets the BASE_URL + :returns IFSC object + """ + + def __init__(self): + self.BASE_URL = 'https://ifsc.razorpay.com/' + with open("../IFSC.json") as json_file: + self.bankdata = json.loads(json_file.read()) + pass + + """ + validate + :returns True if the code is valid + """ + + def validate(self, code: str): + if len(code) != 11: + return False + if code[4] != '0': + return False + _bankcode = code[0:4].upper() + _branchcode = code[5:].upper() + + if not _bankcode in self.bankdata: + return False + + _banklist = set(self.bankdata[_bankcode]) + + if _branchcode.isdigit(): + return int(_branchcode) in _banklist + + return _branchcode in _banklist + + """ + Fetches details for given code + :returns response from razorpay api for ifsc + :raises ValueErro for invalid data + """ + + def fetch_details(self, code: str): + _final_URL = self.BASE_URL + code + + if not self.validate(code): + raise ValueError(f'provided code is invalid') + headers = { + 'Content-Type': 'application/json', + } + #https://nedbatchelder.com/blog/200711/rethrowing_exceptions_in_python.html ( gives a full stack trace for exception) + try: + response = requests.get(_final_URL, headers=headers) + except Exception as e: + import sys + raise sys.exc_info()[1] + + return response.json()