https://github.com/ruanbekker/python-fastapi-restapi-basic
Basic Example using Python FastAPI
https://github.com/ruanbekker/python-fastapi-restapi-basic
api docker docker-compose fastapi python redoc swagger
Last synced: about 2 months ago
JSON representation
Basic Example using Python FastAPI
- Host: GitHub
- URL: https://github.com/ruanbekker/python-fastapi-restapi-basic
- Owner: ruanbekker
- Created: 2021-11-07T09:17:26.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2022-03-04T07:44:12.000Z (over 4 years ago)
- Last Synced: 2025-10-28T09:56:42.380Z (8 months ago)
- Topics: api, docker, docker-compose, fastapi, python, redoc, swagger
- Language: Python
- Homepage:
- Size: 12.7 KB
- Stars: 1
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# python-fastapi-restapi-basic
Basic Example using Python FastAPI
## About
This is a example using fastapi using python directly or using docker-compose.
## Getting Started
### Docker
Build:
```bash
$ docker-compose build
```
Start:
```bash
$ docker-compose up
```
### Non-Docker
Create a virtual environment:
```bash
$ python3 -m pip install virtualenv
$ python3 -m virtualenv -p python3 .venv
$ source .venv/bin/activate
```
Install dependecies:
```bash
$ python3 -m pip install -r requirements.pip
```
### Tests
Run tests using pytest:
```bash
$ pytest
=============================================== test session starts ===============================================
platform darwin -- Python 3.7.12, pytest-7.0.1, pluggy-1.0.0
rootdir: /Users/ruan/git/python-fastapi-restapi-basic
plugins: anyio-3.3.4
collected 1 item
tests/test_main.py . [100%]
================================================ 1 passed in 0.30s ================================================
```
### Start the Server
Start the application:
```bash
$ hypercorn main:app --reload
```
## API Usage
Seed the in-memory database:
```bash
$ curl -XPOST http://localhost:8000/seed
{"msg":"seeded 3 users"}
```
Get all the students:
```bash
$ curl -s http://localhost:8000/students
[
{
"userid":"ruanb",
"email":"ruanb@localhost"
},
{
"userid":"jamesd",
"email":"jamesd@localhost"
},
{
"userid":"deant",
"email":"deant@localhost"
}
]
```
Get a single student:
```bash
$ curl -s http://localhost:8000/students/ruanb
[{"userid":"ruanb","email":"ruanb@localhost"}]
```
Register a student:
```bash
$ curl -XPOST -H 'Content-Type: application/json' http://localhost:8000/students -d '{"userid": "frankp", "email": "frankp@localhost"}'
{"userid":"frankp","email":"frankp@localhost"}
```
Update a student:
```bash
$ curl -s -H "Content-Type: application/json" -XPUT http://localhost:8000/students/ruanb -d '{"userid":"ruanb", "email":"ruan.bekker@localhost"}'
```
Delete a student:
```bash
$ curl -XDELETE http://localhost:8000/students/ruanb
{"msg":"deleted ruanb"}
```
## API Documentation
If you head over to http://localhost:8000/docs you will get to the [Swagger](https://github.com/swagger-api) UI:

If you head over to http://localhost:8000/redoc you will get to the [Redoc](https://github.com/Redocly/redoc) UI:
