Skip to content

About

A production-minded FastAPI service with clean architecture, caching abstraction, and Dockerized deployment

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

4 Commits

Folders and files

Repository files navigation

🌤️ Weather API (FastAPI) — Production-Ready Service

Python FastAPI Docker Status

A clean, production-minded FastAPI service that fetches current weather data for a given city using the Weatherstack API.


🚀 Features

  • 🌍 Fetch current weather by city
  • 🧱 Clean layered architecture
  • ⚡ FastAPI performance
  • 🧠 Smart response transformation
  • ♻️ In-memory caching with TTL
  • 🐳 Docker support
  • 🔐 Environment-based configuration
  • 📊 Logging for observability
  • 📑 Interactive API docs with Swagger UI

⚙️ Run Locally (Without Docker)

  1. Install dependencies:

    pip install -r requirements.txt
  2. Set environment variables:
    Create a .env file or export variables in your shell:

    WEATHER_API_URL=your_api_url_here
    WEATHER_API_KEY=your_api_key_here
    
  3. Start the server:

    uvicorn app.main:app --reload

👉 Open: http://127.0.0.1:8000/docs


🐳 Run with Docker

  1. Build the Docker image:

    docker build -t weather-api .
  2. Create a .env file in the project root with:

    WEATHER_API_URL=your_api_url_here
    WEATHER_API_KEY=your_api_key_here
    
  3. Run the container:

    docker run -p 8000:8000 --env-file .env weather-api

🌐 API Usage

GET /weather?city=Cairo

Example Response

{
  "location": {
    "name": "Cairo",
    "country": "Egypt",
    "localtime": "2026-04-23 17:01"
  },
  "weather": {
    "temperature": 28,
    "description": "Sunny",
    "feels_like": 26,
    "humidity": 19
  },
  "wind": {
    "speed": 13,
    "direction": "NNW"
  },
  "air_quality": {
    "pm2_5": 16.85,
    "pm10": 31.35
  }
}

API Documentation (Swagger UI)

Swagger Screenshot


🔑 Environment Variables

Create a .env file or set the following environment variables:

Key Description
WEATHER_API_URL Weatherstack API base URL
WEATHER_API_KEY Your Weatherstack API key

Note:
An example .env file is provided for your convenience.
However, in production you should never commit your real .env file to version control.
Make sure to add .env to your .gitignore to keep your secrets safe.


🧠 Design Decisions

  • Used layered architecture to separate concerns
  • Implemented service layer for business logic
  • Transformed external API response → clean internal format
  • Used environment variables for secure configuration
  • Added caching abstraction for scalability
  • Dockerized for consistent deployment

⚖️ Trade-offs

  • Used in-memory caching instead of Redis (for simplicity)
  • No authentication or DB (out of scope)
  • Focused on clarity over complexity

I intentionally kept the solution simple and production-ready, given the expected scope (~60–90 minutes).


🚀 Future Improvements

  • Redis caching (distributed)
  • Async HTTP client (httpx)
  • Retry mechanism
  • Rate limiting
  • Unit & integration testing
  • Structured logging

📦 Tech Stack

  • Python 3.11
  • FastAPI
  • Requests
  • Pydantic
  • Docker

⚠️ Error Handling

The API handles:

  • Invalid input (missing or incorrect city)
  • External API errors (Weatherstack responses)
  • Network failures (timeouts, connection issues)

Appropriate HTTP status codes are returned (400, 502).


⚡ Caching Strategy

A simple in-memory cache with TTL is implemented to:

  • Reduce external API calls
  • Improve response time

The caching logic is abstracted, allowing easy replacement with Redis in a production environment.


📄 API Response Contract

The API returns a structured and simplified response instead of exposing raw external API data.

This ensures:

  • Stability of the API contract
  • Decoupling from third-party changes

👩‍💻 Author

Esraa Raffik Mashaal
Senior Fullstack Engineer
LinkedIn
📧 esraa.mashaal96@gmail.com
📞 +201013589988


📌 Notes

This project demonstrates:

  • clean architecture
  • production thinking
  • scalable design approach

This implementation prioritizes clarity, maintainability, and production-readiness over unnecessary complexity.

About

A production-minded FastAPI service with clean architecture, caching abstraction, and Dockerized deployment

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages