|
1 | | -"""Middleware to build and apply CQL2 filters.""" |
| 1 | +"""Middleware to build the Cql2Filter.""" |
2 | 2 |
|
3 | 3 | import json |
4 | 4 | from dataclasses import dataclass |
5 | | -from logging import getLogger |
6 | 5 | from typing import Callable, Optional |
7 | 6 |
|
8 | 7 | from cql2 import Expr |
|
11 | 10 |
|
12 | 11 | from ..utils import filters, requests |
13 | 12 |
|
14 | | -logger = getLogger(__name__) |
15 | | - |
16 | 13 |
|
17 | 14 | @dataclass(frozen=True) |
18 | 15 | class BuildCql2FilterMiddleware: |
19 | 16 | """Middleware to build the Cql2Filter.""" |
20 | 17 |
|
21 | 18 | app: ASGIApp |
22 | 19 |
|
| 20 | + state_key: str = "cql2_filter" |
| 21 | + |
23 | 22 | # Filters |
24 | 23 | collections_filter: Optional[Callable] = None |
25 | 24 | items_filter: Optional[Callable] = None |
26 | 25 |
|
27 | | - state_key: str = "cql2_filter" |
28 | | - |
29 | 26 | async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None: |
30 | 27 | """Build the CQL2 filter, place on the request state.""" |
31 | 28 | if scope["type"] != "http": |
@@ -87,51 +84,3 @@ def _get_filter(self, path: str) -> Optional[Callable[..., Expr]]: |
87 | 84 | if check(path): |
88 | 85 | return builder |
89 | 86 | return None |
90 | | - |
91 | | - |
92 | | -@dataclass(frozen=True) |
93 | | -class ApplyCql2FilterMiddleware: |
94 | | - """Middleware to apply the Cql2Filter to the request.""" |
95 | | - |
96 | | - app: ASGIApp |
97 | | - |
98 | | - state_key: str = "cql2_filter" |
99 | | - |
100 | | - async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None: |
101 | | - """Add the Cql2Filter to the request.""" |
102 | | - if scope["type"] != "http": |
103 | | - return await self.app(scope, receive, send) |
104 | | - |
105 | | - request = Request(scope) |
106 | | - |
107 | | - if request.method == "GET": |
108 | | - cql2_filter = getattr(request.state, self.state_key, None) |
109 | | - if cql2_filter: |
110 | | - scope["query_string"] = filters.append_qs_filter( |
111 | | - request.url.query, cql2_filter |
112 | | - ) |
113 | | - return await self.app(scope, receive, send) |
114 | | - |
115 | | - elif request.method in ["POST", "PUT", "PATCH"]: |
116 | | - |
117 | | - async def receive_and_apply_filter() -> Message: |
118 | | - message = await receive() |
119 | | - if message["type"] != "http.request": |
120 | | - return message |
121 | | - |
122 | | - cql2_filter = getattr(request.state, self.state_key, None) |
123 | | - if cql2_filter: |
124 | | - try: |
125 | | - body = message.get("body", b"{}") |
126 | | - except json.JSONDecodeError as e: |
127 | | - logger.warning("Failed to parse request body as JSON") |
128 | | - # TODO: Return a 400 error |
129 | | - raise e |
130 | | - |
131 | | - new_body = filters.append_body_filter(json.loads(body), cql2_filter) |
132 | | - message["body"] = json.dumps(new_body).encode("utf-8") |
133 | | - return message |
134 | | - |
135 | | - return await self.app(scope, receive_and_apply_filter, send) |
136 | | - |
137 | | - return await self.app(scope, receive, send) |
0 commit comments