Skip to content

software-venus/weather-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Weather API Service using FastAPI

A simple asynchronous weather API service built with FastAPI. It fetches weather data from an external API, caches results, stores weather data in AWS S3, and logs events in AWS DynamoDB.

Features

  • Fetch current weather data for a given city.
  • Cache weather data to avoid redundant external API calls.
  • Store weather data in AWS S3 as JSON files.
  • Log events (city, timestamp, S3 URL) in AWS DynamoDB.
  • Fully asynchronous implementation with FastAPI and boto3.

Requirements

  • Python 3.13
  • Docker and Docker Compose installed
  • AWS account with access to S3 and DynamoDB
  • API key for OpenWeatherMap (or a similar external API)

Setup Instructions

Local Setup

  1. Clone the Repository:

    git clone <repository-url>
    cd weather-api-service
  2. Create a Virtual Environment:

    python -m venv venv
    source venv/bin/activate  # Linux/Mac
    venv\Scripts\activate     # Windows
  3. Install Dependencies:

    pip install -r requirements.txt
  4. Set Up Environment Variables:
    Create a .env file in the root directory with the following content:

    API_KEY=your_openweather_api_key
    AWS_ACCESS_KEY_ID=your_aws_access_key
    AWS_SECRET_ACCESS_KEY=your_aws_secret_key
    AWS_REGION=your_aws_region
    S3_BUCKET_NAME=your_s3_bucket_name
    LOCAL_STORAGE_PATH=./data
    
  5. Run the Application:

    uvicorn app.main:app --reload
  6. Access the API:
    Open your browser or use an HTTP client like Postman to test the endpoint:

    http://localhost:8000/weather?city=London
    

Using Docker

  1. Build the Docker Image:

    docker build -t weather-api-service .
  2. Run the Container:

    docker run -d -p 8000:8000 --env-file .env weather-api-service
  3. Test the API:
    Access the endpoint:

    http://localhost:8000/weather?city=London
    

Using Docker Compose

  1. Create a docker-compose.yml File:
    Ensure the docker-compose.yml file is in the root directory:

    version: '3.8'
    
    services:
      weather-api:
        build: .
        ports:
          - "8000:8000"
        env_file: .env
        volumes:
          - .:/app
  2. Run the Service:

    docker-compose up -d
  3. Test the API:
    Access the endpoint:

    http://localhost:8000/weather?city=London
    
  4. Stop the Service:

    docker-compose down

Testing the API

Endpoint

  • GET /weather
    Query Parameters:
    • city (required): Name of the city to fetch weather data for.

Sample Request

http://localhost:8000/weather?city=New York

Troubleshooting

  • Missing AWS Credentials: Ensure your .env file is correctly populated with AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, and AWS_REGION.

  • S3/DynamoDB Errors: Verify the S3 bucket and DynamoDB table exist. You can create the DynamoDB table using the provided scripts/create_table.py.

  • Docker Issues: Ensure Docker and Docker Compose are installed and running:

    docker --version
    docker-compose --version
  • Dependency Issues: Reinstall dependencies:

    pip install --force-reinstall -r requirements.txt

Project Structure

weather-api-service/
├── app/
│   ├── __init__.py
│   ├── main.py
│   ├── routes/
│   │   ├── __init__.py
│   │   ├── weather.py
│   ├── services/
│   │   ├── __init__.py
│   │   ├── weather_service.py
│   │   ├── storage_service.py
│   │   ├── db_service.py
│   ├── utils/
│   │   ├── __init__.py
│   │   ├── cache.py
│   │   ├── config.py
├── scripts/
│   ├── create_table.py
├── .env
├── requirements.txt
├── Dockerfile
├── docker-compose.yml
├── README.md

License

This project is licensed under the MIT License.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published