yandex-cloud / python-sdk

Yandex.Cloud Python SDK
MIT License
75 stars 24 forks source link

too many values to unpack (expected 2) #57

Closed den41apple closed 2 years ago

den41apple commented 2 years ago

Hi! I'm trying to download logs from the service: https://cloud.yandex.ru/docs/logging/api-ref/grpc/log_reading_service

My code: from yandex.cloud.logging.v1 import log_reading_service_pb2_grpc as reading_service_service from yandex.cloud.logging.v1 import log_reading_service_pb2 as reading_service_service_pb2 import grpc

def make_stub(endpoint='reader.logging.yandexcloud.net', iam_token=iam_token): with open('logging.pem', 'rb') as cert: ssl_creds = grpc.ssl_channel_credentials(cert.read()) call_creds = grpc.access_token_call_credentials(iam_token) chan_creds = grpc.composite_channel_credentials(ssl_creds, call_creds) channel = grpc.secure_channel(endpoint, chan_creds) return reading_service_service.LogReadingServiceStub(channel)

stub = make_stub()

request = reading_service_service_pb2.ReadRequest(criteria={'log_group_id': 'e23lhi4g6vfl7hiokmiq'}) stub.Read(request=request, metadata=(('authorization', f'Bearer {iam_token}')))

I get an internal error:

ValueError Traceback (most recent call last) Input In [21], in <cell line: 19>() 16 stub = make_stub() 18 request = reading_service_service_pb2.ReadRequest(criteria={'log_group_id': 'e23lhi4g6vfl7hiokmiq'}) ---> 19 stub.Read(request=request, metadata=(('authorization', f'Bearer {iam_token}')))

File ~/venv/lib/python3.9/site-packages/grpc/_channel.py:944, in _UnaryUnaryMultiCallable.call(self, request, timeout, metadata, credentials, wait_for_ready, compression) 937 def call(self, 938 request, 939 timeout=None, (...) 942 wait_for_ready=None, 943 compression=None): --> 944 state, call, = self._blocking(request, timeout, metadata, credentials, 945 wait_for_ready, compression) 946 return _end_unary_response_blocking(state, call, False, None)

File ~/venv/lib/python3.9/site-packages/grpc/_channel.py:926, in _UnaryUnaryMultiCallable._blocking(self, request, timeout, metadata, credentials, wait_for_ready, compression) 924 raise rendezvous # pylint: disable-msg=raising-bad-type 925 else: --> 926 call = self._channel.segregated_call( 927 cygrpc.PropagationConstants.GRPC_PROPAGATE_DEFAULTS, 928 self._method, None, _determine_deadline(deadline), metadata, 929 None if credentials is None else credentials._credentials, (( 930 operations, 931 None, 932 ),), self._context) 933 event = call.next_event() 934 _handle_event(event, state, self._response_deserializer)

File src/python/grpcio/grpc/_cython/_cygrpc/channel.pyx.pxi:496, in grpc._cython.cygrpc.Channel.segregated_call()

File src/python/grpcio/grpc/_cython/_cygrpc/channel.pyx.pxi:366, in grpc._cython.cygrpc._segregated_call()

File src/python/grpcio/grpc/_cython/_cygrpc/channel.pyx.pxi:360, in grpc._cython.cygrpc._segregated_call()

File src/python/grpcio/grpc/_cython/_cygrpc/channel.pyx.pxi:218, in grpc._cython.cygrpc._call()

File src/python/grpcio/grpc/_cython/_cygrpc/channel.pyx.pxi:246, in grpc._cython.cygrpc._call()

File src/python/grpcio/grpc/_cython/_cygrpc/channel.pyx.pxi:89, in grpc._cython.cygrpc._operate()

File src/python/grpcio/grpc/_cython/_cygrpc/tag.pyx.pxi:64, in grpc._cython.cygrpc._BatchOperationTag.prepare()

File src/python/grpcio/grpc/_cython/_cygrpc/operation.pyx.pxi:37, in grpc._cython.cygrpc.SendInitialMetadataOperation.c()

File src/python/grpcio/grpc/_cython/_cygrpc/metadata.pyx.pxi:41, in grpc._cython.cygrpc._store_c_metadata()

ValueError: too many values to unpack (expected 2)

Tell me, what am I doing wrong?

l0kix2 commented 2 years ago

According to docs metadata parameter should be sequence of two-tuples. This is an example.

When you write (('authorization', f'Bearer {iam_token}')) in python it is equivalent to ('authorization', f'Bearer {iam_token}') tuple, but you need a sequence of tuples (i.e. list of tuples or tuple of tuples).

So try

stub.Read(
    request=request, 
    metadata=(
        ('authorization', f'Bearer {iam_token}'),
    )
)

(note the trailing comma).

den41apple commented 2 years ago

Thanks a lot, it works!)