
- Created FastAPI application with SQLite database integration - Implemented OpenWeatherMap client with caching - Added endpoints for current weather, forecasts, and history - Included comprehensive error handling and validation - Set up Alembic migrations - Created detailed README with usage examples generated with BackendIM... (backend.im)
101 lines
2.7 KiB
Markdown
101 lines
2.7 KiB
Markdown
# Weather Data API
|
|
|
|
A powerful and efficient REST API for retrieving weather data from OpenWeatherMap. Built with FastAPI and SQLite.
|
|
|
|
## Features
|
|
|
|
- **Current Weather Data**: Get real-time weather data for any location by city name or coordinates
|
|
- **Weather Forecasts**: Retrieve 5-day weather forecasts
|
|
- **Historical Data**: Access historical weather data stored in the database
|
|
- **Built-in Caching**: Reduces API calls to OpenWeatherMap with intelligent caching
|
|
- **Error Handling**: Comprehensive error handling with clear error messages
|
|
- **Database Integration**: Stores weather data in SQLite for persistence
|
|
- **Swagger Documentation**: Interactive API documentation available at `/docs`
|
|
- **Health Check Endpoint**: Easily verify API operational status
|
|
|
|
## API Endpoints
|
|
|
|
- `GET /api/v1/weather/current`: Get current weather data
|
|
- `GET /api/v1/weather/forecast`: Get weather forecast data
|
|
- `GET /api/v1/weather/cities`: Get list of cities in the database
|
|
- `GET /api/v1/weather/history/{city_id}`: Get historical weather data for a city
|
|
- `GET /health`: Health check endpoint
|
|
|
|
## Tech Stack
|
|
|
|
- **FastAPI**: High-performance web framework for building APIs
|
|
- **SQLAlchemy**: SQL toolkit and ORM for database interactions
|
|
- **Pydantic**: Data validation and settings management
|
|
- **Alembic**: Database migration tool
|
|
- **httpx**: Asynchronous HTTP client for external API calls
|
|
- **SQLite**: Lightweight relational database
|
|
- **Tenacity**: Retry library for robust API calls
|
|
|
|
## Getting Started
|
|
|
|
### Prerequisites
|
|
|
|
- Python 3.8+
|
|
- pip package manager
|
|
|
|
### Installation
|
|
|
|
1. Clone the repository:
|
|
```
|
|
git clone <repository-url>
|
|
cd weatherdataapi
|
|
```
|
|
|
|
2. Install dependencies:
|
|
```
|
|
pip install -r requirements.txt
|
|
```
|
|
|
|
3. Run database migrations:
|
|
```
|
|
alembic upgrade head
|
|
```
|
|
|
|
4. Start the server:
|
|
```
|
|
uvicorn main:app --reload
|
|
```
|
|
|
|
5. Visit the API documentation:
|
|
```
|
|
http://localhost:8000/docs
|
|
```
|
|
|
|
## Environment Variables
|
|
|
|
The API can be configured using the following environment variables:
|
|
|
|
- `OPENWEATHERMAP_API_KEY`: Your OpenWeatherMap API key (default is provided)
|
|
- `CACHE_EXPIRE_IN_SECONDS`: Duration for which to cache weather data (default: 1800 seconds)
|
|
|
|
## Database Schema
|
|
|
|
The API uses two main data models:
|
|
|
|
- **City**: Stores information about cities (name, country, coordinates)
|
|
- **WeatherRecord**: Stores weather data points (temperature, humidity, etc.)
|
|
|
|
## Example Requests
|
|
|
|
### Get Current Weather by City Name
|
|
|
|
```
|
|
GET /api/v1/weather/current?city=London&country=GB
|
|
```
|
|
|
|
### Get Weather Forecast by Coordinates
|
|
|
|
```
|
|
GET /api/v1/weather/forecast?lat=51.5074&lon=0.1278
|
|
```
|
|
|
|
### Get Historical Weather Data
|
|
|
|
```
|
|
GET /api/v1/weather/history/1?start_date=2023-01-01T00:00:00Z&end_date=2023-01-02T00:00:00Z
|
|
``` |