Refactor output; add error response
This commit is contained in:
parent
3e4f879178
commit
eeb6a85ebc
1 changed files with 17 additions and 7 deletions
|
@ -8,8 +8,12 @@ import sqlite3
|
|||
|
||||
DATABASE = '/usr/local/www/mocha-server/mocha.db'
|
||||
|
||||
# TODO: Add fetching of list of users
|
||||
# TODO: Add fetching of top N users by score
|
||||
# TODO: Add ability to store and retrieve avatars (as image files?)
|
||||
|
||||
def fetch(user_id):
|
||||
|
||||
def fetch_user(user_id):
|
||||
"""
|
||||
Returns a JSON object containing the requested user
|
||||
Also can return a list of all users if user_id == "*"
|
||||
|
@ -27,8 +31,6 @@ def fetch(user_id):
|
|||
output = json.dumps([dict(row) for row in output])
|
||||
conn.close()
|
||||
|
||||
if output is None:
|
||||
output = ('Error',)
|
||||
return output
|
||||
|
||||
|
||||
|
@ -45,10 +47,11 @@ def process_request(uri):
|
|||
output = ''
|
||||
|
||||
if parts[1] == 'users':
|
||||
output = fetch(parts[2])
|
||||
return output
|
||||
output = fetch_user(parts[2])
|
||||
|
||||
|
||||
return output
|
||||
|
||||
return None
|
||||
|
||||
|
||||
def application(environ, start_response):
|
||||
|
@ -56,10 +59,17 @@ def application(environ, start_response):
|
|||
mod_wsgi entry point
|
||||
"""
|
||||
status = '200 OK'
|
||||
output = process_request(environ['REQUEST_URI']).encode('UTF-8')
|
||||
output = process_request(environ['REQUEST_URI'])
|
||||
|
||||
if output is None:
|
||||
status = '404 Not Found'
|
||||
output = ''
|
||||
|
||||
output = output.encode('UTF-8')
|
||||
|
||||
response_headers = [('Content-type', 'application/json'),
|
||||
('Content-Length', str(len(output)))]
|
||||
|
||||
start_response(status, response_headers)
|
||||
|
||||
return [output]
|
||||
|
|
Reference in a new issue