Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

43 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

People API

A REST API application for managing people records, built with Flask and Connexion.

πŸ“– Overview

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.

πŸ› οΈ Tech Stack

  • Framework: Flask + Connexion
  • Database: SQLAlchemy (ORM)
  • API Documentation: Swagger/OpenAPI
  • Testing: pytest with mock server support

πŸš€ Getting Started

Prerequisites

  • Python 3.x
  • pipenv (recommended) or pip

Installation

  1. Clone the repository:

    git clone <repository-url>
    cd people-api
  2. Install dependencies using pipenv (recommended):

    pipenv shell
    pipenv install

    Or using pip:

    pip install -r Pipfile
  3. Start the server:

    python server.py
  4. Access the API documentation:

    • Swagger UI: http://127.0.0.1:5000/api/ui/
    • Web Interface: http://127.0.0.1:5000/

Note: On Windows, use 127.0.0.1 instead of 0.0.0.0 for better compatibility.

πŸ“ Project Structure

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

πŸ”Œ API Endpoints

People API

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

COVID Tracker API

Method Endpoint Description
GET /api/covid Get COVID-19 data

πŸ§ͺ Testing

Run tests using pytest:

pytest tests/

πŸ“¦ Postman Collection

Import the Postman collections from the postman/ folder to test the API:

  • People.postman_collection.json - People API tests
  • covid.postman_collection.json - COVID tracker tests

πŸ”§ Common Issues & Solutions

Swagger UI Installation

To install Swagger-UI, run:

pipenv install "connexion[swagger-ui]"

Note: On Windows with zsh, the URL must be quoted.

Windows-Specific Notes

  • Use 127.0.0.1 as the host instead of 0.0.0.0
  • When using curl, enclose URLs in double quotes instead of single quotes

πŸ“š References

This project follows the Real Python tutorial series by Doug Farrell:

πŸ“„ License

See the LICENSE file for details.

people-api

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.

Tech stack

Uses Flask, Connexion, Swagger and SQL Alchemy

Setup

  • 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 Pipfile with definitions of all required dependencies.
  • Execute pipenv shell to activate the virtualenv in your terminal
  • Execute pipenv install to 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.1 in above URL

Project structure

  • server.py - Entry point to start the Flask app with Connexion
  • config.py - Flask app configuration and database setup
  • people.py - CRUD operations for the People API
  • models.py - SQLAlchemy database models
  • build_database.py - Script to initialize the database
  • swagger.yml - Swagger spec defining API routes and documentation
  • users/ - User-related services and constants
  • covid_tracker/ - COVID tracking functionality
  • static/ - CSS and JS files for the web UI
  • templates/ - HTML templates
  • postman/ - Postman collections for API testing
  • tests/ - Unit and integration tests

Tests

This project includes unit and integration tests.

Running Tests

# Run all tests with pytest
pytest

# Run specific test file
pytest tests/test_mock_server.py

Common Gotchas

  • 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.1 as the host instead of 0.0.0.0
    • Also enclose the URL in CURL with double quotes instead of single quotes (Reference thread on stack overflow)

Reference

This follows the steps from a real python tutorial series written by Doug Farrell

Further read

Some useful posts to refer apart from the tutorials

About

API Testing in Python - Test Automation University

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages