Пример #1
0
def to_cps_publish_message(source: PubSubMessage) -> PubsubMessage:
    out = PubsubMessage()
    try:
        out.ordering_key = source.key.decode("utf-8")
    except UnicodeError:
        raise InvalidArgument(
            "Received an unparseable message with a non-utf8 key.")
    if PUBSUB_LITE_EVENT_TIME in source.attributes:
        raise InvalidArgument(
            "Special timestamp attribute exists in wire message. Unable to parse message."
        )
    out.data = source.data
    for key, values in source.attributes.items():
        out.attributes[key] = _parse_attributes(values)
    if "event_time" in source:
        out.attributes[PUBSUB_LITE_EVENT_TIME] = encode_attribute_event_time(
            source.event_time)
    return out
def create_fake_message(
        message_data,
        ack_id="ACK_ID",
        message_id="MESSAGE_ID",
        timestamp_epoch_seconds=int(time.time())
) -> ReceivedMessage:
    publish_time_timestamp = Timestamp()
    publish_time_timestamp.seconds = timestamp_epoch_seconds

    pubsub_message = PubsubMessage()
    pubsub_message.data = message_data.encode("UTF-8")
    pubsub_message.message_id = message_id
    pubsub_message.publish_time = publish_time_timestamp
    pubsub_message.ordering_key = ""

    received_message = ReceivedMessage()
    received_message.ack_id = ack_id
    received_message.message = pubsub_message
    received_message.delivery_attempt = 0

    return received_message