apitally 0.16.2__py3-none-any.whl → 0.16.3__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.
apitally/starlette.py CHANGED
@@ -1,16 +1,18 @@
1
1
  from __future__ import annotations
2
2
 
3
3
  import time
4
- from typing import Any, Callable, Dict, List, Optional, Union
4
+ from contextlib import asynccontextmanager
5
+ from typing import Any, Awaitable, Callable, Dict, List, Optional, Union
5
6
  from warnings import warn
6
7
 
7
8
  from httpx import HTTPStatusError, Proxy
9
+ from starlette.applications import Starlette
8
10
  from starlette.datastructures import Headers
9
11
  from starlette.requests import Request
10
12
  from starlette.routing import BaseRoute, Match, Router
11
13
  from starlette.schemas import EndpointInfo, SchemaGenerator
12
14
  from starlette.testclient import TestClient
13
- from starlette.types import ASGIApp, Message, Receive, Scope, Send
15
+ from starlette.types import ASGIApp, Lifespan, Message, Receive, Scope, Send
14
16
 
15
17
  from apitally.client.client_asyncio import ApitallyClient
16
18
  from apitally.client.consumers import Consumer as ApitallyConsumer
@@ -56,10 +58,13 @@ class ApitallyMiddleware:
56
58
  self.client.request_logger.config.enabled and self.client.request_logger.config.log_response_body
57
59
  )
58
60
 
59
- _register_event_handler(app, "startup", self.on_startup)
60
- _register_event_handler(app, "shutdown", self.client.handle_shutdown)
61
+ _inject_lifespan_handlers(
62
+ app,
63
+ on_startup=self.on_startup,
64
+ on_shutdown=self.client.handle_shutdown,
65
+ )
61
66
 
62
- def on_startup(self) -> None:
67
+ async def on_startup(self) -> None:
63
68
  data = _get_startup_data(self.app, app_version=self.app_version, openapi_url=self.openapi_url)
64
69
  self.client.set_startup_data(data)
65
70
  self.client.start_sync_loop()
@@ -289,8 +294,30 @@ def _get_routes(app: Union[ASGIApp, Router]) -> List[BaseRoute]:
289
294
  return [] # pragma: no cover
290
295
 
291
296
 
292
- def _register_event_handler(app: Union[ASGIApp, Router], event: str, handler: Callable[[], Any]) -> None:
293
- if isinstance(app, Router):
294
- app.add_event_handler(event, handler)
295
- elif hasattr(app, "app"):
296
- _register_event_handler(app.app, event, handler)
297
+ def _inject_lifespan_handlers(
298
+ app: Union[ASGIApp, Router],
299
+ on_startup: Callable[[], Awaitable[Any]],
300
+ on_shutdown: Callable[[], Awaitable[Any]],
301
+ ) -> None:
302
+ """
303
+ Ensures the given startup and shutdown functions are called as part of the app's lifespan context manager.
304
+ """
305
+ router = app
306
+ while not isinstance(router, Router) and hasattr(router, "app"):
307
+ router = router.app
308
+ if not isinstance(router, Router):
309
+ raise TypeError("app must be a Starlette or Router instance")
310
+
311
+ lifespan: Optional[Lifespan] = getattr(router, "lifespan_context", None)
312
+
313
+ @asynccontextmanager
314
+ async def wrapped_lifespan(app: Starlette):
315
+ await on_startup()
316
+ if lifespan is not None:
317
+ async with lifespan(app):
318
+ yield
319
+ else:
320
+ yield
321
+ await on_shutdown()
322
+
323
+ router.lifespan_context = wrapped_lifespan
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: apitally
3
- Version: 0.16.2
3
+ Version: 0.16.3
4
4
  Summary: Simple API monitoring & analytics for REST APIs built with FastAPI, Flask, Django, Starlette, Litestar and BlackSheep.
5
5
  Project-URL: Homepage, https://apitally.io
6
6
  Project-URL: Documentation, https://docs.apitally.io
@@ -49,9 +49,9 @@ Requires-Dist: inflection>=0.5.1; extra == 'django-rest-framework'
49
49
  Requires-Dist: requests>=2.26.0; extra == 'django-rest-framework'
50
50
  Requires-Dist: uritemplate>=3.0.0; extra == 'django-rest-framework'
51
51
  Provides-Extra: fastapi
52
- Requires-Dist: fastapi>=0.88.0; extra == 'fastapi'
52
+ Requires-Dist: fastapi>=0.94.1; extra == 'fastapi'
53
53
  Requires-Dist: httpx>=0.22.0; extra == 'fastapi'
54
- Requires-Dist: starlette<1.0.0,>=0.22.0; extra == 'fastapi'
54
+ Requires-Dist: starlette<1.0.0,>=0.26.1; extra == 'fastapi'
55
55
  Provides-Extra: flask
56
56
  Requires-Dist: flask>=2.0.0; extra == 'flask'
57
57
  Requires-Dist: requests>=2.26.0; extra == 'flask'
@@ -62,7 +62,7 @@ Provides-Extra: sentry
62
62
  Requires-Dist: sentry-sdk>=2.2.0; extra == 'sentry'
63
63
  Provides-Extra: starlette
64
64
  Requires-Dist: httpx>=0.22.0; extra == 'starlette'
65
- Requires-Dist: starlette<1.0.0,>=0.21.0; extra == 'starlette'
65
+ Requires-Dist: starlette<1.0.0,>=0.26.1; extra == 'starlette'
66
66
  Description-Content-Type: text/markdown
67
67
 
68
68
  <p align="center">
@@ -8,7 +8,7 @@ apitally/fastapi.py,sha256=IfKfgsmIY8_AtnuMTW2sW4qnkya61CAE2vBoIpcc9tk,169
8
8
  apitally/flask.py,sha256=OoCEnjtnD51GUGq-adK80ebuiLj-5HXubxffCv5XTCM,9622
9
9
  apitally/litestar.py,sha256=mHoMqBO_gyoopeHljY8e8GTcV29UDf3uhQMxY3GeNpA,13451
10
10
  apitally/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
11
- apitally/starlette.py,sha256=g6YmC186uG6Lc9-Gx3Pp4ke-jn8IeKK0nFLBGDnWYv0,12771
11
+ apitally/starlette.py,sha256=081vXTNOy6-zZ8ugXknRtQZnFFqG7XKLW4Ho6oaHEUY,13525
12
12
  apitally/client/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
13
13
  apitally/client/client_asyncio.py,sha256=rTsH5wlLHK3RmyIuEiT6vzjquU-l2OPC34JnC2U6uYw,6658
14
14
  apitally/client/client_base.py,sha256=DvivGeHd3dyOASRvkIo44Zh8RzdBMfH8_rROa2lFbgw,3799
@@ -20,7 +20,7 @@ apitally/client/requests.py,sha256=SDptGOg9XvaEKFj2o3oxJz-JAuZzUrqpHnbOQixf99o,3
20
20
  apitally/client/sentry.py,sha256=qMjHdI0V7c50ruo1WjmjWc8g6oGDv724vSCvcuZ8G9k,1188
21
21
  apitally/client/server_errors.py,sha256=4B2BKDFoIpoWc55UVH6AIdYSgzj6zxCdMNUW77JjhZw,3423
22
22
  apitally/client/validation_errors.py,sha256=6G8WYWFgJs9VH9swvkPXJGuOJgymj5ooWA9OwjUTbuM,1964
23
- apitally-0.16.2.dist-info/METADATA,sha256=Lycf-s6pDP5lO9B8-pRqcDfHZcJTFZQ--wSuquoDZn0,9321
24
- apitally-0.16.2.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
25
- apitally-0.16.2.dist-info/licenses/LICENSE,sha256=vbLzC-4TddtXX-_AFEBKMYWRlxC_MN0g66QhPxo8PgY,1065
26
- apitally-0.16.2.dist-info/RECORD,,
23
+ apitally-0.16.3.dist-info/METADATA,sha256=NbeWFQBfMfdu1Ao_0zGhBTMsNO3m1xmWZ6KNhGe2qNQ,9321
24
+ apitally-0.16.3.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
25
+ apitally-0.16.3.dist-info/licenses/LICENSE,sha256=vbLzC-4TddtXX-_AFEBKMYWRlxC_MN0g66QhPxo8PgY,1065
26
+ apitally-0.16.3.dist-info/RECORD,,