django_socio_grpc.decorators

Module Contents

Functions

_maintain_compat

Transform old arguments to the correct plugins

grpc_action

Easily register a grpc action into the registry to generate it into the proto file.

http_to_grpc

Allow to use Django decorators on grpc endpoint. As the working behavior will depend on the grpc support and/or DSG support of the feature it may not work as expected. If it’s not working as expected, first look at the documentation if the decorators is not listed as one of the unsupported decorator. If not, please open an issue and we will look if possible to support it.

vary_on_metadata

Same as https://docs.djangoproject.com/fr/5.0/topics/http/decorators/#django.views.decorators.vary.vary_on_headers but need to wrap the response in a GRPCInternalProxyResponse. A view decorator that adds the specified metadatas to the Vary metadata of the response. Usage:

cache_endpoint

A decorator for caching gRPC endpoints using Django’s cache_page functionality. This decorator adapts Django’s cache_page for use with gRPC endpoints. It performs the following steps:

cache_endpoint_with_deleter

This decorator does all the same as cache_endpoint but with the addition of a cache deleter. The cache deleter will delete the cache when a signal is triggered. This is useful when you want to delete the cache when a model is updated or deleted. Be warned that this can add little overhead at server start as it will listen to signals.

Data

logger

API

django_socio_grpc.decorators.logger

‘getLogger(…)’

django_socio_grpc.decorators._maintain_compat(use_request_list, use_response_list, use_generation_plugins)

Transform old arguments to the correct plugins

django_socio_grpc.decorators.grpc_action(request=None, response=None, request_name=None, response_name=None, request_stream=False, response_stream=False, use_request_list=False, use_response_list=False, message_name_constructor_class: type[django_socio_grpc.protobuf.message_name_constructor.MessageNameConstructor] = None, use_generation_plugins: list[django_socio_grpc.protobuf.generation_plugin.BaseGenerationPlugin] = None, override_default_generation_plugins: bool = False)

Easily register a grpc action into the registry to generate it into the proto file.

:param request: Format of the request. Can be a list of dict, a proto serilizer class or a string. See doc for more information. :param response: Format of the response. Can be a list of dict, a proto serilizer class or a string. See doc for more information. :param request_name: Name of the request. By default it’s generated according to service name and function name. :param response_name: Name of the response. By default it’s generated according to service name and function name. :param request_stream: If true the request message is marqued as stream. Default to false :param response_stream: If true the response message is marqued as stream. Default to false :param use_request_list: If true the response message is encapsuled in a list message. Default to false :param use_response_list: If true the response message is encapsuled in a list message. Default to false :param message_name_constructor_class: The class used to generate the name of the model. Inherit from MessageNameConstructor and chnage logic to have highly customizable name generation. :param use_generation_plugins: List of generation plugin to use to customize the message.

django_socio_grpc.decorators.http_to_grpc(decorator_to_wrap: collections.abc.Callable, request_setter: dict = None, response_setter: dict = None, support_async: bool = False) collections.abc.Callable

Allow to use Django decorators on grpc endpoint. As the working behavior will depend on the grpc support and/or DSG support of the feature it may not work as expected. If it’s not working as expected, first look at the documentation if the decorators is not listed as one of the unsupported decorator. If not, please open an issue and we will look if possible to support it.

:param decorator_to_wrap: The decorator to wrap. It can be a method or a function decorator. :param request_setter: A dict of attribute to set on the request object before calling the HTTP decorator. :param response_setter: A dict of attribute to set on the response object before returning it to the HTTP decorator. :param support_async: If the decorator to wrap is async or not. If not, it will be wrapped in a sync function. Refer to https://docs.djangoproject.com/en/5.0/topics/async/#decorators

django_socio_grpc.decorators.vary_on_metadata(*headers) collections.abc.Callable

Same as https://docs.djangoproject.com/fr/5.0/topics/http/decorators/#django.views.decorators.vary.vary_on_headers but need to wrap the response in a GRPCInternalProxyResponse. A view decorator that adds the specified metadatas to the Vary metadata of the response. Usage:

@vary_on_metadata('Cookie', 'Accept-language')
def index(request):
    ...

Note that the metadata names are not case-sensitive.

django_socio_grpc.decorators.cache_endpoint(*args, **kwargs)

A decorator for caching gRPC endpoints using Django’s cache_page functionality. This decorator adapts Django’s cache_page for use with gRPC endpoints. It performs the following steps:

  1. Converts cache_page to a method decorator, see: https://docs.djangoproject.com/en/5.0/topics/class-based-views/intro/#decorating-the-class

  2. Transforms the method decorator to be gRPC-compatible.

  3. Forces the request method to be GET.

Do not use this decorator on Create, Update, or Delete endpoints, as it will cache the response and return the same result to all users, potentially leading to data inconsistencies.

django_socio_grpc.decorators.cache_endpoint_with_deleter(timeout: int, key_prefix: str = '', senders: collections.abc.Iterable[django.db.models.Model] | None = None, cache: str = None, invalidator_signals: collections.abc.Iterable[collections.abc.Callable] = None)

This decorator does all the same as cache_endpoint but with the addition of a cache deleter. The cache deleter will delete the cache when a signal is triggered. This is useful when you want to delete the cache when a model is updated or deleted. Be warned that this can add little overhead at server start as it will listen to signals.

:param timeout: The timeout of the cache :param key_prefix: The key prefix of the cache :param cache: The cache alias to use. If None, it will use the default cache. It is named cache and not cache_alias to keep compatibility with Django cache_page decorator :param senders: The senders to listen to the signal :param invalidator_signals: The django signals to listen to delete the cache