A professional REST API for weather data collection and analytics, built with FastAPI and SQLAlchemy. This project demonstrates best practices in Python API development, including proper project structure, testing, containerization, and documentation.
- RESTful API with FastAPI
- Data persistence with SQLAlchemy and SQLite
- Comprehensive analytics endpoints
- Input validation with Pydantic
- Full test coverage with pytest
- Docker containerization
- Interactive API documentation (Swagger/ReDoc)
- Professional code structure following best practices
- FastAPI: Modern, fast web framework for building APIs
- SQLAlchemy: SQL toolkit and ORM
- Pydantic: Data validation using Python type hints
- SQLite: Lightweight database
- Pytest: Testing framework
- Docker: Containerization
- Uvicorn: ASGI server
weather-data-api/
βββ main.py # FastAPI application and endpoints
βββ models.py # SQLAlchemy database models
βββ schemas.py # Pydantic schemas for validation
βββ database.py # Database configuration
βββ test_main.py # Comprehensive test suite
βββ requirements.txt # Python dependencies
βββ Dockerfile # Docker container configuration
βββ docker-compose.yml # Docker Compose setup
βββ .gitignore # Git ignore rules
βββ README.md # This file
-
Clone the repository
git clone <your-repo-url> cd weather-data-api
-
Create a virtual environment
python -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate
-
Install dependencies
pip install -r requirements.txt
-
Run the application
uvicorn main:app --reload
The API will be available at http://localhost:8000
-
Build and run with Docker Compose
docker-compose up --build
-
Or build and run manually
docker build -t weather-api . docker run -p 8000:8000 weather-api
Once the application is running, visit:
- Swagger UI: http://localhost:8000/docs
- ReDoc: http://localhost:8000/redoc
| Method | Endpoint | Description |
|---|---|---|
| GET | / |
Root endpoint with API information |
| GET | /health |
Health check endpoint |
| POST | /weather |
Create new weather data entry |
| GET | /weather |
Get weather data with optional filtering |
| GET | /weather/{id} |
Get specific weather data by ID |
| DELETE | /weather/{id} |
Delete weather data entry |
| Method | Endpoint | Description |
|---|---|---|
| GET | /analytics/stats |
Get weather statistics for a location |
| GET | /analytics/locations |
Get list of available locations |
| GET | /analytics/trends/{location} |
Get temperature trends for a location |
curl -X POST "http://localhost:8000/weather" \
-H "Content-Type: application/json" \
-d '{
"location": "New York",
"temperature": 22.5,
"humidity": 65.0,
"pressure": 1013.25,
"description": "Partly cloudy"
}'# Get all weather data
curl "http://localhost:8000/weather"
# Filter by location
curl "http://localhost:8000/weather?location=New York"
# Pagination
curl "http://localhost:8000/weather?limit=10&skip=0"curl "http://localhost:8000/analytics/stats?location=New York&days=7"curl "http://localhost:8000/analytics/trends/New York?days=30"Run the test suite with pytest:
# Run all tests
pytest
# Run with verbose output
pytest -v
# Run with coverage report
pytest --cov=. --cov-report=htmlAll tests include proper setup and teardown, ensuring isolation between test cases.
id: Unique identifier (auto-generated)location: City or location nametemperature: Temperature in Celsiushumidity: Humidity percentage (0-100)pressure: Atmospheric pressure in hPadescription: Weather descriptiontimestamp: Record timestamp (auto-generated)
- Temperature: -100Β°C to 60Β°C
- Humidity: 0% to 100%
- Pressure: 800 hPa to 1100 hPa
- Location: 1-100 characters, non-empty
- Description: 1-100 characters
- Define new models in
models.py - Create Pydantic schemas in
schemas.py - Implement endpoints in
main.py - Write tests in
test_main.py - Update documentation
This project follows Python best practices:
- Type hints for better code clarity
- Comprehensive docstrings
- Proper error handling
- Input validation
- Database session management
- RESTful API design principles
-
Use PostgreSQL instead of SQLite:
DATABASE_URL = "postgresql://user:password@localhost/weather_db"
-
Set environment variables:
export DATABASE_URL="postgresql://..." export API_KEY="your-secret-key"
-
Enable HTTPS with a reverse proxy (nginx, Caddy)
-
Add authentication for protected endpoints
-
Implement rate limiting to prevent abuse
-
Set up logging and monitoring
Contributions are welcome! Please:
- Fork the repository
- Create a feature branch
- Write tests for new features
- Ensure all tests pass
- Submit a pull request
This project is open source and available under the MIT License.
For questions or feedback, please open an issue on GitHub.
Built with β€οΈ using FastAPI and modern Python practices
Another weather tracker dev project