def decode(self, envelope_bytes: bytes) -> "Envelope": """ Decode the envelope. :param envelope_bytes: the encoded envelope :return: the envelope """ envelope_pb = base_pb2.Envelope() envelope_pb.ParseFromString(envelope_bytes) to = envelope_pb.to # pylint: disable=no-member sender = envelope_pb.sender # pylint: disable=no-member raw_protocol_id = envelope_pb.protocol_id # pylint: disable=no-member protocol_id = PublicId.from_str(raw_protocol_id) message = envelope_pb.message # pylint: disable=no-member uri_raw = envelope_pb.uri # pylint: disable=no-member if uri_raw != "": # empty string means this field is not set in proto3 uri = URI(uri_raw=uri_raw) context = EnvelopeContext(uri=uri) envelope = Envelope( to=to, sender=sender, protocol_id=protocol_id, message=message, context=context, ) else: envelope = Envelope( to=to, sender=sender, protocol_id=protocol_id, message=message, ) return envelope
def encode(self, envelope: 'Envelope') -> bytes: """Encode the envelope.""" envelope_pb = base_pb2.Envelope() envelope_pb.to = envelope.to envelope_pb.sender = envelope.sender envelope_pb.protocol_id = envelope.protocol_id envelope_pb.message = envelope.message envelope_bytes = envelope_pb.SerializeToString() return envelope_bytes
def decode(self, envelope_bytes: bytes) -> 'Envelope': """Decode the envelope.""" envelope_pb = base_pb2.Envelope() envelope_pb.ParseFromString(envelope_bytes) to = envelope_pb.to sender = envelope_pb.sender protocol_id = envelope_pb.protocol_id message = envelope_pb.message envelope = Envelope(to=to, sender=sender, protocol_id=protocol_id, message=message) return envelope
def encode(self, envelope: "Envelope") -> bytes: """ Encode the envelope. :param envelope: the envelope to encode :return: the encoded envelope """ envelope_pb = base_pb2.Envelope() envelope_pb.to = envelope.to envelope_pb.sender = envelope.sender envelope_pb.protocol_id = str(envelope.protocol_id) envelope_pb.message = envelope.message_bytes if envelope.context is not None and envelope.context.uri_raw != "": envelope_pb.uri = envelope.context.uri_raw envelope_bytes = envelope_pb.SerializeToString() return envelope_bytes
def test_envelope_specification_id_translated(): """Test protocol id to protocol specification id translation and back.""" protocol_specification_id = PublicId("author", "specification", "0.1.0") envelope = Envelope( to="to", sender="sender", protocol_specification_id=protocol_specification_id, message=b"", ) assert envelope.protocol_specification_id == protocol_specification_id envelope_bytes = envelope.encode() envelope_pb = base_pb2.Envelope() envelope_pb.ParseFromString(envelope_bytes) new_envelope = Envelope.decode(envelope_bytes) assert new_envelope.protocol_specification_id == envelope.protocol_specification_id