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.
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.
- To install this package, you can use
pip, the Python package installer. Run the following command in your terminal:
pip install boto-model-pyThe main transformation script relies on responses syntax from boto3 documentation website.
- Link: boto3 API services
- Choose from boto3 website the API you want to use and copy its
response syntaxdictionary syntax into local path - Run cli command to transform local copy of the
response syntaxfrom boto3 into base model classes usingbmpycommand The command has 2 main parameters:- file_path: path to local
response syntaxfile - output_path: path to folder where output module is created - the response module location
- with_metadata: flag to indicate whether to include the response metadata in the base class or not
- file_path: path to local
bmpy <file_path> <output_path>- 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()- Go to the boto3
list_bucketsAPI request docs: bot3 API list_buckets - Copy response syntax into a local file called
input
touch list_buckets_inputintput file:
{
'Buckets': [
{
'Name': 'string',
'CreationDate': datetime(2015, 1, 1)
},
],
'Owner': {
'DisplayName': 'string',
'ID': 'string'
}
}
- Running
bmpycommand and generate response module in the desired location (suppose I haveresponsefolder in my project)
bmpy list_buckets_input response- The response module is under generated under the
output_pathfolder, with the with a class calledListBucketsResponse. 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))- --with_metadata - By default, loading the response dictionary into the boto-model-py object does not include the
response_metadataattribute. With using this flag on running the transformation script, this attribute is included on loading the response into the boto-model-py object.
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.
In order to contribute to the repository
- python3.9 or above
git clone https://github.com/OriPoria/boto-model-py.gitcd boto-model-py
python3.9 -m venv venv
source ./venv/bin/activatepip install -r requirements.txtThis project is licensed under the MIT License - see the LICENSE file for details.