def test_encode_custom_message(): """ Custom message encoding should include the standard URIMessage fields plus additionally specified fields """ custom_schema = CustomMessageSchema() codec = PubSubMessageCodec(custom_schema) assert_that( loads( codec.encode( enum_field=TestEnum.key, media_type=CustomMessageSchema.MEDIA_TYPE, opaque_data=dict(foo="bar"), uri="http://example.com", )), is_( equal_to({ "mediaType": "application/vnd.globality.pubsub._.created.resource", "opaqueData": { "foo": "bar", }, "uri": "http://example.com", "enumField": "key", })), )
def find(self, media_type): """ Create a codec or raise KeyError. If autoregistration is enabled, falls back to the URIMessageSchema. """ if media_type not in self._media_types: if self.auto_register and self.lifecycle_change.matches( media_type): # When using convention-based media types, we may need to auto-register self._media_types.add(media_type) else: raise KeyError( "Unregistered media type: {}".format(media_type)) try: # use a concrete schema class if any schema_cls = self._mappings[media_type] schema = schema_cls(strict=self.strict) except KeyError: # use convention otherwise if self.lifecycle_change.Deleted in media_type.split("."): schema = IdentityMessageSchema(media_type) else: schema = URIMessageSchema(media_type) return PubSubMessageCodec(schema)
def test_decode_identity_message_schema(): """ Message decoding should process standard fields. """ schema = IdentityMessageSchema(make_media_type("Foo")) codec = PubSubMessageCodec(schema) message = dumps({ "mediaType": "application/vnd.globality.pubsub.foo", "opaqueData": { "foo": "bar", }, "id": "1", }) assert_that(codec.decode(message), is_(equal_to({ "media_type": "application/vnd.globality.pubsub.foo", "opaque_data": dict(foo="bar"), "id": "1", })))
def test_encode_identity_message_schema(): """ Message encoding should include the standard fields. """ schema = IdentityMessageSchema(make_media_type("Foo", lifecycle_change=LifecycleChange.Deleted)) codec = PubSubMessageCodec(schema) assert_that( loads(codec.encode( opaque_data=dict(foo="bar"), id="1", )), is_(equal_to({ "mediaType": "application/vnd.globality.pubsub._.deleted.foo", "opaqueData": { "foo": "bar", }, "id": "1", })), )
def test_encode_uri_message_schema(): """ Message encoding should include the standard fields. """ schema = URIMessageSchema(make_media_type("Foo")) codec = PubSubMessageCodec(schema) assert_that( loads( codec.encode( opaque_data=dict(foo="bar"), uri="http://example.com", )), is_( equal_to({ "mediaType": "application/vnd.globality.pubsub._.created.foo", "opaqueData": { "foo": "bar", }, "uri": "http://example.com", })), )
def test_decode_uri_message_schema(): """ Message decoding should process standard URIMessage fields. """ schema = URIMessageSchema() codec = PubSubMessageCodec(schema) message = dumps({ "mediaType": "application/vnd.globality.pubsub.foo", "opaqueData": { "foo": "bar", }, "uri": "http://example.com", }) assert_that( codec.decode(message), is_( equal_to({ "media_type": "application/vnd.globality.pubsub.foo", "opaque_data": dict(foo="bar"), "uri": "http://example.com", })))
class CodecMediaTypeAndContentParser(MediaTypeAndContentParser): def __init__(self, graph): super(CodecMediaTypeAndContentParser, self).__init__(graph) self.media_type_codec = PubSubMessageCodec(MediaTypeSchema()) self.pubsub_message_schema_registry = graph.pubsub_message_schema_registry def parse_media_type_and_content(self, message): """ Decode the message once to extract its media type and then again with the correct codec. """ base_message = self.media_type_codec.decode(message) media_type = base_message["mediaType"] try: content = self.pubsub_message_schema_registry.find( media_type).decode(message) except KeyError: return media_type, None else: return media_type, content
def __getitem__(self, media_type): """ Create a codec or raise KeyError. """ try: schema_cls = self.mappings[media_type] if isinstance(schema_cls, string_types): media_type = schema_cls schema = URIMessageSchema(media_type) else: schema = schema_cls(strict=self.strict) except KeyError: if not self.auto_register: raise for lifecycle_change in LifecycleChange: if lifecycle_change.matches(media_type): schema = URIMessageSchema(media_type) break else: raise return PubSubMessageCodec(schema)
def __init__(self, graph): super(CodecMediaTypeAndContentParser, self).__init__(graph) self.media_type_codec = PubSubMessageCodec(MediaTypeSchema()) self.pubsub_message_schema_registry = graph.pubsub_message_schema_registry