Created
April 29, 2019 16:07
-
-
Save jg75/bbfec1ea1325e2749989eefe9e1e8a5c to your computer and use it in GitHub Desktop.
Handles responses from boto3 client methods that can or can't be paginated and yields the results.
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
"""Boto3 client response handler.""" | |
import boto3 | |
class ClientHandler: | |
"""Boto3 client response handler.""" | |
__slots__ = ["client", "method", "key", "paginator"] | |
def __init__(self, client, method, key): | |
"""Override.""" | |
self.client = client | |
self.method = method | |
self.key = key | |
if client.can_paginate(method): | |
self.paginator = self.client.get_paginator(self.method) | |
def generate_paginated(self, **arguments): | |
"""Generate values from the paginated response.""" | |
for response in self.paginator.paginate(**arguments): | |
for value in response[self.key]: | |
yield value | |
def generate_unpaginated(self, **arguments): | |
"""Generate values from the unpaginated response.""" | |
response = getattr(self.client, self.method)(**arguments) | |
for value in response[self.key]: | |
yield value | |
def generate(self, **arguments): | |
"""Get a generated response.""" | |
return self.generate_paginated(**arguments) if self.paginator \ | |
else self.generate_unpaginated(**arguments) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment