July 2018
Intermediate to advanced
324 pages
8h 14m
English
We are going to use Falcon to create a single API with an allowed method as HTTP GET only. This API will have two different operations as we mentioned in the previous sections. The following is the code snippet of resources.py.
File—resources.py:
import osimport datetimeimport requestsimport falconfrom models import QuoteModelfrom mashape import fetch_quoteclass QuoteResource: def on_get(self, req, resp): """Handles GET requests""" if req.get_param('type') in ['daily', None]: data = QuoteModel.select().where(QuoteModel.created_at == datetime.date.today()) if data.exists(): data = data.get() resp.media = {'quote': data.quote, 'author': data.author, 'category': data.category} else: quote = fetch_quote() QuoteModel.create(**quote) ...Read now
Unlock full access