...

/

Getting Started with the API of Langman

Getting Started with the API of Langman

Learn about the API we will be using for Langman.

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.

Press + to interact
from flask import Flask
from flask_restplus import Resource, Api, Namespace
from flask_cors import CORS
from .util import get_config
games_api = Namespace('games', description='Creating and playing games')
@games_api.route('')
class Games(Resource):
pass
class OneGame(Resource):
pass
# Create the app and configure it
app = Flask(__name__) # Create Flask app
app.config.update(get_config(app.config['ENV'],
app.open_resource('config.yaml')))
CORS(app) # Cross-origin resource sharing
api = Api(app) # Create RESTplus api on app
api.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 ...