boto3 response formatter
Project description
boto_formatter
What is boto _formatter?
boto_response_formatter is decorator that convert standard boto3 function response (returned as python list) in flattened JSON or tabular CSV formats for [list of supported services and functions] (https://github.com/awslabs/boto-formatter/blob/main/docs/supported_services.md). You can output the response to print, file or send flattened columnar JSON list to another function to continue your process.
boto_formatter simplifies the process and reduce the need of writing custom codebase potentially of 100s of line of code to 4-5 lines of code for simple use cases like generating list of lambda functions or list of cloudfront distriubtions with all the attributes that AWS Python SDK provides.
How it works?
You simply add decorator to your python function (The function which is returning list from boto3 function) and it will convert the boto3 return list to flatten JSON or comma separate values (CSV).
By adding decorator @boto_response_formatter to a function as example shown below in list_policies_fmt() function the response of the function will be converted to .csv . Generated csv response will also be saved in a file iam_list_polices_.csv in a output folder located in the same directory of invoking python script. You can also notice the difference in lines of code when using boto_formatter and without boto_formatter to achieve the same result of parsing and flattening the json response.
import boto3
from boto_formatter.core_formatter import boto_response_formatter
# With boto_formatter
@boto_response_formatter(service_name="iam", function_name="list_policies", format_type="csv", output_to="file" ,pagination="yes")
def list_policies_fmt():
client = boto3.client('iam')
paginator = client.get_paginator('list_policies')
result = []
for page in paginator.paginate():
result.append(page)
return result
# Without boto_formatter
def list_policies_without_boto_formatter():
client = boto3.client('iam')
paginator = client.get_paginator('list_policies')
result = []
for page in paginator.paginate():
for role in page['Policies']:
json_obj = {}
json_obj['PolicyName'] = role['PolicyName']
json_obj['PolicyId'] = role['PolicyId']
json_obj['Arn'] = role['Arn']
json_obj['Path']=str(role['Path'])
json_obj['DefaultVersionId'] = str(role['DefaultVersionId'])
json_obj['AttachmentCount'] = str(role['AttachmentCount'])
json_obj['PermissionsBoundaryUsageCount'] = str(role['PermissionsBoundaryUsageCount'])
json_obj['IsAttachable'] = str(role['IsAttachable'])
if "Description" in role.keys():
json_obj['Description'] = str(role['Description'])
json_obj['CreateDate'] = str(role['CreateDate'])
json_obj['UpdateDate'] = str(role['UpdateDate'])
if "Tags" in role.keys():
json_obj['Tags_Key'] = role['Tags']['Key']
json_obj['Tags_Value'] = role['Tags']['Value']
result.append(json_obj)
return result
list_policies_fmt()
list_policies_without_boto_formatter()
Setup
For Installation setup click here
Usage
Please click on each function to see the usage/example
Click on service to see the usage
Service | Functions |
---|---|
General usage | General usage |
accessanalyzer |
|
apigateway |
|
budgets |
|
cloudfront |
|
cloudtrail |
|
cloudwatch |
|
codecommit |
|
dynamodb |
|
ec2 |
|
ecs |
|
efs |
|
eks |
|
elasticache |
|
elbv2 |
|
emr-serverless |
|
emr |
|
iam |
|
kms |
|
lambda |
|
organizations |
|
rds |
|
redshift-serverless |
|
redshift |
|
route53 |
|
route53domains |
|
s3 |
|
sns |
|
sqs |
|
License
This library is licensed under the MIT-0 License. See the LICENSE file.
Considerations
- When the format_type is selected as "csv" ;boto_formatter will skip the columns which contains "," in value.
- Majority of the cases Library returns formatted response of all the attributes that Python SDK provides. However, it doesn't assure 100 % coverage of all the attributes that Python SDK provides.
- Library is not designed for latency-based requirements. So, if you have high latency requirements, please evaluate library in lower enviornments (dev,QA) before using in high latency-based environment.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for boto_formatter-1.0.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 00517b7a768a82aa348b3789ca7dab78a35f610b5d1fad6a3a2242bcc4eec47e |
|
MD5 | 165df32f0e762755250a07f3256ca7a5 |
|
BLAKE2b-256 | 693b6911a5d1bf9ce5c4581a49e80cf61cb0aa571b573c74fe1a0f7fb957adea |