Skip to content

Latest commit

 

History

18 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Weather API Wrapper

A FastAPI service that wraps the OpenWeatherMap API, providing a simplified, well-documented interface for retrieving current weather and 5-day forecast data by city.

🔗 Live Demo: https://weather-api-59gn.onrender.com/docs (Note: hosted on Render's free tier — the first request after inactivity may take up to ~50 seconds while the service wakes up)

Features

  • GET /weather/{city} — returns current weather for a city in a clean, simplified format
  • GET /weather/{city}/forecast — returns a 5-day forecast with one summary per day
  • In-memory caching (5-minute TTL) to reduce redundant OpenWeatherMap calls
  • Rate limiting (10 requests/minute per IP) to protect against abuse
  • Input validation (empty names, overly long names)
  • Proper error handling with meaningful HTTP status codes (404 for unknown cities, 502 for upstream failures, 429 for rate limits)
  • Structured logging
  • Automated test suite (pytest)
  • Interactive API documentation via Swagger UI
  • Dockerized for consistent, portable deployment

Tech Stack

  • FastAPI — web framework
  • httpx — async HTTP client for calling OpenWeatherMap
  • Pydantic — data validation and response schemas
  • cachetools — in-memory TTL caching
  • slowapi — rate limiting
  • pytest — testing framework
  • python-dotenv — environment variable management
  • Docker — containerization

Architecture

Client → FastAPI (rate limit check) → Cache lookup → OpenWeatherMap API (on cache miss) → Response reshaping → Client

Our service validates input, checks the cache, calls OpenWeatherMap only when needed, and transforms their raw response into a simplified schema — rather than exposing OpenWeatherMap's format directly.

Getting Started

Prerequisites

  • Python 3.10+ (tested with 3.14)
  • An OpenWeatherMap API key (sign up free)
  • Docker Desktop (optional, for containerized runs)

Installation (local)

  1. Clone the repository:
git clone <your-repo-url>
cd weather-api
  1. Create and activate a virtual environment:
python -m venv venv
venv\Scripts\activate      # Windows
source venv/bin/activate   # Mac/Linux
  1. Install dependencies:
pip install -r requirements.txt
  1. Set up environment variables:
cp .env.example .env

Then edit .env and add your OpenWeatherMap API key.

  1. Run the server:
uvicorn app.main:app --reload
  1. Open the interactive docs:
http://127.0.0.1:8000/docs

Installation (Docker)

  1. Build the image:
docker build -t weather-api .
  1. Run the container:
docker run -p 8000:8000 --env-file .env weather-api
  1. Visit http://127.0.0.1:8000/docs as above.

API Reference

GET /weather/{city}

Returns current weather for the given city.

Example request:

GET /weather/London

Example response:

{
  "city": "London",
  "country": "GB",
  "temperature_celsius": 15.2,
  "feels_like_celsius": 14.6,
  "humidity_percent": 60,
  "condition": "few clouds",
  "wind_speed_mps": 3.1
}

GET /weather/{city}/forecast

Returns a 5-day forecast (one entry per day, midday reading).

Example request:

GET /weather/London/forecast

Example response:

{
  "city": "London",
  "forecast": [
    {"date": "2026-08-17", "temperature_celsius": 16.4, "condition": "light rain", "humidity_percent": 72},
    {"date": "2026-08-18", "temperature_celsius": 18.1, "condition": "clear sky", "humidity_percent": 55}
  ]
}

Error responses:

Status Meaning
422 Invalid input (empty or overly long city name)
404 City not found
429 Too many requests (rate limit exceeded — 10/minute per IP)
502 OpenWeatherMap unavailable or misconfigured

Running Tests

python -m pytest

Project Structure

app/
├── main.py              # FastAPI app and routes
├── models/               # Pydantic response schemas
└── services/              # External API client logic (with caching)
tests/                    # Automated test suite
Dockerfile                # Container build instructions

Known Limitations

  • In-memory cache resets on server restart and doesn't share state across multiple instances (Redis would solve this)
  • Rate limiting is per-IP, so multiple users behind the same network share a limit
  • Free-tier hosting (Render) may have a cold-start delay after inactivity

Future Improvements

  • Multi-city comparison endpoint
  • Redis-based caching (for multi-instance deployments)
  • Authentication / API key system for consumers of this API

License

MIT

About

A FastAPI service wrapping the OpenWeatherMap API — with caching, rate limiting, testing, Docker, and live deployment.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages