A straightforward RESTful API built with Python and Flask that allows users to manage a collection of drinks. This project demonstrates basic CRUD (Create, Read, Update, Delete) operations and serves as a foundational example of building a web API.
- Python: The core programming language.
- Flask: A lightweight web framework for Python.
- Flask-SQLAlchemy: An extension for Flask that adds support for SQLAlchemy, an Object Relational Mapper (ORM), to simplify database interactions.
- SQLite: A C-language library that implements a small, fast, self-contained, high-reliability, full-featured, SQL database engine.
To get a local copy up and running, follow these simple steps.
- Python 3.x installed on your system.
pipfor installing Python packages.
-
Clone the repository (or download the source code):
git clone [YOUR_REPOSITORY_URL] cd [YOUR_PROJECT_DIRECTORY] -
Create and activate a virtual environment:
- On macOS and Linux:
python3 -m venv venv source venv/bin/activate - On Windows:
python -m venv venv .\venv\Scripts\activate
- On macOS and Linux:
-
Install the required packages:
pip install Flask Flask-SQLAlchemy
-
Initialize the database: From your terminal, start a Python interpreter and run the following commands to create the
data.dbfile and thedrinktable.from application import db db.create_all() exit()
-
Run the Flask application:
flask run
Your API should now be running at
http://127.0.0.1:5000.
The following are the available endpoints for this API.
- URL:
/drinks - Method:
GET - Description: Retrieves a list of all drinks in the database.
- Success Response:
- Code: 200 OK
- Content:
{ "drinks": [ "Water", "Coffee" ] }
- URL:
/drinks/<id> - Method:
GET - Description: Retrieves a specific drink by its ID.
- Success Response:
- Code: 200 OK
- Content:
{ "name": "Water", "description": "Clear and refreshing" }
- Error Response:
- Code: 404 Not Found
- URL:
/drinks - Method:
POST - Description: Adds a new drink to the database.
- Request Body:
{ "name": "Tea", "description": "A warm, comforting beverage" } - Success Response:
- Code: 200 OK
- Content:
{ "id": 3 }
- URL:
/drinks/<id> - Method:
DELETE - Description: Deletes a specific drink by its ID.
- Success Response:
- Code: 204 No Content