It is a small part of a customer loyalty app. It has 2️⃣ microservices accessible via REST API: 1) Generate Discount Coderuns 2) Fetch discount code for a user.
A development web server and two microservices
Python 3.10, Flask 2.2 (web framework), SQLAlchemy (ORM) and Marshmallow (serialization). In this project SQLite database is used that can be replaced with other database type.
Note: these instructions are for Mac OS
- Create a parent working folder
- Goto that folder and clone the repository. It will create a folder discount_api_codes
- Setup the Python 🐍 virtual environment (I have assumed that Python 3.x is already installed on your system)
-
- Install the libraries and setup the database
- goto discount_codes_api 📁
- Install the dependencies, (refer to requirement.txt 📄).
-
pip3 install Flask==2.2.2 pip3 install Flask-SQLAlchemy==2.5.1 pip3 install flask-marshmallow==0.14.0
- Create the database, Seed the database (refer to next section)
- Lets run the app 💻 and test the endpoints using Postman 🏤 or web browser 🌍
flask run
SQLite database loyalty.db is used by the app. During development/testing you can create the Database, Drop it and Seed it with initial data
Action | Command |
---|---|
Create Database | flask --app db_setup db_create |
Drop Database | flask --app db_setup db_drop |
Seed Database | flask --app db_setup db_seed |
Note: commands should be entered in discount_api_codes folder 📁 at command prompt
python3 app.py
ORflask run
The above command will run the development web server http://localhost:5000 where you can test the API endpoints
API endpoint | Description |
---|---|
http://localhost:5000/brands | Return the brands 🛍️ list defined in the DB |
http://localhost:5000/codes | Return all the discount codes 💸 for all the brands |
http://localhost:5000/codes?brand_id=2 | Return all the discount codes of brand_id=2 There are 3 brands defined in the DB having ids 1, 2 and 3 |
http://localhost:5000/generate_disc_codes?brand_id=1&count=20 | It will generate the 20 discount codes for the brand_id=2 You can pass other values 2 or 3 for brand_id. The count is optional, by default it will generate 🔟 codes or you can pass any positive number. This endpoint expects 2️⃣ parameters: brand_id and count (i.e. number of codes to generate.) |
http://localhost:5000/fetch_discount_code?brand_id=1&user_id=2 | It fetches a discount code 🎟️ for a user of a brand. This endpoint expects 2️⃣ parameters brand_id and user_id (there are two users defined in the DB with id 1 and 2 |
Happy Programming! 🌛 Fawad