authentik.api.parsers
1from typing import Any 2 3from django.conf import settings 4from msgspec import DecodeError 5from msgspec.json import Decoder 6from rest_framework.exceptions import ParseError 7from rest_framework.parsers import BaseParser 8 9_DECODER = Decoder() 10 11 12class MsgspecJSONParser(BaseParser): 13 media_type = "application/json" 14 15 def parse(self, stream, media_type=None, parser_context: dict[str, Any] | None = None): 16 parser_context = parser_context or {} 17 encoding = parser_context.get("encoding", settings.DEFAULT_CHARSET) 18 19 try: 20 data = stream.read().decode(encoding) 21 return _DECODER.decode(data) 22 except DecodeError as exc: 23 raise ParseError(f"JSON parse error - {exc}") from exc
class
MsgspecJSONParser(rest_framework.parsers.BaseParser):
13class MsgspecJSONParser(BaseParser): 14 media_type = "application/json" 15 16 def parse(self, stream, media_type=None, parser_context: dict[str, Any] | None = None): 17 parser_context = parser_context or {} 18 encoding = parser_context.get("encoding", settings.DEFAULT_CHARSET) 19 20 try: 21 data = stream.read().decode(encoding) 22 return _DECODER.decode(data) 23 except DecodeError as exc: 24 raise ParseError(f"JSON parse error - {exc}") from exc
All parsers should extend BaseParser, specifying a media_type
attribute, and overriding the .parse() method.
def
parse( self, stream, media_type=None, parser_context: dict[str, Any] | None = None):
16 def parse(self, stream, media_type=None, parser_context: dict[str, Any] | None = None): 17 parser_context = parser_context or {} 18 encoding = parser_context.get("encoding", settings.DEFAULT_CHARSET) 19 20 try: 21 data = stream.read().decode(encoding) 22 return _DECODER.decode(data) 23 except DecodeError as exc: 24 raise ParseError(f"JSON parse error - {exc}") from exc
Given a stream to read from, return the parsed representation.
Should return parsed data, or a DataAndFiles object consisting of the
parsed data and files.