Task6
Step 1: Use a Public API
We demonstrate how to consume a public API using the requests library in Python.
Run the public_api.py script:
python public_api.py
This script fetches current weather data using the Open-Meteo API (which requires no API key). The script also contains commented-out code for OpenWeatherMap if you'd like to try it with your own API key.
Step 2: Build APIs with FastAPI
We've built a mini API with FastAPI in main.py.
Installation
Make sure to install the required packages. You can create a virtual environment first:
python -m venv venv
source venv/bin/activate
pip install -r requirements.txt
Running the API
Run the FastAPI development server:
uvicorn main:app --reload
Endpoints
-
GET /greet/{name}: Greets the given name. Example: http://127.0.0.1:8000/greet/Srilatha -
GET /villages: Returns the list of villages. -
POST /add-village: Adds a village to the in-memory list.
API Documentation
FastAPI automatically generates interactive API documentation. Once the server is running, you can visit:
- Swagger UI: http://127.0.0.1:8000/docs
- ReDoc: http://127.0.0.1:8000/redoc