This project provides an API that summarizes campaign data from an SQL database according to specified date ranges. The API is developed using the FastAPI framework and SQLAlchemy for database operations.
- Python 3.9+
- Docker (optional, if you want to run using Docker)
- MySQL database (or another SQL database, but MySQL is used in this project)
-
Clone the project:
git clone https://github.com/Backend-Codes/adin_python_task.git cd campaign-summary-api
-
Create and activate a virtual environment:
python3 -m venv venv source venv/bin/activate
-
Install the required Python packages:
pip install -r requirements.txt
If you want to use Docker, you can easily run your project with Docker:
-
Build the Docker image:
docker build -t adin_python_task .
-
Start the Docker container:
docker run -d -p 8000:8000 --name adin_python_task \ -e DATABASE_HOSTNAME="hostname" \ -e DATABASE_PORT="port" \ -e DATABASE_NAME="dbname" \ -e DATABASE_USERNAME="db username" \ -e DATABASE_PASSWORD="db password" \ adin_python_task
You need to define the database connection details in the .env
file:
```env
DATABASE_HOSTNAME=your_host_name
DATABASE_PORT=your_db_port
DATABASE_NAME=your_db_name
DATABASE_USERNAME=your_username
DATABASE_PASSWORD=your_password
```
To run the API, use the following command:
```bash
uvicorn app.main:app --reload
```
GET /campaigns/summary
: Returns campaign summary data for a specified date range and campaign ID.
campaign_id
(optional): The campaign ID. If not provided, summaries for all campaigns will be returned.start_date
(required): The start date. Format: YYYY-MM-DDend_date
(required): The end date. Format: YYYY-MM-DD
```bash
curl -X 'GET' \
'http://127.0.0.1:8000/campaigns/summary?start_date=2023-04-20&end_date=2023-04-30' \
-H 'accept: application/json'
```
To run the tests that come with the project:
-
Install pytest:
pip install pytest
-
Run the tests:
pytest
The tests are located in the tests directory and validate the API with sample requests.
If you would like to contribute, please create a pull request. Any contributions and feedback are welcome!