django_socio_grpc.request_transformer.socio_internal_request

Module Contents

Classes

InternalHttpRequest

Class mocking django.http.HttpRequest to make some django behavior like middleware, filtering, authentication, … still work.

RequestMeta

Class allowing specific automatic transformation/matching behavior between HTTP headers format expected by django and gRPC metadata format

Data

logger

API

django_socio_grpc.request_transformer.socio_internal_request.logger

‘getLogger(…)’

class django_socio_grpc.request_transformer.socio_internal_request.InternalHttpRequest(grpc_context: django_socio_grpc.request_transformer.grpc_internal_proxy.GRPCInternalProxyContext, grpc_request: google.protobuf.message.Message, grpc_action: str, service_class_name: str)

Bases: django.http.request.HttpRequest

Class mocking django.http.HttpRequest to make some django behavior like middleware, filtering, authentication, … still work.

Initialization

grpc_context is used to get all the headers and other informations grpc_request is only used for filter and pagination from the request if setted by FILTER_BEHAVIOR or PAGINATION_BEHAVIOR settings. grpc_action is used to populate InternalHttpRequest.method

HEADERS_KEY

‘headers’

FILTERS_KEY

‘filters’

PAGINATION_KEY

‘pagination’

FILTERS_KEY_IN_REQUEST

‘_filters’

PAGINATION_KEY_IN_REQUEST

‘_pagination’

METHOD_MAP

None

headers()
get_from_metadata(metadata_key: str) dict[str, str | bytes]

Allow to:

  • Customise the metadata key used for pagination, filter and headers

  • override default metadata with some passed in HEADERS metadata key to have custom advanced behavior

parse_specific_key_from_metadata(metadata_key: str) dict[str, str | bytes]

Allow to Customise the metadata key used for pagination, filter and headers

get_from_request_struct(grpc_request: google.protobuf.message.Message, struct_field_name: str) dict
convert_metadata_to_dict(invocation_metadata: tuple) dict[str, str | bytes]
get_query_params(grpc_request: google.protobuf.message.Message) dict[str, str]

Method that transform specific metadata and/or request fields (depending on FILTER_BEHAVIOR and PAGINATION_BEHAVIOR settings) into a dict as if it was some query params passed in simple HTTP/1 calls

grpc_action_to_http_method_name(grpc_action: str) str

Allow to match known grpc action to method type. In the futur we may need/want to use a specific decorator that inject the correct mapping into the grpc context and parse it

Example:

  • List -> GET

  • Update -> PUT

class django_socio_grpc.request_transformer.socio_internal_request.RequestMeta(data)

Bases: django.utils.datastructures.CaseInsensitiveMapping

Class allowing specific automatic transformation/matching behavior between HTTP headers format expected by django and gRPC metadata format

Initialization

HTTP_PREFIX

None

__getitem__(key)

As HTTP headers are prefixed by HTTP_ by proxy server or CGI, Django store and retrieve headers with HTTP_ prefix As there is no same rule/restriction in gRPC, we need to check if the key is in the dict without HTTP_ prefix if not existing with

__setitem__(key, value)

See: https://github.com/django/django/blob/main/django/utils/datastructures.py#L305