In this exercise, you will implement a calculator API that serves endpoints for mathematical functions using FastAPI. The goal is to make you familiar with building APIs, poetry
for package and dependency management and pytest
for testing. You should also make sure that your code is properly formatted using black
and isort
.
- Verify if poetry is installed on your machine using
poetry --version
. - Clone this repository
- In a terminal, navigate to the project's root folder
- Use poetry to generate a new package called
math_api
. Refer to the documentation of thepoetry new
command to find out how to do that. This will generate the following project structure:. ├── math_api │ ├── README.md │ ├── math_api │ │ └── __init__.py │ ├── pyproject.toml │ └── tests │ └── __init__.py
- Create a FastAPI app in
math_api/app.py
(so on the level ofpyproject.toml
) that serves your FastAPI API. The logic of your app should exist inmath_api/math_api/
. See FastAPI's 'first steps' documentation to help you to get started. Manage your dependencies with poetry so that teammates could easily set up their own environments. Also try to make a distinction between dependencies for development and dependencies for code that runs in production. - Implement the following endpoints:
-
/add
which accepts two parametersa
andb
and returns the result ofa+b
-
/sub
which accepts two parametersa
andb
and returns the result ofa-b
-
/mul
which accepts two parametersa
andb
and returns the result ofa*b
-
/div
which accepts two parametersa
andb
and returns the result ofa/b
-
/fib
which accepts a parametern
and returns Fibonacci number$F_n$
-
- During development, pay attention to your project structure and add type hints for your methods and parameters.
- Document your code with docstrings.
- Write tests for each method with
pytest
- Format your code with black, isort and flake8.
Extra:
- Install pre-commit and configure it so it runs black, flake8, isort and pytest.
- Write a shell script that runs your tests and formatting checks, collects the return codes and writes a report to a file.
- Package your app with poetry.