-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.py
149 lines (116 loc) · 5.3 KB
/
api.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import os
import json
import logging
from wsgiref import simple_server
import falcon
from models import *
from playhouse.shortcuts import model_to_dict
import jwt
from datetime import datetime,timedelta
EMAIL = "modulus@helloworld.in"
PASS = "foobar"
def jwtAuth(req,resp,resource,params):
token = req.get_header('Authorization')
if token is None:
description = ('Please provide an auth token '
'as part of the request.')
raise falcon.HTTPUnauthorized('Authentication Error',
description)
if not _token_is_valid(token):
description = ('The auth token is invalid or has expired. '
'Please request a new token and try again.')
raise falcon.HTTPUnauthorized('Authentication Token Error',
description)
def _token_is_valid(token):
try:
payload = jwt.decode(token.split(" ")[1],os.environ.get('JWT_SECRET'), algorithms='HS256')
return True
except jwt.ExpiredSignatureError:
return False
except jwt.InvalidTokenError:
return False
class RedirectToGithub:
def __init__(self):
self.logger = logging.getLogger('bank-api-redirectToGithub' + __name__)
def on_get(self, req, resp):
resp.set_header("Powered-By","Falcon")
resp.status = falcon.HTTP_200
resp.body = json.dumps({'status' : 'OK', 'API-DOCS-LINK' : 'https://github.com/suryatmodulus/falcon-bank-api/blob/master/README.md'})
class GetToken:
def __init__(self):
self.logger = logging.getLogger('bank-api-getToken' + __name__)
def on_get(self, req, resp):
raise falcon.HTTPBadRequest(
'Request Method Error',
'getToken excepts POST Request')
def on_post(self, req, resp):
email = req.get_param('email') or ''
password = req.get_param('password') or ''
raw = req.get_param('raw') or ''
print(email,password)
if(email==EMAIL and password==PASS):
payload = {'email': EMAIL, 'exp': datetime.utcnow() + timedelta(days=5)}
jwt_token = jwt.encode(payload,os.environ.get('JWT_SECRET'), algorithm='HS256').decode('utf-8')
result = {"auth": "OK", "token" : jwt_token}
resp.set_header("Powered-By","Falcon")
resp.status = falcon.HTTP_200
if(not raw=='' and raw=="true"):
resp.body = jwt_token
else:
resp.body = json.dumps(result,indent=4, sort_keys=True)
else:
description = ('Make sure your email and password are correct.')
raise falcon.HTTPUnauthorized('Incorrect Authentication Credentials',
description)
class GetBankDetails:
def __init__(self):
self.logger = logging.getLogger('bank-api-getBankDetails' + __name__)
@falcon.before(jwtAuth)
def on_get(self, req, resp):
ifsc = req.get_param('ifsc') or ''
offset = req.get_param_as_int('offset') or 0
limit = req.get_param_as_int('limit') or 0
if(ifsc==''):
raise falcon.HTTPMissingParam('ifsc')
try:
result = model_to_dict(Branches.select().where(Branches.ifsc == ifsc.upper()).offset(offset).limit(limit).get())
except Exception as ex:
self.logger.error(ex)
raise falcon.HTTPServiceUnavailable('Resource Not Found','Server was unable to fetch the requested data.')
resp.set_header('Powered-By', 'Falcon')
resp.status = falcon.HTTP_200
resp.body = json.dumps(result,indent=4, sort_keys=True)
class GetBranchDetails:
def __init__(self):
self.logger = logging.getLogger('bank-api-getBranchDetails' + __name__)
@falcon.before(jwtAuth)
def on_get(self, req, resp):
bank_name = req.get_param('bank_name') or ''
city = req.get_param('city') or ''
offset = req.get_param_as_int('offset') or 0
limit = req.get_param_as_int('limit') or 0
if(bank_name=='' or city==''):
raise falcon.HTTPMissingParam('bank_name and city')
try:
result = list(model_to_dict(branch) for branch in Branches.select().join(Banks).where(Banks.name == bank_name.upper(), Branches.city==city.upper()).offset(offset).limit(limit))
if not result:
raise falcon.HTTPServiceUnavailable('Resource Not Found','Server was unable to fetch the requested data.')
except Exception as ex:
self.logger.error(ex)
raise falcon.HTTPServiceUnavailable('Resource Not Found','Server was unable to fetch the requested data.')
resp.set_header('Powered-By', 'Falcon')
resp.status = falcon.HTTP_200
resp.body = json.dumps(result,indent=4, sort_keys=True)
# Configure your WSGI server to load "things.app" (app is a WSGI callable)
app = falcon.API()
app.add_route('/',RedirectToGithub())
app.add_route('/api',RedirectToGithub())
app.add_route('/api/getToken', GetToken())
app.add_route('/api/getBankDetails', GetBankDetails())
app.add_route('/api/getBranchDetails', GetBranchDetails())
if __name__ == '__main__':
httpd = simple_server.make_server('127.0.0.1', 4000, app)
print("Server started @ port 4000")
httpd.serve_forever()
# IFSC - BARB0MITHAP
# Bank - BANK OF BARODA,CHENNAI