Code Monkey home page Code Monkey logo

fastapi-example's Introduction

Authors and books

This is an example for implementing a REST API using FastAPI and coroutines and the following features:

  • request body validation
  • response body validation
  • query parameter
  • non default response code
  • indicating additional response schemas with different response status
  • exception handling

The REST API provides CRUD operations for author and book models where each book has one author.

Author(id, name):

  • create: POST /authors
  • retrieve:
    • GET /authors
    • GET /authors/{id}
  • update: PATCH /authors/{id}
  • delete: DELETE /authors/{id}

Book(id, name, author_id):

  • create: POST /books
  • retrieve:
    • GET /books with optional filter by author ID
    • GET /books/{id}
  • update: PATCH /books/{id}
  • delete: DELETE /books/{id}

Data are stored using sqlite.

Note: The constraint of existing foreign key is not applied. PRAGMA foreign_keys=ON needs to be executed in the database connection.

Testing

Go to the directory of the git repository.

Create and activate a Python virtual environment:

python -m venv .venv
source .venv/bin/activate
pip install --upgrade pip

Install dependencies:

pip install -r requirements.txt

Run end-to-end tests:

pytest -v

Running the application

The path of the database file can be configured with environment variable:

export MS_DATABASE_URL=sqlite:///./bookstore.db

Go to the directory of the git repository and activate the virtual environment.

Start the application:

uvicorn app.api:api

Check the generated OpenApi documentation:

${BROWSER} http://localhost:8000/docs

Perform a request to an endpoint, e.g.:

curl -i -X POST http://localhost:8000/authors -H 'content-type: application/json' -d '{"name": "Kate Brewster"}'

fastapi-example's People

Contributors

matez0 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.