|
56 | 56 | ) |
57 | 57 | from litestar.types.callable_types import AnyCallable, OperationIDCreator |
58 | 58 |
|
59 | | -__all__ = ("delete", "get", "head", "patch", "post", "put", "route") |
| 59 | +__all__ = ("delete", "get", "head", "patch", "post", "put", "query", "route") |
60 | 60 |
|
61 | 61 |
|
62 | 62 | def route( |
@@ -1086,6 +1086,177 @@ def decorator(fn: AnyCallable) -> HTTPRouteHandler: |
1086 | 1086 | return decorator |
1087 | 1087 |
|
1088 | 1088 |
|
| 1089 | +def query( |
| 1090 | + path: str | None | Sequence[str] = None, |
| 1091 | + *, |
| 1092 | + after_request: AfterRequestHookHandler | None = None, |
| 1093 | + after_response: AfterResponseHookHandler | None = None, |
| 1094 | + background: BackgroundTask | BackgroundTasks | None = None, |
| 1095 | + before_request: BeforeRequestHookHandler | None = None, |
| 1096 | + cache: bool | int | type[CACHE_FOREVER] = False, |
| 1097 | + cache_control: CacheControlHeader | None = None, |
| 1098 | + cache_key_builder: CacheKeyBuilder | None = None, |
| 1099 | + dependencies: Dependencies | None = None, |
| 1100 | + dto: type[AbstractDTO] | None | EmptyType = Empty, |
| 1101 | + etag: ETag | None = None, |
| 1102 | + exception_handlers: ExceptionHandlersMap | None = None, |
| 1103 | + guards: Sequence[Guard] | None = None, |
| 1104 | + media_type: MediaType | str | None = None, |
| 1105 | + middleware: Sequence[Middleware] | None = None, |
| 1106 | + name: str | None = None, |
| 1107 | + opt: Mapping[str, Any] | None = None, |
| 1108 | + request_class: type[Request] | None = None, |
| 1109 | + request_max_body_size: int | None | EmptyType = Empty, |
| 1110 | + response_class: type[Response] | None = None, |
| 1111 | + response_cookies: ResponseCookies | None = None, |
| 1112 | + response_headers: ResponseHeaders | None = None, |
| 1113 | + return_dto: type[AbstractDTO] | None | EmptyType = Empty, |
| 1114 | + signature_namespace: Mapping[str, Any] | None = None, |
| 1115 | + status_code: int | None = None, |
| 1116 | + sync_to_thread: bool | None = None, |
| 1117 | + # OpenAPI related attributes |
| 1118 | + content_encoding: str | None = None, |
| 1119 | + content_media_type: str | None = None, |
| 1120 | + deprecated: bool = False, |
| 1121 | + description: str | None = None, |
| 1122 | + include_in_schema: bool | EmptyType = False, |
| 1123 | + operation_class: type[Operation] = Operation, |
| 1124 | + operation_id: str | OperationIDCreator | None = None, |
| 1125 | + raises: Sequence[type[HTTPException]] | None = None, |
| 1126 | + response_description: str | None = None, |
| 1127 | + responses: Mapping[int, ResponseSpec] | None = None, |
| 1128 | + security: Sequence[SecurityRequirement] | None = None, |
| 1129 | + summary: str | None = None, |
| 1130 | + tags: Sequence[str] | None = None, |
| 1131 | + type_decoders: TypeDecodersSequence | None = None, |
| 1132 | + type_encoders: TypeEncodersMap | None = None, |
| 1133 | + handler_class: type[HTTPRouteHandler] = HTTPRouteHandler, |
| 1134 | + **kwargs: Any, |
| 1135 | +) -> Callable[[AnyCallable], HTTPRouteHandler]: |
| 1136 | + """Create an :class:`HTTPRouteHandler` with a ``QUERY`` method. |
| 1137 | +
|
| 1138 | + Args: |
| 1139 | + path: A path fragment for the route handler function or a sequence of path fragments. |
| 1140 | + If not given defaults to ``/`` |
| 1141 | + after_request: A sync or async function executed before a :class:`Request <.connection.Request>` is passed |
| 1142 | + to any route handler. If this function returns a value, the request will not reach the route handler, |
| 1143 | + and instead this value will be used. |
| 1144 | + after_response: A sync or async function called after the response has been awaited. It receives the |
| 1145 | + :class:`Request <.connection.Request>` object and should not return any values. |
| 1146 | + background: A :class:`BackgroundTask <.background_tasks.BackgroundTask>` instance or |
| 1147 | + :class:`BackgroundTasks <.background_tasks.BackgroundTasks>` to execute after the response is finished. |
| 1148 | + Defaults to ``None``. |
| 1149 | + before_request: A sync or async function called immediately before calling the route handler. Receives |
| 1150 | + the :class:`.connection.Request` instance and any non-``None`` return value is used for the response, |
| 1151 | + bypassing the route handler. |
| 1152 | + cache: Enables response caching if configured on the application level. Valid values are ``True`` or a number |
| 1153 | + of seconds (e.g. ``120``) to cache the response. |
| 1154 | + cache_control: A ``cache-control`` header of type |
| 1155 | + :class:`CacheControlHeader <.datastructures.CacheControlHeader>` that will be added to the response. |
| 1156 | + cache_key_builder: A :class:`cache-key builder function <.types.CacheKeyBuilder>`. Allows for customization |
| 1157 | + of the cache key if caching is configured on the application level. |
| 1158 | + dependencies: A string keyed mapping of dependency :class:`Provider <.di.Provide>` instances. |
| 1159 | + dto: :class:`AbstractDTO <.dto.base_dto.AbstractDTO>` to use for (de)serializing and |
| 1160 | + validation of request data. |
| 1161 | + etag: An ``etag`` header of type :class:`ETag <.datastructures.ETag>` that will be added to the response. |
| 1162 | + exception_handlers: A mapping of status codes and/or exception types to handler functions. |
| 1163 | + guards: A sequence of :class:`Guard <.types.Guard>` callables. |
| 1164 | + media_type: A member of the :class:`MediaType <.enums.MediaType>` enum or a string with a |
| 1165 | + valid IANA Media-Type. |
| 1166 | + middleware: A sequence of :class:`Middleware <.types.Middleware>`. |
| 1167 | + name: A string identifying the route handler. |
| 1168 | + opt: A string keyed mapping of arbitrary values that can be accessed in :class:`Guards <.types.Guard>` or |
| 1169 | + wherever you have access to :class:`Request <.connection.Request>` or :class:`ASGI Scope <.types.Scope>`. |
| 1170 | + request_class: A custom subclass of :class:`Request <.connection.Request>` to be used as route handler's |
| 1171 | + default request. |
| 1172 | + request_max_body_size: Maximum allowed size of the request body in bytes. If this size is exceeded, |
| 1173 | + a '413 - Request Entity Too Large' error response is returned. |
| 1174 | + response_class: A custom subclass of :class:`Response <.response.Response>` to be used as route handler's |
| 1175 | + default response. |
| 1176 | + response_cookies: A sequence of :class:`Cookie <.datastructures.Cookie>` instances. |
| 1177 | + response_headers: A string keyed mapping of :class:`ResponseHeader <.datastructures.ResponseHeader>` |
| 1178 | + instances. |
| 1179 | + responses: A mapping of additional status codes and a description of their expected content. |
| 1180 | + This information will be included in the OpenAPI schema |
| 1181 | + return_dto: :class:`AbstractDTO <.dto.base_dto.AbstractDTO>` to use for serializing |
| 1182 | + outbound response data. |
| 1183 | + signature_namespace: A mapping of names to types for use in forward reference resolution during signature modelling. |
| 1184 | + status_code: An http status code for the response. Defaults to ``200`` for mixed method or ``GET``, ``PUT`` and |
| 1185 | + ``PATCH``, ``201`` for ``POST`` and ``204`` for ``DELETE``. |
| 1186 | + sync_to_thread: A boolean dictating whether the handler function will be executed in a worker thread or the |
| 1187 | + main event loop. This has an effect only for sync handler functions. See using sync handler functions. |
| 1188 | + content_encoding: A string describing the encoding of the content, e.g. ``base64``. |
| 1189 | + content_media_type: A string designating the media-type of the content, e.g. ``image/png``. |
| 1190 | + deprecated: A boolean dictating whether this route should be marked as deprecated in the OpenAPI schema. |
| 1191 | + description: Text used for the route's schema description section. |
| 1192 | + include_in_schema: A boolean flag dictating whether the route handler should be documented in the OpenAPI schema. |
| 1193 | + operation_class: :class:`Operation <.openapi.spec.operation.Operation>` to be used with the route's OpenAPI schema. |
| 1194 | + operation_id: Either a string or a callable returning a string. An identifier used for the route's schema operationId. |
| 1195 | + raises: A list of exception classes extending from litestar.HttpException that is used for the OpenAPI documentation. |
| 1196 | + This list should describe all exceptions raised within the route handler's function/method. The Litestar |
| 1197 | + ValidationException will be added automatically for the schema if any validation is involved. |
| 1198 | + response_description: Text used for the route's response schema description section. |
| 1199 | + security: A sequence of dictionaries that contain information about which security scheme can be used on the endpoint. |
| 1200 | + summary: Text used for the route's schema summary section. |
| 1201 | + tags: A sequence of string tags that will be appended to the OpenAPI schema. |
| 1202 | + type_decoders: A sequence of tuples, each composed of a predicate testing for type identity and a msgspec |
| 1203 | + hook for deserialization. |
| 1204 | + type_encoders: A mapping of types to callables that transform them into types supported for serialization. |
| 1205 | + handler_class: Route handler class instantiated by the decorator |
| 1206 | + **kwargs: Any additional kwarg - will be set in the opt dictionary. |
| 1207 | + """ |
| 1208 | + |
| 1209 | + def decorator(fn: AnyCallable) -> HTTPRouteHandler: |
| 1210 | + return handler_class( |
| 1211 | + fn=fn, |
| 1212 | + after_request=after_request, |
| 1213 | + after_response=after_response, |
| 1214 | + background=background, |
| 1215 | + before_request=before_request, |
| 1216 | + cache=cache, |
| 1217 | + cache_control=cache_control, |
| 1218 | + cache_key_builder=cache_key_builder, |
| 1219 | + content_encoding=content_encoding, |
| 1220 | + content_media_type=content_media_type, |
| 1221 | + dependencies=dependencies, |
| 1222 | + deprecated=deprecated, |
| 1223 | + description=description, |
| 1224 | + dto=dto, |
| 1225 | + etag=etag, |
| 1226 | + exception_handlers=exception_handlers, |
| 1227 | + guards=guards, |
| 1228 | + http_method=HttpMethod.QUERY, |
| 1229 | + include_in_schema=include_in_schema, |
| 1230 | + media_type=media_type, |
| 1231 | + middleware=middleware, |
| 1232 | + name=name, |
| 1233 | + operation_class=operation_class, |
| 1234 | + operation_id=operation_id, |
| 1235 | + opt=opt, |
| 1236 | + path=path, |
| 1237 | + raises=raises, |
| 1238 | + request_class=request_class, |
| 1239 | + request_max_body_size=request_max_body_size, |
| 1240 | + response_class=response_class, |
| 1241 | + response_cookies=response_cookies, |
| 1242 | + response_description=response_description, |
| 1243 | + response_headers=response_headers, |
| 1244 | + responses=responses, |
| 1245 | + return_dto=return_dto, |
| 1246 | + security=security, |
| 1247 | + signature_namespace=signature_namespace, |
| 1248 | + status_code=status_code, |
| 1249 | + summary=summary, |
| 1250 | + sync_to_thread=sync_to_thread, |
| 1251 | + tags=tags, |
| 1252 | + type_decoders=type_decoders, |
| 1253 | + type_encoders=type_encoders, |
| 1254 | + **kwargs, |
| 1255 | + ) |
| 1256 | + |
| 1257 | + return decorator |
| 1258 | + |
| 1259 | + |
1089 | 1260 | def delete( |
1090 | 1261 | path: str | None | Sequence[str] = None, |
1091 | 1262 | *, |
|
0 commit comments