nucliadb-utils 5.2.1.post1904__py3-none-any.whl → 5.2.1.post1912__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -30,8 +30,9 @@ import nats
30
30
  from fastapi import Request
31
31
  from google.protobuf.timestamp_pb2 import Timestamp
32
32
  from opentelemetry.trace import format_trace_id, get_current_span
33
+ from starlette.background import BackgroundTask
33
34
  from starlette.middleware.base import BaseHTTPMiddleware, RequestResponseEndpoint
34
- from starlette.responses import Response, StreamingResponse
35
+ from starlette.responses import Response
35
36
  from starlette.types import ASGIApp
36
37
 
37
38
  from nucliadb_protos.audit_pb2 import AuditField, AuditRequest, ChatContext, ClientType, RetrievedContext
@@ -98,11 +99,15 @@ class AuditMiddleware(BaseHTTPMiddleware):
98
99
 
99
100
  response = await call_next(request)
100
101
 
101
- if isinstance(response, StreamingResponse):
102
- response = self.wrap_streaming_response(response, context)
103
- else:
104
- self.enqueue_pending(context)
102
+ # This task will run when the response finishes streaming
103
+ # When dealing with streaming responses, AND if we depend on any state that only will be available once
104
+ # the request is fully finished, the response we have after the dispatch call_next is not enough, as
105
+ # there, no iteration of the streaming response has been done yet.
106
+ response.background = BackgroundTask(self.enqueue_pending, context)
105
107
 
108
+ # It is safe to reset the context here since the asyncio task for generating the streaming response is
109
+ # already running. If we want to spawn a different task during streaming and we want that task be able
110
+ # to read the context_var, we need to manually pass the context into that task.
106
111
  request_context_var.reset(token)
107
112
 
108
113
  return response
@@ -116,29 +121,6 @@ class AuditMiddleware(BaseHTTPMiddleware):
116
121
  if self.audit_utility is not None:
117
122
  self.audit_utility.send(context.audit_request)
118
123
 
119
- def wrap_streaming_response(
120
- self, response: StreamingResponse, context: RequestContext
121
- ) -> StreamingResponse:
122
- """
123
- When dealing with streaming responses, AND if we depend on any state that only will be available once
124
- the request is fully finished, the response we have after the dispatch call_next is not enough, as
125
- there, no iteration of the streaming response has been done yet.
126
-
127
- This is why we need to rewrap to be able to to the auditing at the _real_ request end without losing
128
- any audit bits.
129
- """
130
- original_body_iterator = response.body_iterator
131
-
132
- async def custom_body_iterator():
133
- try:
134
- async for chunk in original_body_iterator:
135
- yield chunk
136
- finally:
137
- self.enqueue_pending(context)
138
-
139
- response.body_iterator = custom_body_iterator()
140
- return response
141
-
142
124
 
143
125
  KB_USAGE_STREAM_SUBJECT = "kb-usage.nuclia_db"
144
126
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: nucliadb_utils
3
- Version: 5.2.1.post1904
3
+ Version: 5.2.1.post1912
4
4
  Home-page: https://nuclia.com
5
5
  License: BSD
6
6
  Classifier: Development Status :: 4 - Beta
@@ -24,8 +24,8 @@ Requires-Dist: PyNaCl
24
24
  Requires-Dist: pyjwt>=2.4.0
25
25
  Requires-Dist: memorylru>=1.1.2
26
26
  Requires-Dist: mrflagly>=0.2.9
27
- Requires-Dist: nucliadb-protos>=5.2.1.post1904
28
- Requires-Dist: nucliadb-telemetry>=5.2.1.post1904
27
+ Requires-Dist: nucliadb-protos>=5.2.1.post1912
28
+ Requires-Dist: nucliadb-telemetry>=5.2.1.post1912
29
29
  Provides-Extra: cache
30
30
  Requires-Dist: redis>=4.3.4; extra == "cache"
31
31
  Requires-Dist: orjson>=3.6.7; extra == "cache"
@@ -24,7 +24,7 @@ nucliadb_utils/aiopynecone/models.py,sha256=XkNIZx4bxdbVo9zYVn8IRp70q4DWUMWN79yb
24
24
  nucliadb_utils/audit/__init__.py,sha256=cp15ZcFnHvpcu_5-aK2A4uUyvuZVV_MJn4bIXMa20ks,835
25
25
  nucliadb_utils/audit/audit.py,sha256=hH1L5XeRjTO896wMyINqiI5dkgGD_h8kbMsRs2HZ8Ts,3326
26
26
  nucliadb_utils/audit/basic.py,sha256=qKA7YAesuhRfW3iQXInI_GVm32az-WI6UPdS8QKQ_Fw,3974
27
- nucliadb_utils/audit/stream.py,sha256=vouDT75vCZwQEio0MRUm8JObK_25eknSotvVlaz3ppY,15162
27
+ nucliadb_utils/audit/stream.py,sha256=j2C-jrm1lLnHlojrUn2nnf3Rk9LCItw7IQbwkfSfw9E,14828
28
28
  nucliadb_utils/cache/__init__.py,sha256=itSI7dtTwFP55YMX4iK7JzdMHS5CQVUiB1XzQu4UBh8,833
29
29
  nucliadb_utils/cache/exceptions.py,sha256=Zu-O_-0-yctOEgoDGI92gPzWfBMRrpiAyESA62ld6MA,975
30
30
  nucliadb_utils/cache/nats.py,sha256=-AjCfkFgKVdJUlGR0hT9JDSNkPVFg4S6w9eW-ZIcXPM,7037
@@ -59,8 +59,8 @@ nucliadb_utils/tests/indexing.py,sha256=YW2QhkhO9Q_8A4kKWJaWSvXvyQ_AiAwY1VylcfVQ
59
59
  nucliadb_utils/tests/local.py,sha256=7nuP8EFUAiA8ZH50R1iPV9EUXBySQxOanVm3Zht_e0g,1835
60
60
  nucliadb_utils/tests/nats.py,sha256=xqpww4jZjTKY9oPGlJdDJG67L3FIBQsa9qDHxILR8r8,7687
61
61
  nucliadb_utils/tests/s3.py,sha256=IdMxK_cNdSHLvO1u8BwsKFzD87Hk1MVPDZ57zx6h-rA,3656
62
- nucliadb_utils-5.2.1.post1904.dist-info/METADATA,sha256=gyjajvmdK3t-uM1BP0E9n13yuDqwNujNzEC1ea6_KvA,2055
63
- nucliadb_utils-5.2.1.post1904.dist-info/WHEEL,sha256=OVMc5UfuAQiSplgO0_WdW7vXVGAt9Hdd6qtN4HotdyA,91
64
- nucliadb_utils-5.2.1.post1904.dist-info/top_level.txt,sha256=fE3vJtALTfgh7bcAWcNhcfXkNPp_eVVpbKK-2IYua3E,15
65
- nucliadb_utils-5.2.1.post1904.dist-info/zip-safe,sha256=AbpHGcgLb-kRsJGnwFEktk7uzpZOCcBY74-YBdrKVGs,1
66
- nucliadb_utils-5.2.1.post1904.dist-info/RECORD,,
62
+ nucliadb_utils-5.2.1.post1912.dist-info/METADATA,sha256=Gb_v9Cp3mabD74kyLbFswHqT0jDUFLo8AZdfokbZK5w,2055
63
+ nucliadb_utils-5.2.1.post1912.dist-info/WHEEL,sha256=OVMc5UfuAQiSplgO0_WdW7vXVGAt9Hdd6qtN4HotdyA,91
64
+ nucliadb_utils-5.2.1.post1912.dist-info/top_level.txt,sha256=fE3vJtALTfgh7bcAWcNhcfXkNPp_eVVpbKK-2IYua3E,15
65
+ nucliadb_utils-5.2.1.post1912.dist-info/zip-safe,sha256=AbpHGcgLb-kRsJGnwFEktk7uzpZOCcBY74-YBdrKVGs,1
66
+ nucliadb_utils-5.2.1.post1912.dist-info/RECORD,,