Skip to content

Instantly share code, notes, and snippets.

@jg75
Created April 29, 2019 16:14
Show Gist options
  • Save jg75/29a8dd547cd6432e2c55d3c58d4c5387 to your computer and use it in GitHub Desktop.
Save jg75/29a8dd547cd6432e2c55d3c58d4c5387 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.
"""Boto3 client response handler."""
import boto3
class ClientHandler:
"""Boto3 client response handler."""
__slots__ = ["client", "method", "key"]
def __init__(self, client, method, key):
"""Override."""
self.client = client
self.method = method
self.key = key
def generate_paginated(self, **arguments):
"""Generate values from the paginated response."""
paginator = self.client.get_paginator(self.method)
for response in 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."""
if self.client.can_paginate(self.method):
return self.generate_paginated(**arguments)
else:
return self.generate_unpaginated(**arguments)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment