generated from timoguin/repo-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Cleanup for first beta release (#4)
### Changed - APIClient class moved to `aws_data_tools.client.APIClient` - README clean-up - Bump version to 0.1.0-beta1 - Adds a CI config for Semantic Pull Requests
- Loading branch information
Showing
8 changed files
with
75 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
titleAndCommits: true | ||
scopes: ["cli", "organizations"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,60 +1,6 @@ | ||
from dataclasses import dataclass, field, InitVar | ||
from typing import Any, ClassVar, Dict, List, Union | ||
|
||
from boto3.session import Session | ||
from botocore.client import BaseClient | ||
from botocore.paginate import PageIterator, Paginator | ||
from humps import depascalize, pascalize | ||
|
||
|
||
__VERSION__ = "0.1.0-alpha4" | ||
|
||
|
||
_DEFAULT_PAGINATION_CONFIG = {"MaxItems": 500} | ||
__VERSION__ = "0.1.0-beta1" | ||
|
||
|
||
def get_version() -> str: | ||
"""Return the version of the package""" | ||
return __VERSION__ | ||
|
||
|
||
@dataclass | ||
class APIClient: | ||
""" | ||
Service client for interacting with named AWS API services. When initialized, it | ||
establishes a boto3 session and client for the specified service. Loads | ||
""" | ||
|
||
service: str | ||
client: BaseClient = field(default=None) | ||
session: Session = field(default_factory=Session) | ||
|
||
def api(self, func: str, **kwargs) -> Union[Dict[str, Any], List[Dict[str, Any]]]: | ||
""" | ||
Call a named API action by string. All arguments to the action should be passed | ||
as kwargs. The returned data has keys normalized to snake_case. Similarly, all | ||
kwargs can be passed in snake_case as well. | ||
If the API action is one that supports pagination, it is handled automaticaly. | ||
All paginated responses are fully aggregated and then returned. | ||
""" | ||
kwargs = pascalize(kwargs) | ||
paginate = self.client.can_paginate(func) | ||
if paginate: | ||
paginator = self.client.get_paginator(func) | ||
if kwargs.get("PaginationConfig") is None: | ||
kwargs.update(PaginationConfig=_DEFAULT_PAGINATION_CONFIG) | ||
page_iterator = paginator.paginate(**kwargs) | ||
responses = [] | ||
for page in page_iterator: | ||
page = depascalize(page) | ||
metakeys = ["next_token", "response_metadata"] | ||
key = [k for k in page.keys() if k not in metakeys][0] | ||
responses.extend(page.get(key)) | ||
return responses | ||
else: | ||
response = getattr(self.client, func)(**kwargs) | ||
return depascalize(response) | ||
|
||
def __post_init__(self): | ||
self.client = self.session.client(self.service) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
from dataclasses import dataclass, field, InitVar | ||
from typing import Any, ClassVar, Dict, List, Union | ||
|
||
from boto3.session import Session | ||
from botocore.client import BaseClient | ||
from botocore.paginate import PageIterator, Paginator | ||
from humps import depascalize, pascalize | ||
|
||
|
||
_DEFAULT_PAGINATION_CONFIG = {"MaxItems": 500} | ||
|
||
|
||
@dataclass | ||
class APIClient: | ||
""" | ||
Service client for interacting with named AWS API services. When initialized, it | ||
establishes a boto3 session and client for the specified service. Loads | ||
""" | ||
|
||
service: str | ||
client: BaseClient = field(default=None) | ||
session: Session = field(default_factory=Session) | ||
|
||
def api(self, func: str, **kwargs) -> Union[Dict[str, Any], List[Dict[str, Any]]]: | ||
""" | ||
Call a named API action by string. All arguments to the action should be passed | ||
as kwargs. The returned data has keys normalized to snake_case. Similarly, all | ||
kwargs can be passed in snake_case as well. | ||
If the API action is one that supports pagination, it is handled automaticaly. | ||
All paginated responses are fully aggregated and then returned. | ||
""" | ||
kwargs = pascalize(kwargs) | ||
paginate = self.client.can_paginate(func) | ||
if paginate: | ||
paginator = self.client.get_paginator(func) | ||
if kwargs.get("PaginationConfig") is None: | ||
kwargs.update(PaginationConfig=_DEFAULT_PAGINATION_CONFIG) | ||
page_iterator = paginator.paginate(**kwargs) | ||
responses = [] | ||
for page in page_iterator: | ||
page = depascalize(page) | ||
metakeys = ["next_token", "response_metadata"] | ||
key = [k for k in page.keys() if k not in metakeys][0] | ||
responses.extend(page.get(key)) | ||
return responses | ||
else: | ||
response = getattr(self.client, func)(**kwargs) | ||
return depascalize(response) | ||
|
||
def __post_init__(self): | ||
self.client = self.session.client(self.service) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[tool.poetry] | ||
name = "aws_data_tools" | ||
version = "0.1.0-alpha4" | ||
version = "0.1.0-beta1" | ||
description = "A set of Python libraries for querying and transforming data from AWS APIs" | ||
authors = ["Tim O'Guin <[email protected]>"] | ||
license = "MIT" | ||
|