# Path: microcosm_pubsub/constants.py
# TTL_KEY = "X-Request-Ttl"
# 
# URI_KEY = "uri"
# 
# Path: microcosm_pubsub/message.py
# class SQSMessage:
#     """
#     SQS message wrapper.
# 
#     """
#     def __init__(self,
#                  consumer,
#                  content,
#                  media_type,
#                  message_id,
#                  receipt_handle,
#                  topic_arn=None,
#                  approximate_receive_count=None,
#                  handler=None):
#         self.consumer = consumer
#         self.content = content
#         self.media_type = media_type
#         self.message_id = message_id
#         self.receipt_handle = receipt_handle
#         self.topic_arn = topic_arn
#         self.approximate_receive_count = approximate_receive_count
#         self.handler = handler
# 
#     def ack(self):
#         """
#         Acknowledge this message was processed successfully.
# 
#         """
#         self.consumer.ack(self)
# 
#     def nack(self, visibility_timeout_seconds=None):
#         """
#         Acknowledge this message was NOT processed successfully.
# 
#         """
#         self.consumer.nack(self, visibility_timeout_seconds)
# 
#     @property
#     def opaque_data(self):
#         if not self.content:
#             return dict()
#         return self.content.get("opaque_data", {})
# 
#     @property
#     def ttl(self):
#         if TTL_KEY not in self.opaque_data:
#             return None
#         return int(self.opaque_data[TTL_KEY])
# 
#     @property
#     def uri(self):
#         if not self.content:
#             return None
#         return self.content.get("uri")


from typing import Dict
from microcosm.api import defaults, typed
from microcosm.config.types import boolean
from microcosm_logging.decorators import logger
from microcosm_pubsub.constants import TTL_KEY, URI_KEY
from microcosm_pubsub.message import SQSMessage
"""
Message context.

"""




@defaults(
    enable_ttl=typed(boolean, default_value=True),
    initial_ttl=typed(int, default_value=32),
)
@logger
class SQSMessageContext:
    """
    Factory for per-message contexts.

    """
    def __init__(self, graph):
        self.enable_ttl = graph.config.sqs_message_context.enable_ttl
        self.initial_ttl = graph.config.sqs_message_context.initial_ttl

    def __call__(self, context: SQSMessage, **kwargs) -> Dict[str, str]:
        """
        Create a new context from a message.

        """
        return self.from_sqs_message(context, **kwargs)


# Path: microcosm_pubsub/constants.py
# TTL_KEY = "X-Request-Ttl"
# 
# URI_KEY = "uri"
# 
# Path: microcosm_pubsub/message.py
# class SQSMessage:
#     """
#     SQS message wrapper.
# 
#     """
#     def __init__(self,
#                  consumer,
#                  content,
#                  media_type,
#                  message_id,
#                  receipt_handle,
#                  topic_arn=None,
#                  approximate_receive_count=None,
#                  handler=None):
#         self.consumer = consumer
#         self.content = content
#         self.media_type = media_type
#         self.message_id = message_id
#         self.receipt_handle = receipt_handle
#         self.topic_arn = topic_arn
#         self.approximate_receive_count = approximate_receive_count
#         self.handler = handler
# 
#     def ack(self):
#         """
#         Acknowledge this message was processed successfully.
# 
#         """
#         self.consumer.ack(self)
# 
#     def nack(self, visibility_timeout_seconds=None):
#         """
#         Acknowledge this message was NOT processed successfully.
# 
#         """
#         self.consumer.nack(self, visibility_timeout_seconds)
# 
#     @property
#     def opaque_data(self):
#         if not self.content:
#             return dict()
#         return self.content.get("opaque_data", {})
# 
#     @property
#     def ttl(self):
#         if TTL_KEY not in self.opaque_data:
#             return None
#         return int(self.opaque_data[TTL_KEY])
# 
#     @property
#     def uri(self):
#         if not self.content:
#             return None
#         return self.content.get("uri")


# Path: microcosm_pubsub/context.py
from typing import Dict
from microcosm.api import defaults, typed
from microcosm.config.types import boolean
from microcosm_logging.decorators import logger
from microcosm_pubsub.constants import TTL_KEY, URI_KEY
from microcosm_pubsub.message import SQSMessage

"""
Message context.

"""




@defaults(
    enable_ttl=typed(boolean, default_value=True),
    initial_ttl=typed(int, default_value=32),
)
@logger
class SQSMessageContext:
    """
    Factory for per-message contexts.

    """
    def __init__(self, graph):
        self.enable_ttl = graph.config.sqs_message_context.enable_ttl
        self.initial_ttl = graph.config.sqs_message_context.initial_ttl

    def __call__(self, context: SQSMessage, **kwargs) -> Dict[str, str]:
        """
        Create a new context from a message.

        """
        return self.from_sqs_message(context, **kwargs)


    def from_sqs_message(self, message: SQSMessage, **kwargs):