django-socio-grpc icon indicating copy to clipboard operation
django-socio-grpc copied to clipboard

Accept filters from request arguments and not only metadata

Open AMontagu opened this issue 2 years ago • 2 comments

In DSG metadata is what used to replace the query parameters systems. It is very flexible as it's not in the proto file.

The main inconvenients are:

  • The metadata are not binary serialized so passing a lot of data as filters may result in poor performance
  • They not exported in the proto so not documented by default.

To contourn that we want to look about best practices and implement it.

Think to update doc after.

AMontagu avatar Dec 18 '23 12:12 AMontagu

@AMontagu, I fully support this reature request.

Right now, I am using a "decorator" workaround, e.g.:

# filter_decorator.py
import functools
import json

import lara_django_samples_grpc.v1.lara_django_samples_pb2 as lara_django_samp_pb2

def dict_filter(func):
    @functools.wraps(func)
    def wrapper_filter(filter_dict : dict={}, **kwargs):
        metadata = (("filters", (json.dumps(filter_dict))),)
        return func.List(lara_django_samp_pb2.SampleListRequest(), metadata=metadata, **kwargs)
    return wrapper_filter


and then I can do something like:

from filter_decorator import dict_filter

channel = grpc.insecure_channel(f'{grpc_server_host}:{grpc_server_port}')
sample_client = lara_django_samp_pb2_grpc.SampleControllerStub(channel)
list_filter = dict_filter(sample_client)

filter_dict = { "name__contains": "term-to-search"}
res = list_filter(filter_dict=filter_dict)
print(res)

Which is more readable, but still has the problem of using metadata to send the filter.

markdoerr avatar Jan 07 '24 12:01 markdoerr

The first part of this issue is completed. The second part is to precisely detailled the filters and pagination fields names and types instead of using a struct. This improvement may happen in late 2024.

AMontagu avatar Feb 15 '24 11:02 AMontagu