Skip to content

m1guel17/python_RESTful_API

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Python RESTful API

This API allows dynamic querying, insertion, and updating of records across multiple database tables based on the request payload. By specifying the table name and relevant data in the payload, users can interact with specific tables using a single endpoint.

Requirements

  • Python 3.12
  • Flask
  • Flask-SQLAlchemy

Installation

  1. Clone the repository:

    git clone https://github.com/m1guel17/python_RESTful_API.git
    cd python_RESTful_API
  2. Install the required Python packages:

    pip install -r requirements.txt
  3. Set up your database configuration in the config.py file.

    class Config:
       SECRET_KEY = os.environ.get('SECRET_KEY') or "S3CR37"
       SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_URL', 'sqlite:///DATABASE.db')
       SQLALCHEMY_TRACK_MODIFICATIONS = False
  4. Run the application:

    python3.12 run.py

    The API will be available at http://127.0.0.1:80/

Endpoints

GET

Request Payload Examples:

For this example we'll request the instance(s) from a specific Table

No filters field

With filters api will return all instances that match filter

{
  "table": "ClientModel"
}

// Api will return all instances from Table 

  
{
    "table": "ClientModel",
    "filters": {
        "email": "johndoe@example.com",
        "lastName": "Doe"
    }
}

Response Payload Examples:

All instances from Table

All instances that match filter

[
    {
        "cellphone": "1234567890",
        "createdAt": "Sun, 10 Nov 2024 01:07:40 GMT",
        "createdBy": "admin",
        "email": "johndoe@example.com",
        "firstName": "John",
        "id": 1,
        "lastName": "Doe",
        "modifiedBy": "admin",
        "modifiedOn": "Sun, 10 Nov 2024 01:07:40 GMT"
    },
    {
        "cellphone": "15236585585",
        "createdAt": "Sun, 10 Nov 2024 01:09:05 GMT",
        "createdBy": "admin",
        "email": "somebody@example.com",
        "firstName": "Sam",
        "id": 2,
        "lastName": "Somebody",
        "modifiedBy": "admin",
        "modifiedOn": "Sun, 10 Nov 2024 01:09:05 GMT"
    },
    ...
]
[
    {
        "cellphone": "1234567890",
        "createdAt": "Sun, 10 Nov 2024 01:07:40 GMT",
        "createdBy": "admin",
        "email": "johndoe@example.com",
        "firstName": "John",
        "id": 1,
        "lastName": "Doe",
        "modifiedBy": "admin",
        "modifiedOn": "Sun, 10 Nov 2024 01:07:40 GMT"
    }
]










POST

Insert Payload Example:

For this example we'll Insert data into a specified table

{
    "table": "ClientModel",
    "data": {
        "firstName": "John",
        "lastName": "Doe",
        "email": "johndoe@example.com",
        "cellphone": "15236585585",
        "createdBy": "admin",
        "modifiedBy": "admin"
    }
}

Response Payload Example:

{
    "id": 1,
    "message": "Record inserted successfully"
}
// This will return a 201 Status Code

In case of sending duplicate values to unique values column the following error will prompt:

Error Handling

Each endpoint will return an error message with a status code for invalid input, missing records, or issues with database operations.

  1. 400 Bad Request: For issues like invalid table names, missing fields, or invalid data in the request payload.
  2. 409 Conflict: The request could not be processed because of conflict in the request.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published