Note: You can use tools like: Postman, and curl for test the response from the API or any case, use your own web browser, and pass the query parameters by hand. :)
This project contains a small API implementation that shows one of the many possible ways to implement a RESTful API using Flask and Flask-restful libraries.
Main libraries used: 0. Flask framework - build light-way web apps.
- Flask Blueprints - for build modular web apps.
- Flask-RESTful - restful API library.
- requests - handle requests to external APIs.
- pytest / pytest-flask - Adds unit-test functionalities to the APIs.
- python-dotenv - handle enviroments variables.
Project structure:
.
├── README.md
├── src
│ ├── __init__.py
│ ├── api
│ │ ├── __init__.py
│ │ ├── resources.py
│ │ └── weather_helpers.py
│ └── main.py
├── tests
│ ├── __init__.py
│ ├── test_weather.py
├── .gitignore
├── docker-compse.yml
├── Dockerfile
├── requirements.txt
├── Pipfile
└── Pipfile.lock
-
src/api
src/api/resources.py
holds the resource for define the unique API endpoint, and their internal logic.src/api/weather_helpers.py
Where we define the helper functions to format comming from the external API.
-
tests/
src/test/test_weather.py
define test-suits for the endpoint- Unit-test: 🛠️
- Add unit-test cases for testing the functionaly of the helper functions.
Build
Clone repository
- Set env variables :
cd root_directory & touch .env
- Define env variables:
OPENWEATHERMAP_API_KEY=1508a9a4840a5574c822d70ca2132032 OPENWEATHERMAP_API_URL='https://api.openweathermap.org/data/2.5/weather'
- Build and Run :
docker-compose up --build
- Note: Only required on the first build
Running
4. Run Docker container after build use: docker-compose up weather
[GET] http://127.0.0.1:5000/api/weather?city=istmina&country=co
RESPONSE
{
"location_name": "Istmina, CO",
"temperature": "23.43 °C / -163.09 °F",
"wind": "Light Air, 0.29 m/s, South-Southwest",
"cloudiness": "moderate rain",
"pressure": "1009 hpa",
"humidity": "98%",
"sunrise": "06:00:34",
"sunset": "17:51:13",
"geo_coordinates": "[5.16, -76.68]",
"requested_time": "2023-12-01 09:01:37",
"forecast": {}
}
Some interesting posts that teach how to make some of the calculations. That I implement to format the data coming from the external API.