Getting Started with the API of Langman
Learn about the API we will be using for Langman.
We'll cover the following...
Stubbing out the app
In this chapter, we’ll use API design principles. First, we stub out the application, that is, create do-nothing methods as placeholders, and its routes. We then create the database connection and fill in the routes. The configuration parameters are stored in the config.yaml
file.
from flask import Flaskfrom flask_restplus import Resource, Api, Namespacefrom flask_cors import CORSfrom .util import get_configgames_api = Namespace('games', description='Creating and playing games')@games_api.route('')class Games(Resource):passclass OneGame(Resource):pass# Create the app and configure itapp = Flask(__name__) # Create Flask appapp.config.update(get_config(app.config['ENV'],app.open_resource('config.yaml')))CORS(app) # Cross-origin resource sharingapi = Api(app) # Create RESTplus api on appapi.add_namespace(games_api, path='/api/games') # Insert games namespace# -- client expects /api/games; changed from /games to /api/games here.
The primary advantage of using an API namespace is that the API can be specified first in one file and then added to the app in a different file. Also, it is possible to define a collection of API pieces and then assemble them into one app at the end. For example, authentication and administration interactions can be put at the /auth
and /admin
base routes. Notice that the games
resource path is an empty string. It is then attached to games
...