gRPC not showing details

I’m trying to use the List request device profile using gRPC. My issue is while executing the function I’m only getting the total number of device profile but not the device profiles available

request = api.device_profile_pb2.ListDeviceProfilesRequest()
request.tenant_id = '52f14cd4-c6f1-4fbd-8f87-4025e1d49242'
response = device_profile_stub.List(request, metadata = authorization_metadata)

Output:
total_count: 1

Issue:
can we not get the total count with the list of all the device profiles?

@brocaar
can you please check this and suggest a solution

You need to submit the “limit” parameter.

Cheers,

1 Like

Thanks for the solution

There are use-cases where you want to get the total count, but not all the items. If you have thousands of devices for example, this might generate a lot of data if all you are interested in is just the total number of devices.

Note that gRPC / Protobuf defaults to the default type values in case these are not set. E.g. an integer defaults to 0, a string to "", a boolean to false etc… That is why the limit defaults to 0 if not set.

Thanks for the explanation @brocaar.
I have another query regarding the gRPC API.

For the create device API my code is

        request = api.device_pb2.CreateDeviceRequest()
        request.device.dev_eui = "aaaaaaaaaaaaaaae"
        request.device.name = "test_adddevice"
        request.device.application_id = "d7dceacd-5866-4814-ba8a-db791206dd07"
        request.device.device_profile_id = "88e2b170-9714-40ec-a0b7-c869b74d309b"
        request.device.is_disabled = True
        response = device_stub.Create(request, metadata = metadata)

The device gets successfully created on chirpstack UI but is there a way to determine that the response received was a success?
When the response is not successful the response I get is

<bound method _InactiveRpcError.details of <_InactiveRpcError of RPC that terminated with:
        status = StatusCode.INTERNAL
        details = "duplicate key value violates unique constraint "device_pkey""
        debug_error_string = "UNKNOWN:Error received from peer  {grpc_message:"duplicate key value violates unique constraint \"device_pkey\"", grpc_status:13, created_time:"2023-09-12T18:12:15.538084+05:30"}"

on successful response I get a blank

Is there a way to get the status code/success response that can be checked.?

This is more a generic gRPC question. gRPC uses several response codes:
https://grpc.github.io/grpc/core/md_doc_statuscodes.html

It depends on the gRPC implementation how these are exposed. I’m not sure which implementation you are using, but it might be that everything which is not an OK is exposed as an exception.

@brocaar
My script is as below

import grpc
from chirpstack_api import API
device_stub = api.device_pb2_grpc.DeviceServiceStub(channel)
metadata = [("authorization", authorization_token),]

request = api.device_pb2.CreateDeviceRequest()
request.device.dev_eui = "aaaaaaaaaaaaaaae"
request.device.name = "test_adddevice"
request.device.application_id = "d7dceacd-5866-4814-ba8a-db791206dd07"
request.device.device_profile_id = "88e2b170-9714-40ec-a0b7-c869b74d309b"
request.device.is_disabled = True

response = device_stub.Create(request, metadata = metadata)

I need to add another function if the response is OK.
Can you please tell how can I check and add a condition to do so

As I mentioned before, if response does not contain the status code, it might be that the gRPC implementation that you are using is using exceptions in case the status is anything other than OK. In such case you would use something like try / except to handle potential errors.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.