There are many different ways to organize your Flask-RESTful app, but here we’ll describe one that scales pretty well with larger apps and maintains a nice level organization.
The basic idea is to split your app into three main parts: the routes, the resources, and any common infrastructure.
Here’s an example directory structure:myapi/ __init__.py app.py # this file contains your app and routes resources/ __init__.py foo.py # contains logic for /Foo bar.py # contains logic for /Bar common/ __init__.py util.py # just some common infrastructure
The common directory would probably just contain a set of helper functions to fulfill common needs across your application. It could also contain, for example, any custom input/output types your resources need to get the job done.
In the resource files, you just have your resource objects. So here’s what
foo.pymight look like:from flask_restful import Resource class Foo(Resource): def get(self): pass def post(self): pass
The key to this setup lies in
app.py:from flask import Flask from flask_restful import Api from myapi.resources.foo import Foo from myapi.resources.bar import Bar from myapi.resources.baz import Baz app = Flask(__name__) api = Api(app) api.add_resource(Foo, '/Foo', '/Foo/<string:id>') api.add_resource(Bar, '/Bar', '/Bar/<string:id>') api.add_resource(Baz, '/Baz', '/Baz/<string:id>')
Powerful image optical character recognition (OCR) for over 20 languages and with machine-readable-zone support. Perfect for receipt and invoice scanning as well as general image-based text extraction.
Creating and exposing APIs allows your web application to interact with other applications through machine-to-machine communication.
API creation frameworks
- Django REST framework and Tastypie are the two most widely used API frameworks to use with Django. The edge currently goes to Django REST framework based on rough community sentiment. Django REST framework continues to knock out great releases after the 3.0 release mark when Tom Christie ran a successful Kickstarter campaign.
- Flask-RESTful is widely used for creating web APIs with Flask. It was originally open sourced and explained in a blog post by Twilio then moved into its own GitHub organization so engineers from outside the company could be core contributors.
- Flask API is another common library for exposing APIs from Flask web applications.
- Email Newsletters
- Password Resets
- Promotional Emails
- Shipping Notifications With Confidence
The basics of API design
If you’re spending your time discussing if URLs should be in the singular
/restaurants/:id, then that means you’re not creating evolvable APIs.
.. The server may ask for all the fields in the first request, or they may acknowledge some fields and ask for the remaining ones in another request. The most important bit here is that the server responds to each interaction with the same interface.
.. This way you have no coupling between one specific URL and the response. One team can develop the client, and another team can develop the server; they only need to agree on the communication format. There’s no need to deploy both client and server at the same time in a specific order!
In Domain Driven Design, you model the architecture of the code with how the people from your business operate. With an evolvable API, it’s the same. You model the asynchronous communication between machines exactly how people in your business communicate.