Esempio n. 1
0
def test_encode_required_field():
    # Required fields must be set prior to encoding.
    message = test_pblite_pb2.TestRequiredMessage()
    with pytest.raises(ValueError):
        pblite.encode(message)
    message.test_required_int = 0
    assert pblite.encode(message) == [0]
Esempio n. 2
0
def test_encode_required_field():
    # Required fields must be set prior to encoding.
    message = test_pblite_pb2.TestRequiredMessage()
    with pytest.raises(ValueError):
        pblite.encode(message)
    message.test_required_int = 0
    assert pblite.encode(message) == [0]
Esempio n. 3
0
    def _pb_request(self, endpoint, request_pb, response_pb):
        """Send a Protocol Buffer formatted chat API request.

        Args:
            endpoint (str): The chat API endpoint to use.
            request_pb: The request body as a Protocol Buffer message.
            response_pb: The response body as a Protocol Buffer message.

        Raises:
            NetworkError: If the request fails.
        """
        logger.debug('Sending Protocol Buffer request %s:\n%s', endpoint,
                     request_pb)
        res = yield from self._base_request(
            'https://clients6.google.com/chat/v1/{}'.format(endpoint),
            'application/json+protobuf',  # The request body is pblite.
            'protojson',  # The response should be pblite.
            json.dumps(pblite.encode(request_pb))
        )
        pblite.decode(response_pb, javascript.loads(res.body.decode()),
                      ignore_first_item=True)
        logger.debug('Received Protocol Buffer response:\n%s', response_pb)
        status = response_pb.response_header.status
        if status != hangouts_pb2.RESPONSE_STATUS_OK:
            description = response_pb.response_header.error_description
            raise exceptions.NetworkError(
                'Request failed with status {}: \'{}\''
                .format(status, description)
            )
Esempio n. 4
0
def test_encode():
    message = test_pblite_pb2.TestMessage(
        test_int=1,
        test_repeated_int=[3, 4],
        test_string='foo',
        test_repeated_string=['bar', 'baz'],
        test_enum=test_pblite_pb2.TestMessage.TEST_1,
        test_repeated_enum=[test_pblite_pb2.TestMessage.TEST_2,
                            test_pblite_pb2.TestMessage.TEST_3],
        test_embedded_message=test_pblite_pb2.TestMessage.EmbeddedMessage(
            test_embedded_int=1,
        ),
        test_repeated_embedded_message=[
            test_pblite_pb2.TestMessage.EmbeddedMessage(
                test_embedded_int=2,
            ),
            test_pblite_pb2.TestMessage.EmbeddedMessage(
                test_embedded_int=3,
            ),
        ],
        test_bytes=b'\x00',
        test_repeated_bytes=[b'\x00\x01', b'\x00\x01\x02'],
    )
    assert pblite.encode(message) == [
        1,
        [3, 4],
        'foo',
        ['bar', 'baz'],
        1,
        [2, 3],
        [1],
        [[2], [3]],
        'AA==',
        ['AAE=', 'AAEC'],
    ]
Esempio n. 5
0
    def _pb_request(self, endpoint, request_pb, response_pb):
        """Send a Protocol Buffer formatted chat API request.

        Args:
            endpoint (str): The chat API endpoint to use.
            request_pb: The request body as a Protocol Buffer message.
            response_pb: The response body as a Protocol Buffer message.

        Raises:
            NetworkError: If the request fails.
        """
        logger.debug('Sending Protocol Buffer request %s:\n%s', endpoint,
                     request_pb)
        res = yield from self._base_request(
            'https://clients6.google.com/chat/v1/{}'.format(endpoint),
            'application/json+protobuf',  # The request body is pblite.
            'protojson',  # The response should be pblite.
            json.dumps(pblite.encode(request_pb))
        )
        pblite.decode(response_pb, javascript.loads(res.body.decode()),
                      ignore_first_item=True)
        logger.debug('Received Protocol Buffer response:\n%s', response_pb)
        status = response_pb.response_header.status
        if status != hangouts_pb2.RESPONSE_STATUS_OK:
            description = response_pb.response_header.error_description
            raise exceptions.NetworkError(
                'Request failed with status {}: \'{}\''
                .format(status, description)
            )
Esempio n. 6
0
def test_encode():
    message = test_pblite_pb2.TestMessage(
        test_int=1,
        test_repeated_int=[3, 4],
        test_string='foo',
        test_repeated_string=['bar', 'baz'],
        test_enum=test_pblite_pb2.TestMessage.TEST_1,
        test_repeated_enum=[
            test_pblite_pb2.TestMessage.TEST_2,
            test_pblite_pb2.TestMessage.TEST_3
        ],
        test_embedded_message=test_pblite_pb2.TestMessage.EmbeddedMessage(
            test_embedded_int=1, ),
        test_repeated_embedded_message=[
            test_pblite_pb2.TestMessage.EmbeddedMessage(test_embedded_int=2, ),
            test_pblite_pb2.TestMessage.EmbeddedMessage(test_embedded_int=3, ),
        ],
        test_bytes=b'\x00',
        test_repeated_bytes=[b'\x00\x01', b'\x00\x01\x02'],
    )
    assert pblite.encode(message) == [
        1,
        [3, 4],
        'foo',
        ['bar', 'baz'],
        1,
        [2, 3],
        [1],
        [[2], [3]],
        'AA==',
        ['AAE=', 'AAEC'],
    ]
Esempio n. 7
0
 def serialize(self):
     """Serialize the segment to pblite."""
     segment = hangouts_pb2.Segment(
         type=self.type_,
         text=self.text,
         formatting=hangouts_pb2.Formatting(
             bold=self.is_bold,
             italic=self.is_italic,
             strikethrough=self.is_strikethrough,
             underline=self.is_underline,
         ),
     )
     if self.link_target is not None:
         segment.link_data.link_target = self.link_target
     return pblite.encode(segment)
Esempio n. 8
0
 def serialize(self):
     """Serialize the segment to pblite."""
     segment = hangouts_pb2.Segment(
         type=self.type_,
         text=self.text,
         formatting=hangouts_pb2.Formatting(
             bold=self.is_bold,
             italic=self.is_italic,
             strikethrough=self.is_strikethrough,
             underline=self.is_underline,
         ),
     )
     if self.link_target is not None:
         segment.link_data.link_target = self.link_target
     return pblite.encode(segment)
Esempio n. 9
0
def test_encode_serialize_default_value():
    # Field is always serialized when it is set, even when set to the default
    # value.
    message = test_pblite_pb2.TestMessage()
    message.test_int = 0
    assert pblite.encode(message) == [0]
Esempio n. 10
0
def test_encode_no_fields():
    message = test_pblite_pb2.TestMessage()
    assert pblite.encode(message) == []
Esempio n. 11
0
def test_encode_serialize_default_value():
    # Field is always serialized when it is set, even when set to the default
    # value.
    message = test_pblite_pb2.TestMessage()
    message.test_int = 0
    assert pblite.encode(message) == [0]
Esempio n. 12
0
def test_encode_no_fields():
    message = test_pblite_pb2.TestMessage()
    assert pblite.encode(message) == []