Grocery API
The initial repository for PyLadies Hamburg's "Introduction to FastAPI" workshop.
🔧 ⚙️ 💻 Before the workshop: Setup your computer
Part 1: Requirements
The setup instructions assumes you already have the following:
- Git
- Python 3.8 (if you have an existing installation of 3.9, that works too)
- The code editor you are most comfortable using
Part 2: Setup
Instructions are based on MacOS terminal but if you use Git Bash in Windows, the instructions should be fairly the same.
-
Clone this repository
a) In your terminal, executegit clone https://github.com/paxcodes/grocery_api
b) Still in the terminal, change your working directory:cd grocery_api
-
Create the virtual environment
-
Activate your virtual environment:
source .venv/bin/activate
1✅ Your terminal prompt will be prefixed with(grocery_api)
once the virtual environment is activated.✅ Runningwhich pip
andwhich python
should output the.venv
directory inside yourgrocery_api
folder. (e.g./grocery_api/.venv/bin/pip
and/grocery_api/.venv/bin/python
)✅ Runningpython --version
should output 3.8/3.9. If it isn't, delete your.venv
folder and repeat Step #2 onwards.
-
Install dependencies:
pip install -r requirements.txt
💡 This installs fastapi, uvicorn, pytest, requests, etc. and their dependencies. When creating your own FastAPI project, you'd dopip install fastapi
etc.✅ It should output something like,Successfully installed ... fastapi-0.65.2 ... uvicorn-0.14.0 ... pytest-6.2.4 ... requests-2.25.1
✅ Runningwhich uvicorn
orwhich pytest
should output the.venv
directory inside yourgrocery_api
folder. (e.g./grocery_api/.venv/bin/uvicorn
and/grocery_api/.venv/bin/pytest
)
-
Install the project:
pip install -e .
💡 This will install the grocery_api package locally. This is necessary to do forpytest
to work.✅ It should output something like,Successfully installed grocery-api-1.0
-
Run tests in the terminal by typing,
pytest
✅ It should output,... collected 25 items tests\test_endpoints.py . [ 4%] tests\crud\test_item.py ........... [ 48%] tests\crud\test_store.py ........... [ 92%] tests\crud\test_user.py .. [100%] ============== 25 passed in 0.XXs ===============
-
Run the server:
uvicorn grocery_api.main:app
✅ It should say,INFO: Application startup complete. INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
-
Go to your browser and visit, http://127.0.0.1:8000
✅ It should say,{ "message": "Hello World!" }
-
Exit the server by pressing
Ctrl+C
in the terminal.
If you have made it all the way to the end and everything is as expected (
If you are having trouble with any of the steps, reach out in PyLadies' Slack channel #city-hamburg.
1: In Windows, this would be source .venv/Scripts/activate
What to Expect in the Workshop
I will show how to implement 3 paths or endpoints in the FastAPI framework. A path to:
- Get all items
- Get a specific item given an Item ID (with some data validation using type-hinting)
- Create an item resource (with some data validation using Pydantic's
BaseModel
class + type-hinting)
Attendees will have a chance after every section to create similar endpoints for the store data (e.g. Get all stores, get a specific store, and create a store). There is a cheatsheet available that may help when you go through the exercises / workshop and need a quick review of a concept.
If time allows, I will demonstrate:
- Validating "outgoing" data (e.g. automatically strip sensitive data before sending data back)
- Testing an endpoint