-
-
Save an01f01/19c5deae3925b9ff2ba1a1677cf92330 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
class BookHandler(BaseHandler): | |
def initialize(self): | |
database_url = os.environ['BOOKS_DB_CONN'] | |
self.session = queries.TornadoSession(uri=database_url) | |
""" | |
GET handler for fetching numbers from database | |
""" | |
@gen.coroutine | |
def get(self, **params): | |
auth_header = self.request.headers.get('Authorization') | |
print(auth_header) | |
if auth_header: | |
auth_token = auth_header.split(" ")[1] | |
else: | |
auth_token = '' | |
if auth_token: | |
token = decode_auth_token(auth_token) | |
if token['success'] == 0: | |
try: | |
sql = "SELECT row_to_json(json) json FROM (SELECT bookid, title, book_info FROM books WHERE bookid = %(book_id)s) as json;" | |
results = yield self.session.query(sql, {'book_id': params['id']}) | |
data_ret = results.as_dict() | |
results.free() | |
self.set_status(200) | |
self.write({'message': 'Book with id ' + params['id'], 'book': data_ret['json']}) | |
self.finish() | |
except (queries.DataError, queries.IntegrityError) as error: | |
print(error) | |
self.set_status(500) | |
self.write({'message': 'Error: no book with that id exists', 'book': {} }) | |
self.finish() | |
else: | |
self.set_status(401) | |
self.write({'status': 'fail', 'message': token['message'] }) | |
self.finish() | |
return | |
else: | |
self.set_status(403) | |
self.write({'status': 'fail', 'message': 'Invalid authentication token' }) | |
self.finish() | |
return |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment