Skip to content

aghwotu/rick-morty-api-wrapper

Repository files navigation

Rick and Morty API Wrapper

A FastAPI-based wrapper for the Rick and Morty API with additional filtering capabilities and caching.

Features

  • Additional character filtering with combined filters
  • Redis caching for improved performance
  • Server-side pagination
  • Comprehensive error handling
  • Full test coverage
  • Containerized deployment to AWS Lambda

Tech Stack

  • FastAPI: Modern Python web framework
  • Redis: In-memory caching
  • aiohttp: Async HTTP client for API calls
  • pytest: Testing framework
  • Docker: Containerization
  • AWS Lambda: Serverless deployment
  • Amazon ECR: Container registry

Project Structure

rick-morty-backend/
├── src/
│   ├── main.py          # FastAPI application entry point
│   ├── api.py           # Rick and Morty API client
│   ├── cache.py         # Redis cache implementation
│   └── config.py        # Configuration settings
├── tests/
│   ├── test_api.py      # API client tests
│   ├── test_cache.py    # Cache layer tests
│   └── test_main.py     # Endpoint tests
├── deploy_ecr.sh        # AWS ECR deployment script
├── Dockerfile           # Container definition
├── lambda_function.py   # AWS Lambda handler
└── pytest.ini          # Pytest configuration

Getting Started

Prerequisites

  • Python 3.9+
  • Redis
  • Docker
  • AWS CLI configured with appropriate permissions

Installation

  1. Clone the repository
git clone https://github.com/aghwotu/rick-morty-backend.git
cd rick-morty-backend
  1. Create and activate virtual environment
# Create virtual environment
python3 -m venv venv

# Activate virtual environment
# On macOS/Linux:
source venv/bin/activate
# On Windows:
.\venv\Scripts\activate
  1. Install dependencies
pip install -r requirements.txt
  1. Copy environment template and update variables
cp .env.example .env
# Update .env with your configurations
  1. Set up Redis

Redis needs to be installed and running on your system:

# On macOS using Homebrew:
brew services start redis

# On Linux (Ubuntu/Debian):
sudo service redis-server start

# On Windows:
# Redis for Windows requires Windows Subsystem for Linux (WSL) or Docker
# Please refer to Redis documentation for Windows installation

Note: For detailed Redis installation instructions for your platform, visit the Redis documentation.

Local Development

Run the application locally:

uvicorn src.main:app --reload

Running Tests

pytest -v

Deployment

Prerequisites

  • AWS CLI installed and configured
  • Docker installed and running
  • Appropriate AWS permissions for ECR and Lambda

Deploy to AWS ECR

  1. Make the deployment script executable:
chmod +x deploy_ecr.sh
  1. Run the deployment script:
./deploy_ecr.sh

This will:

  • Create an ECR repository if it doesn't exist
  • Build the Docker image
  • Push the image to ECR

API Documentation

The API documentation is available at the following endpoints:

  • Swagger UI: /docs
  • OpenAPI Schema: /openapi.json

Example: If your API is deployed at https://api.example.com/prod, the Swagger UI would be available at https://api.example.com/prod/docs

Endpoints

  • GET /health: Health check
  • GET /characters: Get characters with filtering
  • GET /cache/stats: Get cache statistics
  • POST /cache/invalidate: Invalidate cache

Development Notes

  • Uses async/await for non-blocking operations
  • Implements caching with 1-hour expiration
  • Handles Redis connection failures gracefully
  • Complete test coverage with mocked dependencies
  • AWS Lambda deployment ready

License

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

About

A FastAPI-powered wrapper for the Rick and Morty API (rickandmortyapi.com) with enhanced filtering, caching, and serverless deployment. Features Redis caching, extended search capabilities, and AWS Lambda integration.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors