This project is a Weather API that fetches and returns weather data from a third-party API instead of relying on our own weather data. It helps me in understanding how to work with third-party APIs, caching mechanisms, and environment variables.
- Fetches real-time weather data from a third-party API (e.g., OpenWeather API).
- Implements in-memory caching using Redis to store recent requests and reduce API calls.
- Uses environment variables to securely store sensitive information like API keys.
- Implements proper error handling for invalid requests and third-party API failures.
- Supports rate limiting to prevent abuse.
- Includes a GUI for user-friendly interaction.
- Programming Language: Rust
- Third-party API: Open Weather API
- Caching: Redis
- HTTP Requests: Reqwest
- GUI: Egui
- Environment Variable Management: dotenv
- Rate Limiting: Custom implementation using Redis
- Install Redis and start service
- Obtain an API key from Open Weather (or any chosen weather API)
- Clone the repository:
git clone https://github.com/cenixeriadev/WeatherAPI.git cd WeatherAPI
- Install dependencies:
cargo build
- Set up environment variables:
Create a
.env
file and add the following:API_KEY=your_openweather_api_key REDIS_URL=your_redis_connection_string
- Start the application:
cargo run
- Endpoint:
GET /weather/:city/:country_code
- Description: Fetches weather data for a given city and country code(Ex: US).
- Response:
{ "city": "New York", "temperature": "15°C", "humidity": 60, "pressure": 1013, "wind_speed": 5.0, "description": "Clear sky" }
- The city and country code entered by the user is used as the cache key.
- Weather data is stored in Redis with a 12-hour expiration time to avoid stale data.
- Handles invalid city and country codes and returns appropriate error messages.
- If the third-party API is down, returns a suitable fallback response.
- To prevent API abuse, a rate limiter is implemented using Redis.
- Allows a maximum of 15 requests per minute per city-country pair.
- Anthony (GitHub: cenixeriadev)