This project implements a RESTful API for managing data using SQL. The project is built with Flask, a lightweight Python web framework, and interacts with a SQL database to perform CRUD (Create, Read, Update, Delete) operations. It features robust error handling and data validation to ensure data integrity.
- CRUD Operations: Perform Create, Read, Update, and Delete operations on the SQL database.
- SQL Database Integration: Efficiently manages data using SQL databases.
- Error Handling: Provides detailed error responses for invalid operations.
- Validation: Ensures the validity and integrity of data input and output.
- Scalability: The API structure allows easy expansion with additional endpoints.
- Python: The core language for the project.
- Flask: Micro web framework used to build the API.
- SQLAlchemy: SQL toolkit and Object-Relational Mapping (ORM) library for database operations.
- SQLite: The database management system used for local development (can be replaced with other SQL databases like PostgreSQL or MySQL).
To run the project locally, follow these steps:
-
Clone the repository:
git clone https://github.com/burakpekisik/me_api_sql.git
-
Navigate to the project directory:
cd me_api_sql -
Create a virtual environment:
python3 -m venv venv
-
Activate the virtual environment:
-
On Windows:
venv\Scripts\activate
-
On macOS/Linux:
source venv/bin/activate
-
-
Install dependencies:
pip install -r requirements.txt
-
Create a
.envfile: In the root directory, create a.envfile and configure your database and other necessary environment variables:FLASK_APP=app.py FLASK_ENV=development DATABASE_URL=sqlite:///your_database.db
-
Initialize the database: Run the following command to set up the database:
flask db init flask db migrate flask db upgrade
-
Run the application:
flask run
The server will start at
http://localhost:5000.
All configuration options, such as the database URL and Flask settings, are managed via the .env file. For example:
FLASK_APP=app.py
FLASK_ENV=development
DATABASE_URL=sqlite:///me_api_sql.db- Endpoint:
/create - Method: POST
- Description: Create a new entry in the database.
- Request Body:
{ "field1": "value1", "field2": "value2" } - Response:
{ "message": "Data created successfully" }
- Endpoint:
/read/<id> - Method: GET
- Description: Retrieve data by ID.
- Response:
{ "id": 1, "field1": "value1", "field2": "value2" }
- Endpoint:
/update/<id> - Method: PUT
- Description: Update data by ID.
- Request Body:
{ "field1": "newValue1", "field2": "newValue2" } - Response:
{ "message": "Data updated successfully" }
- Endpoint:
/delete/<id> - Method: DELETE
- Description: Delete data by ID.
- Response:
{ "message": "Data deleted successfully" }
This API provides standardized error messages and appropriate HTTP status codes for various failure scenarios, including:
- 400 Bad Request: The request was malformed or contained invalid data.
- 404 Not Found: The requested resource was not found in the database.
- 500 Internal Server Error: The server encountered an unexpected condition.
You can test the API using tools such as Postman or cURL. Here is an example of how to make a request using cURL:
curl -X POST http://localhost:5000/api/create \
-H "Content-Type: application/json" \
-d '{
"field1": "value1",
"field2": "value2"
}'This project is licensed under the MIT License. See the LICENSE file for more details.