|
1 | 1 | import sys |
2 | | -from collections.abc import Callable, Collection, Sequence |
| 2 | +from collections.abc import Callable, Sequence |
3 | 3 | from dataclasses import dataclass |
4 | 4 | from enum import Enum |
5 | 5 | from typing import Any, Literal, TypeAlias, TypeGuard |
@@ -41,16 +41,17 @@ class ActionDescriptor: |
41 | 41 | """ |
42 | 42 |
|
43 | 43 |
|
44 | | -ActionDescriptors: TypeAlias = dict[str, ActionDescriptor] |
| 44 | +HTTPMethod: TypeAlias = Literal["GET", "POST", "PUT", "PATCH", "DELETE"] |
| 45 | + |
| 46 | + |
| 47 | +ActionDescriptors: TypeAlias = dict[tuple[str, HTTPMethod], ActionDescriptor] |
45 | 48 | """ |
46 | 49 | Typing for the value of `_actions_variable`. |
47 | 50 |
|
48 | | -Dictionary that maps action paths to action descriptors. This allows |
| 51 | +Dictionary that maps action path-method pairs to action descriptors. This allows |
49 | 52 | decorator stacking (registering the same action with multiple paths). |
50 | 53 | """ |
51 | 54 |
|
52 | | -HTTPMethod: TypeAlias = Literal["GET", "POST", "PUT", "PATCH", "DELETE"] |
53 | | - |
54 | 55 |
|
55 | 56 | _actions_variable = "_holm_action_descriptors" |
56 | 57 | """The name of the variable that stores action descriptors in modules.""" |
@@ -89,7 +90,7 @@ def __call__( |
89 | 90 | *, |
90 | 91 | use_layout: bool = False, |
91 | 92 | metadata: MetadataMappingOrDependency | None = None, |
92 | | - methods: Collection[HTTPMethod] | None = None, |
| 93 | + methods: Sequence[HTTPMethod] | None = None, |
93 | 94 | dependencies: Sequence[DependsParam] | None = None, |
94 | 95 | deprecated: bool | None = None, |
95 | 96 | tags: list[str | Enum] | None = None, |
@@ -329,6 +330,7 @@ def _register_action( |
329 | 330 | action: ActionDependency, |
330 | 331 | *, |
331 | 332 | use_layout: bool, |
| 333 | + methods: Sequence[HTTPMethod] | None, |
332 | 334 | metadata: MetadataMappingOrDependency | None, |
333 | 335 | path: str | None, |
334 | 336 | tags: list[str | Enum] | None, |
@@ -362,9 +364,10 @@ def _register_action( |
362 | 364 | route_args["response_model"] = None # Don't generate a response schema. |
363 | 365 |
|
364 | 366 | # Register the action. |
365 | | - module_actions[path] = ActionDescriptor( |
366 | | - action=action, |
367 | | - route_args=route_args, |
368 | | - use_layout=use_layout, |
369 | | - metadata=metadata, |
370 | | - ) |
| 367 | + for method in ("GET",) if not methods else methods: |
| 368 | + module_actions[(path, method)] = ActionDescriptor( |
| 369 | + action=action, |
| 370 | + route_args={**route_args, "methods": (method,)}, |
| 371 | + use_layout=use_layout, |
| 372 | + metadata=metadata, |
| 373 | + ) |
0 commit comments