Just a fun project to code a simple weather app. However, I wanted to get more familiar with new technologies such as:
To learn something about the technologies (follow some conventions mentioned in their documentations) and also to follow best practices I used the following concepts:
- env vars for WeatherClient domain and API Key (so we can change and for security reasons),
- Use FastAPI's Settings Object to load env vars into our FastAPI app
- InfluxDB Python client to connect with our InfluxDB
- App separation so our project has a nice structure
- Path parameters
- Query parameters
- How-To for NoSQL DBs a recipe how to connect with a NoSQL Database in FastAPI
To keep following the best practices and to have my code more structured I decided to follow the patterns below:
- A Client class for 3rd party integration - so the code responsible for requesting 3rd parties is separated from the rest
- Adapter design pattern - so I can convert the 3rd party response the way I want it to be used by my app
- dataclasses - thanks to Pydantic I could use real objects instead of plain dictionaries.
To make this project work you first need to set up the required env vars:
OPENWEATHER_API_KEY
OPENWEATHER_DOMAIN
INFLUXDB_ORG=<your InfluxDB organization>
INFLUXDB_BUCKET=<your InfluxDB initial bucket>
INFLUXDB_URL=http://influxdb:8086 (because we will create a connection between containers)
INFLUXDB_TOKEN=<your InfluxDB token that will be used for DB connection/queries)
You can receive the openweather values from your openweather account on https://home.openweathermap.org/api_keys
To set up your InfluxDB instance (using docker compose) first you need to create a separate env file called .infludb2.env
and fill the values for:
DOCKER_INFLUXDB_INIT_MODE=setup
DOCKER_INFLUXDB_INIT_USERNAME=<admin username>
DOCKER_INFLUXDB_INIT_PASSWORD=<admin password>
DOCKER_INFLUXDB_INIT_ORG=<your InfluxDB init organization>
DOCKER_INFLUXDB_INIT_BUCKET=<your InfluxDB initial bucket>
DOCKER_INFLUXDB_INIT_ADMIN_TOKEN=<your admin init token>
After that when you run the container and enter: http://127.0.0.1:8086/ you should see the InfluxDB logindashboard. After a successful login you can follow the "getting-started" tutorial.
Build and the application with the following:
docker compose up app
You can run it in the background if you use the -d
flag:
docker compose up -d app
The project follows some specific conventions thanks to pre-commit:
- isort
- black
- flake8
- no-commit-to-branch (main branch)
- bandit
- docformatter
- python-safety-dependencies-check
To install the GitHub pre-commit hooks. This can be done in your virtual environment by:
pre-commit install
Use the following command (inside the app contaienr) to execute all tests using pytest:
pytest
You can debug your project using a debugger. When working with docker containers it's easier to use a debugger called WDB. It allows to debug your workflow at runtime using a web browser. You can debug tests or flows using the installed wdb debugger. Don't worry, the docker-compose.yml file sets the necessary environment variables:
PYTHONBREAKPOINT: wdb.set_trace
WDB_SOCKET_SERVER: wdb
WDB_NO_BROWSER_AUTO_OPEN: 1
First, place a breakpoint:
breakpoint()
The start the WDB container in the background:
docker compose up -d wdb
After that run your piece of code and check the statement inside the interactive console on: http://127.0.0.1:1984/