Latest news about Bitcoin and all cryptocurrencies. Your daily crypto news habit.
It wouldnât be an overstatement to say that REST APIs power the web. Everything from social networks to the apps that help you order pizza is powered by REST APIs. In fact, mobile apps, enterprise applications, and even games need to talk to servers once in a (very short) while. Thereâs no point in limiting REST API tutorials to just web developers.
Photograph by Sam LoydTornado & Motor
Tornado is a mature, battle-tested web framework written in Python. Motor is an async driver for MongoDB that integrates really well with Tornado. These are going to be the two main tools we will be working as we create our superhero API.
Prerequisites and initial setup
Before we begin, please ensure that you have:
- python>=3.6 installed on your machine.
- The venv module along with pip installed.
- mongodb up and running at localhost:27017.
- A decent text editor or IDE.
On Ubuntu and similar systems, venv and pip can be easily installed using:
$ sudo apt install python3-pip python3-venv
Create a virtual environment for our new project.
$ mkdir heroes$ cd heros$ python3 -m venv env
Install the project dependencies.
$ source env/bin/activate$ pip install tornado motor
Initial App Configuration
Create a folder named heroes inside the project folder (which is confusingly also named heroes but this is how things are!). This folder will contain our .py files.
- In the app.py file, we create an instance of tornado.web.Application and make the tornado.httpserver.HTTPServer serve it.
- The handlers.py file contains the request handlers that inherit from tornado.web.RequestHandler.
- The HomeHandler implements the get method so that it can handle GET requests. Tornado automatically sends the response as JSON when a dictionary is passed as a parameter to the write method.
- The ErrorHandler implements the prepare method which is called before the get or any other method. In this case, we send back an error message and finish the response.
Firing up the Motor
Connecting the MongoDB driver to Tornado is simpler than most web frameworks.
- Attach an instance of the MotorClient to the Application object.
- Use the heroes database.
- Refactor the handlers to inherit from a BaseHandler. This step is not required, but it will save us from a lot of duplicate code in the future.
Thank you Pycharm for these awesome Git Diffs!If you prefer to copy, you can find the updated files here.An API for Super Heroes
Add a /heroes endpoint that allows CRUD operations for the hero resource.
- Add the /heroes routes to the Application instance in app.py.
- Add the prepare method in the BaseHandler that automatically parses the request body as JSON for POST and PUT requests.
- Add the HeroesHandler class with get, put, delete, and post methods.
Two routes are added to the app. One for the /heroes endpoint and another for the /heroes/<hero_id> endpoint. While the former is the general endpoint for the resource, the latter is used for operations on a single item.
The changes in handlers.py are too many to show in a diff. Please check the complete source code listing below.
- The get method returns all the heroes for the /heroes route. For the /heroes/<hero_id> route, the details of a single hero are returned.
- The get method also implements a basic form of paging.
Disclaimer
The views and opinions expressed in this article are solely those of the authors and do not reflect the views of Bitcoin Insider. Every investment and trading move involves risk - this is especially true for cryptocurrencies given their volatility. We strongly advise our readers to conduct their own research when making a decision.