A REST API application for managing people records, built with Flask and Connexion.
This project provides a RESTful API for managing a simple people record database. It includes features for CRUD operations, Swagger documentation, and a web interface.
- Framework: Flask + Connexion
- Database: SQLAlchemy (ORM)
- API Documentation: Swagger/OpenAPI
- Testing: pytest with mock server support
- Python 3.x
- pipenv (recommended) or pip
-
Clone the repository:
git clone <repository-url> cd people-api
-
Install dependencies using pipenv (recommended):
pipenv shell pipenv install
Or using pip:
pip install -r Pipfile
-
Start the server:
python server.py
-
Access the API documentation:
- Swagger UI:
http://127.0.0.1:5000/api/ui/ - Web Interface:
http://127.0.0.1:5000/
- Swagger UI:
Note: On Windows, use
127.0.0.1instead of0.0.0.0for better compatibility.
people-api/
βββ server.py # Main application entry point
βββ people.py # People API CRUD operations
βββ models.py # SQLAlchemy database models
βββ build_database.py # Database initialization script
βββ config.py # Configuration settings
βββ swagger.yml # OpenAPI/Swagger specification
βββ README.md # This file
βββ LICENSE # License file
β
βββ users/ # User-related modules
β βββ __init__.py
β βββ constants.py # User constants
β βββ mocks.py # Mock data for testing
β βββ services.py # User service logic
β
βββ covid_tracker/ # COVID-19 tracking module
β βββ covid_tracker.py # COVID tracker implementation
β βββ cases_summary.xml # COVID cases data
β
βββ static/ # Static assets
β βββ css/
β β βββ home.css
β βββ js/
β βββ home.js
β
βββ templates/ # HTML templates
β βββ __init__.py
β βββ home.html
β
βββ tests/ # Test suite
β βββ __init__.py
β βββ test_mock_server.py
β
βββ postman/ # Postman collections
βββ People.postman_collection.json
βββ people.postman_environment.json
βββ covid.postman_collection.json
βββ covid.postman_environment.json
| Method | Endpoint | Description |
|---|---|---|
| GET | /api/people |
Get all people |
| GET | /api/people/{id} |
Get person by ID |
| POST | /api/people |
Create new person |
| PUT | /api/people/{id} |
Update person |
| DELETE | /api/people/{id} |
Delete person |
| Method | Endpoint | Description |
|---|---|---|
| GET | /api/covid |
Get COVID-19 data |
Run tests using pytest:
pytest tests/Import the Postman collections from the postman/ folder to test the API:
People.postman_collection.json- People API testscovid.postman_collection.json- COVID tracker tests
To install Swagger-UI, run:
pipenv install "connexion[swagger-ui]"Note: On Windows with zsh, the URL must be quoted.
- Use
127.0.0.1as the host instead of0.0.0.0 - When using curl, enclose URLs in double quotes instead of single quotes
This project follows the Real Python tutorial series by Doug Farrell:
- Part 1: Building API and simple app with connexion, flask
- Part 2: Adding database and ORM (SQL Alchemy)
See the LICENSE file for details.
Hello ππ»,
This is a REST API with that simulates a simple people record database. Post following the setup
instructions, you may want to play around with the API's in either swagger or with the provided
postman collections in /postman folder. Enjoy and happy testing.
Uses Flask, Connexion, Swagger and SQL Alchemy
- Ensure you have pipenv available. Read this blog to understand all about pipenv
- Ensure you have cloned this repo and are in the project root directory that has the
Pipfilewith definitions of all required dependencies. - Execute
pipenv shellto activate the virtualenv in your terminal - Execute
pipenv installto install all dependencies - Execute
python server.py - To open swagger navigate to
http://0.0.0.0:5000/api/ui/
On a windows machine, you may want to replace the host as
127.0.0.1in above URL
server.py- Entry point to start the Flask app with Connexionconfig.py- Flask app configuration and database setuppeople.py- CRUD operations for the People APImodels.py- SQLAlchemy database modelsbuild_database.py- Script to initialize the databaseswagger.yml- Swagger spec defining API routes and documentationusers/- User-related services and constantscovid_tracker/- COVID tracking functionalitystatic/- CSS and JS files for the web UItemplates/- HTML templatespostman/- Postman collections for API testingtests/- Unit and integration tests
This project includes unit and integration tests.
# Run all tests with pytest
pytest
# Run specific test file
pytest tests/test_mock_server.py- To install swagger-ui, please run
pipenv install "connexion[swagger-ui]". Read this bug to understand about why zsh needs this to be quoted. - If you are on windows platform,
- Try using
127.0.0.1as the host instead of0.0.0.0 - Also enclose the URL in CURL with double quotes instead of single quotes (Reference thread on stack overflow)
- Try using
This follows the steps from a real python tutorial series written by Doug Farrell
- Part 1: Building API and simple app with connexion, flask, Check out the original repo here
- Part 2: Adding database and ORM (SQL Alchemy). Updated code can be found here
Some useful posts to refer apart from the tutorials