pass data to functions

This commit is contained in:
Tim Bentley 2013-09-14 08:22:01 +01:00
parent 448da916e9
commit cca76a90bd
2 changed files with 13 additions and 32 deletions

View File

@ -154,7 +154,7 @@ class HttpRouter(object):
(r'^/stage/poll$', {'function': self.poll, 'secure': False}), (r'^/stage/poll$', {'function': self.poll, 'secure': False}),
(r'^/main/poll$', {'function': self.poll, 'secure': False}), (r'^/main/poll$', {'function': self.poll, 'secure': False}),
(r'^/main/image$', {'function': self.main_poll, 'secure': False}), (r'^/main/image$', {'function': self.main_poll, 'secure': False}),
(r'^/api/controller/(live|preview)/(.*)$', {'function': self.controller, 'secure': False}), (r'^/api/controller/(live|preview)/(.*)$', {'function': self.controller, 'secure': True}),
(r'^/stage/controller/(live|preview)/(.*)$', {'function': self.controller, 'secure': False}), (r'^/stage/controller/(live|preview)/(.*)$', {'function': self.controller, 'secure': False}),
(r'^/api/service/(.*)$', {'function':self.service, 'secure': False}), (r'^/api/service/(.*)$', {'function':self.service, 'secure': False}),
(r'^/stage/service/(.*)$', {'function': self.service, 'secure': False}), (r'^/stage/service/(.*)$', {'function': self.service, 'secure': False}),
@ -184,42 +184,20 @@ class HttpRouter(object):
``*args`` ``*args``
Any passed data. Any passed data.
""" """
self.request_data = None
url_path_split = urlparse(url_path) url_path_split = urlparse(url_path)
url_query = parse_qs(url_path_split.query)
if 'data' in url_query.keys():
self.request_data = url_query['data'][0]
for route, func in self.routes: for route, func in self.routes:
match = re.match(route, url_path_split.path) match = re.match(route, url_path_split.path)
if match:
print('Route "%s" matched "%s"', route, url_path)
args = []
for param in match.groups():
args.append(param)
return func, args
return None, None
def _process_http_request(self, url_path, *args):
"""
Common function to process HTTP requests
``url_path``
The requested URL.
``*args``
Any passed data.
"""
response = None
for route, func in self.routes:
match = re.match(route, url_path)
if match: if match:
log.debug('Route "%s" matched "%s"', route, url_path) log.debug('Route "%s" matched "%s"', route, url_path)
args = [] args = []
for param in match.groups(): for param in match.groups():
args.append(param) args.append(param)
response = func(*args) return func, args
break return None, None
if response:
return response
else:
log.debug('Path not found %s', url_path)
return self.do_not_found()
def do_http_success(self): def do_http_success(self):
self.send_response(200) self.send_response(200)
@ -469,6 +447,8 @@ class HttpRouter(object):
event += '_item' event += '_item'
if self.request_data: if self.request_data:
try: try:
# print(json.loads(self.request_data['data']))
print(json.loads(self.request_data))
data = json.loads(self.request_data)['request']['id'] data = json.loads(self.request_data)['request']['id']
except KeyError: except KeyError:
return self._http_bad_request() return self._http_bad_request()

View File

@ -72,12 +72,13 @@ class CustomHandler(BaseHTTPRequestHandler, HttpRouter):
""" """
if self.path == '/favicon.ico': if self.path == '/favicon.ico':
return return
print(self.headers['content-type']) #print(self.headers['content-type'],self.headers['content-length'])
if self.headers['content-type'] == 'application/json': if self.headers['content-type'] == 'application/text':
length = int(self.headers['content-length']) length = int(self.headers['content-length'])
postvars = parse_qs(self.rfile.read(length), keep_blank_values=1) postvars = parse_qs(self.rfile.read(length), keep_blank_values=1)
for var in postvars: for var in postvars:
print(var.decode("utf-8")) print(var)
#{"request": {"id": 1}}
if not hasattr(self, 'auth'): if not hasattr(self, 'auth'):
self.initialise() self.initialise()
function, args = self.process_http_request(self.path) function, args = self.process_http_request(self.path)