Skip to main content

A code generation library that converts AWS boto3 responses into a Python module with fully hierarchical Python data classes

Project description

Boto Model Python

A code generation library that converts AWS boto3 responses into a Python module with fully hierarchical data classes representing the response structure.

The package provides a single gateway function. This function takes the path to a boto3 response JSON syntax and a desired output path, generating Python data classes and enums based on the response structure.

This utility is designed for Python-boto3 developers who want quick access to boto3 response attributes and the full benefits of Pydantic BaseModel and Python enums, without dealing with the complexity of deep dictionary hierarchies.

How to use

Clarification

boto3 package is expected to be installed in the target environment where generated code is created. There is no installation of boto3 package during installation of boto-model-py package.

Pre-steps

  1. To install this package, you can use pip, the Python package installer. Run the following command in your terminal:
pip install boto-model-py

Usage

The main transformation script relies on responses syntax from boto3 documentation website.

  1. Choose from boto3 website the API you want to use and copy its response syntax dictionary syntax into local path
  2. Run cli command to transform local copy of the response syntax from boto3 into base model classes using bmpy command The command has 2 main parameters:
    1. file_path: path to local response syntax file
    2. output_path: path to folder where output module is created - the response module location
    3. with_metadata: flag to indicate whether to include the response metadata in the base class or not
bmpy <file_path> <output_path>

Example

  1. Writing the following code section to get list of all buckets in my account:
import boto3
service = "s3"
client = boto3.client(service, region_name="us-east-2")
buckets = client.list_buckets()
  1. Go to the boto3 list_buckets API request docs: bot3 API list_buckets
  2. Copy response syntax into a local file called input
touch list_buckets_input

intput file:

{
    'Buckets': [
        {
            'Name': 'string',
            'CreationDate': datetime(2015, 1, 1)
        },
    ],
    'Owner': {
        'DisplayName': 'string',
        'ID': 'string'
    }
}
  1. Running bmpy command and generate response module in the desired location (suppose I have response folder in my project)
bmpy list_buckets_input response
  1. The response module is under generated under the output_path folder, with the with a class called ListBucketsResponse. You can import it in the project, and load the response into the base class. Then using all pydantic base class feature is easy.

Expected output from GitHubRepo

import boto3

from response.list_buckets_response import ListBucketsResponse

service = "s3"
client = boto3.client(service, region_name="us-east-2")
buckets = client.list_buckets()

buckets_object = ListBucketsResponse(**buckets)
for bucket in buckets_object.Buckets:
    print(bucket.Name)
    print(bucket.CreationDate)

print(buckets_object.model_dump_json(indent=3))

Flags

  • --with_metadata - By default, loading the response dictionary into the boto-model-py object does not include the response_metadata attribute. With using this flag on running the transformation script, this attribute is included on loading the response into the boto-model-py object.

Response folder

Under response folder you can see a batch of responses output from several example from boto3 responses tests. You can take your desired module class into your repo or generate the response class by using the boto-model-py package.

Developer setups

In order to contribute to the repository

Prerequisites

  • python3.9 or above

Git clone

git clone https://github.com/OriPoria/boto-model-py.git

Installation of venv

cd boto-model-py
python3.9 -m venv venv
source ./venv/bin/activate

Install requirements.txt

pip install -r requirements.txt

License

This project is licensed under the MIT License - see the LICENSE file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

boto_model_py-0.1.0.tar.gz (13.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

boto_model_py-0.1.0-py3-none-any.whl (15.6 kB view details)

Uploaded Python 3

File details

Details for the file boto_model_py-0.1.0.tar.gz.

File metadata

  • Download URL: boto_model_py-0.1.0.tar.gz
  • Upload date:
  • Size: 13.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.9.18

File hashes

Hashes for boto_model_py-0.1.0.tar.gz
Algorithm Hash digest
SHA256 080eceff27be6a16bb18399fa3f7f6e84da145a13ae8080699322bde6a2004f5
MD5 9f0636a87729b1b01c05643507eab055
BLAKE2b-256 e27f32308e5dd5a9cf002c0d98cd14fda1a9b3cb352a10a6e735abcdbf4a6c88

See more details on using hashes here.

File details

Details for the file boto_model_py-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: boto_model_py-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 15.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.9.18

File hashes

Hashes for boto_model_py-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 67cbca32b5c4f96a4c911f31f4dbd9a70f9506d4e5013f8258b598d48e0867ef
MD5 d9db3e275f4bba88916f61f8da5fb609
BLAKE2b-256 45ede2481ec1933647721f894b74e82047e9a85986be9bf9ba08dead7a8412b6

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page