Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /pfun-health_pfun-cma-model /pfun_cma_model /misc /sessions.py
| from collections.abc import Iterable | |
| from typing import List, Optional | |
| import socketio | |
| from fastapi.applications import FastAPI | |
| from pfun_common.settings import get_settings | |
| class PFunSocketIOSession: | |
| """ | |
| A session for interacting with Socket.IO. | |
| """ | |
| def __init__( | |
| self, | |
| app: FastAPI, | |
| ns: Optional[socketio.AsyncNamespace | List[socketio.AsyncNamespace]] = None, | |
| ): | |
| """ | |
| Initialize the PFunSocketIOSession with a FastAPI app. | |
| This sets up the Socket.IO server with the provided app. | |
| """ | |
| self.app = app | |
| self.ns = ( | |
| ns | |
| if isinstance(ns, Iterable) and not isinstance(ns, (str, bytes)) | |
| else [ns] if ns else [] | |
| ) | |
| self.sio: Optional[socketio.AsyncServer] = None | |
| self.mgr = None | |
| # Setup Socket.IO server and mount it to the FastAPI app | |
| self.setup_redis_manager() | |
| self.setup_socketio() | |
| self.mount_socketio() | |
| def __getattr__(self, name): | |
| """Pass any non-matching attributes to the Socket.IO server.""" | |
| # First, determine if the attribute exists in the Socket.IO server | |
| if hasattr(self.sio, name): | |
| # if it does, return it | |
| return getattr(self.sio, name) | |
| # if it doesn't, check if it's a method of the PFunSocketIOSession class | |
| elif hasattr(super(), name): | |
| # if it is, return it | |
| return getattr(super(), name) | |
| # otherwise, raise an AttributeError | |
| raise AttributeError( | |
| f"'{self.__class__.__name__}' object has no attribute '{name}'" | |
| ) | |
| def setup_socketio( | |
| self, mgr: Optional[socketio.AsyncRedisManager] = None | |
| ) -> socketio.AsyncServer: | |
| """ | |
| Setup Socket.IO server with FastAPI app. | |
| This is useful for handling WebSocket connections. | |
| """ | |
| # see https://python-socketio.readthedocs.io/en/latest/server.html#using-a-message-queue | |
| # Create a Socket.IO server instance with Redis manager | |
| if mgr is None: | |
| self.setup_redis_manager() | |
| self.sio = socketio.AsyncServer( | |
| async_mode="asgi", cors_allowed_origins="*", connection_manager=self.mgr | |
| ) | |
| # Register namespaces if provided | |
| if self.ns: | |
| for namespace in self.ns: | |
| if isinstance(namespace, socketio.AsyncNamespace): | |
| self.sio.register_namespace(namespace) # type: ignore | |
| namespace._set_server(self.sio) # set the server instance | |
| else: | |
| raise TypeError( | |
| "Namespaces must be instances of socketio.AsyncNamespace" | |
| ) | |
| # initialize the Socket.IO server with the FastAPI app | |
| self.sio_app = socketio.ASGIApp( | |
| socketio_server=self.sio, other_asgi_app=self.app | |
| ) | |
| return self.sio | |
| def mount_socketio(self, path: str = "/socket.io/") -> None: | |
| """ | |
| Mount the Socket.IO ASGI app to the FastAPI app at a specified path. | |
| This allows WebSocket connections to be handled at the given path. | |
| """ | |
| self.app.add_route( | |
| path, self.sio_app, methods=["GET", "POST"], include_in_schema=False # type: ignore | |
| ) | |
| self.app.add_api_websocket_route(path, self.sio_app) | |
| def setup_redis_manager( | |
| self, url: Optional[str] = None, **kwargs | |
| ) -> socketio.AsyncRedisManager: | |
| """ | |
| Setup Redis manager for Socket.IO. | |
| """ | |
| if url is None: | |
| settings = get_settings() | |
| url = settings.redis_url | |
| self.mgr = socketio.AsyncRedisManager(url=url, **kwargs) | |
| return self.mgr | |
| class PFunBotoSession: | |
| """@todo: Implement a session for interacting with AWS Boto3.""" | |
| pass | |
Xet Storage Details
- Size:
- 3.85 kB
- Xet hash:
- 7f52170ebf6edcd72406c392289b7575232c9dcfaafb9a3372a9a2fe0e94008b
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.