Any software is nowadays embedded between Web Servers and WebAPIs. Communication between services, applications and (web) apps often takes place using RESTful HTTP protocols.
The first Python approaches to this world usually occur with Flask. A micro-framework that - fortunately - allows us to create web applications potentially far from the monolithic style. In fact, when the application starts to become more robust, it emerges the need to define and design an architecture.
In this repository a small collection of best practices and examples of architectural patterns to promote the orderly development of WebAPIs.
Below is an illustration of the WebAPI examples listed in this repository.
This is the easiest example that can be realized in Flask: a website with a single route.
NAME | ROUTE |
---|---|
Index | / |
In this example we extend the possibilities of our website by integrating a second page (ex. Blog).
NAME | ROUTE |
---|---|
Index | /``` |
Blog | /blog``` |
It is possible to integrate HTML code into the responses of our website.
NAME | ROUTE |
---|---|
Index | / |
Blog | /blog |
When the web page becomes very complex and starts integrating HTML, CSS or JS code, it may be useful to refer to templates.
NAME | ROUTE |
---|---|
Index | / |
Blog | /blog |
Communication between the users and the website can take place in both directions: inward and outward. We can pass parameters using a HTTP GET request.
NAME | ROUTE |
---|---|
Index | / |
Blog | /blog |
Welcome Page | /welcome/ <your-name-here> |
WebApi usually respond through JSON. Let's integrate a small dataset inside the server.
NAME | ROUTE |
---|---|
Index | / |
Welcome Page | /welcome/<your-name-here> |
Data | /api/data |
Architecture: Flat. Data access: variable.
The number of parameters that are passed to a WebAPI, using the methodology proposed in the example (4), can become many and difficult to interpret. The approach shown in this example allows to communicate with a WebAPI in a flexible and easily manageable way.
NAME | ROUTE |
---|---|
Index | / |
Welcome Page Structured | /welcome/<your-name-here> |
Welcome Page Unstructured | /welcome?user=<your-name-here> |
Data | /api/data |
Architecture: Flat. Data access: variable.
In this example we join the experience matured in the examples (5) and (6).
NAME | ROUTE |
---|---|
Index | / |
API - Get all data | /api |
API - Query user by ID | /api/user?id=<id-here> |
Architecture: Flat. Data access: variable.
Here we expose readed data from a saved CSV file. Pandas package is used to read and query the data.
NAME | ROUTE |
---|---|
Index | / |
API - Get all data | /api |
API - Query user by ID | /api/user?id=<id-here> |
Architecture: Flat. Data access: CSV and Pandas.
Usually a Database is more suitable and secure to store and read data. In this case we use the simple SQLite3 package by still using a flat monolithic architecture.
NAME | ROUTE |
---|---|
Index | / |
API - Get all data | /api |
API - Query user by ID | /api/user?id=<id-here> |
Architecture: Flat. Data access: DB and SQLite.
The definition and design of an architecture becomes an increasingly important requirement as WebAPI grows. In this example, WebAPI is split into a start point, a routing module and a DAO (Data Access Object).
NAME | ROUTE |
---|---|
Index | / |
API - Infos about the API | /api |
API - Get all users | /api/users |
API - Insert new user | /api/newUser?id=<id-here>&name=<name-here>&age=<age-here> |
Architecture: Main, Routes (Controller) and DAO. Data access: DB and SQLite.
Here the case of the same WebAPI using the more structured SQLAlchemy package. Here we also included Models to help interact easily with DBs.
NAME | ROUTE |
---|---|
Index | / |
API - Infos about the API | /api |
API - Get all users | /api/users |
API - Insert new user in DB | /api/newUser?id=<id-here>&name=<name-here>&age=<age-here> |
Architecture: Main, Routes (Controller) and DAO. Data access: DB and SQLAlchemy.
These resources were created with ❤ by Alessio Vaccaro. Website | LinkedIn