Last active
July 8, 2022 15:10
-
-
Save an01f01/6df6feafdfac7926abca2d28e7489f61 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): | |
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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment