Skip to content

adobhal95/weather-api

Repository files navigation

Weather API Documentation

Overview

This is a FastAPI-based application that retrieves weather data for a given location using the OpenWeatherMap API. The application includes caching with Redis to improve performance and rate limiting using SlowAPI to prevent abuse.

This is a solution to the roadmap.sh project weather api


Features

  • Fetch current weather data for a specified location.
  • Caching of weather data using Redis to reduce API calls.
  • Rate limiting to control the number of requests per user.

Requirements

  • Python 3.8+
  • Redis server
  • OpenWeatherMap API key

Installation

  1. Clone the repository:

    git clone https://github.com/adobhal95/weather-api.git
    cd weather-api
  2. Set up environment variables:

    • OPENWEATHER_API_KEY: Your OpenWeatherMap API key.
    • REDIS_URL: Redis server URL (e.g., redis://localhost:6379).
    • OPENWEATHER_API_WEATHER_URL: url for open weather api weather location for a city
    • OPENWEATHER_API_GEO_URL: url for open weather api geo code location
    • CacheTTL: int value containing for how long api cache will last in seconds

    Example .env file:

    OPENWEATHER_API_KEY=your_api_key
    REDIS_URL=redis://redis:6379
    
  3. Use Docker and docker compose:

    docker compose build --no-cache
    docker compose up # to start services
    docker compose stop # to stop services

Usage

  1. Access the API documentation at http://localhost:8000/docs.

API Endpoints

GET /weather/{city}/{country}

Retrieve weather data for a given location.

Path Parameters:

  • city (string, required): Name of the location (e.g., "London").
  • country (string, required): Name of the Country (e.g., "England").

Example Request:

curl -X GET "http://127.0.0.1:8000/weather/city=London/country=England"

Example Response:

{
  "coord": {
    "lon": -0.1257,
    "lat": 51.5072
  },
  "weather": [
    {
      "id": 802,
      "main": "Clouds",
      "description": "scattered clouds",
      "icon": "03d"
    }
  ],
  "base": "stations",
  "main": {
    "temp": 285.44,
    "feels_like": 284.29,
    "temp_min": 285.44,
    "temp_max": 285.44,
    "pressure": 1004,
    "humidity": 60,
    "sea_level": 1004,
    "grnd_level": 1000
  },
  "visibility": 10000,
  "wind": {
    "speed": 7.27,
    "deg": 219,
    "gust": 11.52
  },
  "rain": null,
  "snow": null,
  "clouds": {
    "all": 31
  },
  "dt": 1744801912,
  "sys": {
    "type": null,
    "id": null,
    "country": "GB",
    "sunrise": 1744779734,
    "sunset": 1744829873
  },
  "timezone": 3600,
  "id": 2643743,
  "name": "London",
  "cod": 200
}

Caching

  • Weather data is cached in Redis for a configurable duration (default: 1 minute).
  • Cached responses are served for repeated requests to the same location within the cache duration.

Rate Limiting

  • The application uses SlowAPI to enforce rate limits.
  • Default limit: 5 requests per minute per user.
  • Requests exceeding the limit will receive a 429 Too Many Requests response.

License

This project is licensed under the MIT License. See the LICENSE file for details.


Acknowledgments

About

Build a weather api using fastapi python

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published