From 050ddce0f0d1a0adc50b7d3d37377b204d9a9d19 Mon Sep 17 00:00:00 2001 From: hguercan Date: Thu, 8 Jul 2021 03:09:51 +0200 Subject: [PATCH] add region and endpoint for creating session also for localstack --- sqs_launcher/__init__.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/sqs_launcher/__init__.py b/sqs_launcher/__init__.py index 0496c35..802759f 100644 --- a/sqs_launcher/__init__.py +++ b/sqs_launcher/__init__.py @@ -27,7 +27,7 @@ class for running sqs message launcher class SqsLauncher(object): - def __init__(self, queue=None, queue_url=None, create_queue=False, visibility_timeout='600'): + def __init__(self, queue=None, queue_url=None, create_queue=False, visibility_timeout='600', **kwargs): """ :param queue: (str) name of queue to listen to :param queue_url: (str) url of queue to listen to @@ -37,6 +37,7 @@ def __init__(self, queue=None, queue_url=None, create_queue=False, visibility_ti Typically this should reflect the maximum amount of time your handler method will take to finish execution. See http://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-visibility-timeout.html for more information + :param kwargs: options for fine tuning. see below """ if not any(queue, queue_url): raise ValueError('Either `queue` or `queue_url` should be provided.') @@ -45,10 +46,15 @@ def __init__(self, queue=None, queue_url=None, create_queue=False, visibility_ti raise EnvironmentError('Environment variable `AWS_ACCOUNT_ID` not set and no role found.') # new session for each instantiation self._session = boto3.session.Session() - self._client = self._session.client('sqs') self._queue_name = queue self._queue_url = queue_url + self._endpoint_name = kwargs.get('endpoint_name', None) + self._region_name = kwargs.get('region_name', self._session.region_name) + if self._region_name and self._endpoint_name: + self._client = self._session.client('sqs', region_name=self._region_name, endpoint_url=self._endpoint_name) + else: + self._client = self._session.client('sqs') if not queue_url: queues = self._client.list_queues(QueueNamePrefix=self._queue_name) exists = False