This project provides a caching engine for the Star Wars API (SWAPI) to enhance its features for high school students working on school projects.
- REST API: Designed to serve the needs of high school students with additional features like search, sort, etc.
- Caching: Utilizes SQLite for caching SWAPI data, providing faster responses and reducing the load on the SWAPI server.
- Modular Design: Follows a modular structure with controllers, models, and utilities.
- Logging: Implements logging for better visibility into the application's behavior.
- Exception Handling: Handles exceptions gracefully to ensure the stability of the application.
- Unit Tests: Includes unit tests for the SWAPI controller.
- Cron Job: Sets up a cron job using APScheduler to periodically update the cached data.
- Python (>v3.9)
- Pip (Python package installer)
- SQLite (for local development)
-
Clone the repository.
git clone https://github.com/nbpatil0/starwars_api.git cd starwars_api
-
Create a virtual environment and activate it.
python -m venv venv
- on Windows
venv\Scripts\activate
- on Mac/Linux
source venv/bin/activate
-
Install the dependencies:
pip install -r requirements.txt
-
Create a
.env
file based on the provided example:FLASK_APP=app.main:app FLASK_ENV=development DATABASE_URI=sqlite:///cache.db LOG_FILE=app.log CRON_INTERVAL=3600
-
Source the
.env
file:source .env
-
Run the Flask application:
flask run
A Dockerfile is included to containerize the application for easy deployment and isolation. Follow the instructions below to run the Dockerized application locally:
- Docker installed on your machine.
-
Open a terminal and navigate to the project directory.
-
Build the Docker image:
docker build -t starwars-api-server .
-
Run the Docker container:
docker run -p 5000:5000 starwars-api-server
The application will be accessible at http://127.0.0.1:5000/.
/swapi/<resource>
: Retrieve data for a specific Star Wars resource with query parameters defined below./swapi/<resource>/<id>
: Retrieve details for a specific item within a resource.
page
: Page number for pagination (default: 1)items_per_page
: Number of items per page (default: 10)sort
: Attribute to sort byorder
: Sorting order ('asc' or 'desc')search
: Search term
Run the unit tests using the following command:
python -m unittest discover -s tests
The cron job is automatically set up to run every hour (adjustable in the .env file). To manually update the cache, run the following:
python cron.py
Logging has been implemented throughout the application to capture important events and errors. Loggers help in monitoring and troubleshooting the application.
To deploy the application on a remote server, follow the deployment procedures for your chosen hosting platform.
The Star Wars API Caching Server has been deployed on the Render platform. Please note the following considerations:
-
Service Limitations:
- Render services have a limitation where if the server doesn't receive any inbound hits for 15 minutes, the server shuts down to conserve resources.
- The server will start automatically upon receiving a new incoming request.
-
Initialization Time:
- As part of the deployment process, the server first updates the cache data from the Star Wars API.
- It may take a few minutes for the server to respond to the first API request, especially during the initial cache update.
-
Example:
- Retrieve data for a specific Star Wars resource with Search and Sort:
https://starwars-api-7nk0.onrender.com/swapi/starships?items_per_page=20&search=transport&sort=cargo_capacity
- Retrieve details for a specific item within a resource:
https://starwars-api-7nk0.onrender.com/swapi/starships/2