def throws(exception_cls: Type[Exception], handler_cls: Optional[Type['ExceptionHandler']] = None): """Decorator to use on methods.""" if handler_cls is not None: handler = handler_cls() else: handler = None return annotate(ExceptionAnnotation(exception_cls, handler), unique=True)
def register_global_exception( exception_class: Type[Exception]) -> Type[Exception]: assert issubclass( exception_class, Exception ), f'Class "{exception_class}" must be a subclass of Exception' annotation = GlobalExceptionAnnotation(exception_cls=exception_class) annotation_decorator = annotate(annotation, unique=True) annotation_decorator(exception_class) return exception_class
def route( url_path: str, http_method: Optional[str] = None, produces: Optional[Tuple[MediaType]] = None, consumes: Optional[Tuple[MediaType]] = None, ): route_annotation = RouteAnnotation(url_path, http_method, produces, consumes) return annotate(route_annotation, single=True)
def wrapper(exception_class): assert issubclass( exception_class, Exception ), f'Class "{exception_class}" must be a subclass of Exception' annotation = ProblemAnnotation(handling_info=ProblemHandlingInfo( status=int(status), type=type, title=title, detail=detail, ), ) annotation_decorator = annotate(annotation, unique=True) class_ = annotation_decorator(exception_class) return class_
def simple_annotation(string: str): return annotate(SimpleAnnotation(string))
def limits(*, default: Optional[int], maximum: Optional[int], redirect_to_default: bool = False): limits_ = Limits(default=default, maximum=maximum, redirect_to_default=redirect_to_default) annotation = LimitsAnnotation(limits_) return annotate(annotation, single=True)
def no_authentication(controller_class): return annotate(NotNeedAuthentication(), single=True)(controller_class)
def input_serializer(serializer_class: Type[Serializer], argument_name: str, **serializer_kwargs): input_serializer_ = InputSerializer(serializer_class, serializer_kwargs, argument_name) return annotate(input_serializer_, single=True)
def wrapper(method): method = annotate(annotation)(method) output_processor = DRFOutputProcessor(serializer_class, serializer_kwargs) method = register_output_processor(method, output_processor) return method