PyLoid Server Adapter
Project description
Pyloid Server Adapter
Pyloid Server Adapter는 FastAPI 애플리케이션에서 Pyloid 컨텍스트를 주입할 수 있는 어댑터입니다.
기능
- 자동 미들웨어: FastAPI 앱에 어댑터를 설정하면 모든 요청에 대해 자동으로
PyloidContext주입 X-Pyloid-Window-Id헤더를 통한 윈도우 ID 관리- Pyloid 인스턴스와 윈도우 등록/조회 기능
- 다양한 사용법: 미들웨어, 데코레이터, Depends 등 여러 방식 지원
설치
# 개발 의존성 포함
pip install -e ".[dev]"
또는
# FastAPI만 필요
pip install fastapi
사용법
기본 설정
from fastapi import FastAPI, Request
from src.fastapi_adapter import FastAPIAdapter
app = FastAPI()
def start(app: FastAPI, host: str, port: int):
import uvicorn
uvicorn.Server(config=uvicorn.Config(app, host=host, port=port)).run()
fastapi_adapter = FastAPIAdapter(app=app, start_function=start)
# Pyloid 인스턴스 설정
class MockPyloid:
def __init__(self):
self.name = "My Pyloid App"
fastapi_adapter.set_pyloid_instance(MockPyloid())
# 윈도우 등록
class MockWindow:
def __init__(self, window_id: str):
self.id = window_id
self.title = f"Window {window_id}"
window = MockWindow("main-window")
fastapi_adapter.register_window("main-window", window)
ctx 사용 방법 1: 미들웨어 방식 (권장) ⭐
자동으로 모든 요청에 대해 PyloidContext가 주입됩니다!
@app.get("/api/data")
async def get_data(request: Request):
# request.state.pyloid_context로 접근
ctx = request.state.pyloid_context
return {
"pyloid": ctx.pyloid.name if ctx.pyloid else None,
"window": ctx.window.title if ctx.window else None
}
ctx 사용 방법 2: 데코레이터 사용
from src.fastapi_adapter import PyloidContext
@app.get("/api/data2")
@fastapi_adapter.inject_context
async def get_data2(ctx: PyloidContext):
return {
"pyloid": ctx.pyloid.name if ctx.pyloid else None,
"window": ctx.window.title if ctx.window else None
}
ctx 사용 방법 3: FastAPI Depends 사용
from fastapi import Depends
from src.fastapi_adapter import PyloidContext
@app.get("/api/data3")
async def get_data3(ctx: PyloidContext = Depends(fastapi_adapter.get_context_dependency())):
return {
"pyloid": ctx.pyloid.name if ctx.pyloid else None,
"window": ctx.window.title if ctx.window else None
}
클라이언트 요청 예시
# X-Pyloid-Window-Id 헤더를 포함한 요청
curl -H "X-Pyloid-Window-Id: main-window" http://localhost:8000/api/data
PyloidContext
PyloidContext 클래스는 다음과 같은 속성을 제공합니다:
pyloid: Pyloid 애플리케이션 인스턴스window: 현재 윈도우 인스턴스 (X-Pyloid-Window-Id 헤더에 해당하는 윈도우)
API 참조
FastAPIAdapter
__init__(app: FastAPI, start_function: Callable[[str, int], None])
FastAPI 어댑터를 초기화합니다.
set_pyloid_instance(pyloid: Any)
Pyloid 인스턴스를 설정합니다.
register_window(window_id: str, window: Any)
윈도우를 등록합니다.
get_window_by_id(window_id: str) -> Optional[Any]
윈도우 ID로 윈도우를 조회합니다.
get_pyloid_context(request: Request) -> PyloidContext
요청으로부터 PyloidContext를 생성합니다.
get_context_dependency() -> Callable
FastAPI 의존성 주입을 위한 함수를 반환합니다.
inject_context(func: Callable) -> Callable
ctx 매개변수가 있는 함수에 PyloidContext를 주입하는 데코레이터입니다.
PyloidContextMiddleware
자동으로 모든 HTTP 요청에 대해 PyloidContext를 주입하는 미들웨어입니다.
속성
pyloid_adapter: FastAPIAdapter 인스턴스
사용법
미들웨어는 FastAPIAdapter 생성 시 자동으로 등록되므로 별도 설정이 필요하지 않습니다.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pyloid_server_adapter-0.1.0.tar.gz.
File metadata
- Download URL: pyloid_server_adapter-0.1.0.tar.gz
- Upload date:
- Size: 10.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.6.17
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fa5f69f606b3ca9f92ef50224b1944531c9918d960926bc7651e528295c5e556
|
|
| MD5 |
5fc83910dc6f68a0b139e4d15ac0dbc6
|
|
| BLAKE2b-256 |
f8970ded4bb4e8426c3deda6d9d7e06d96df03c45638d85f5360fcec6d58cec5
|
File details
Details for the file pyloid_server_adapter-0.1.0-py3-none-any.whl.
File metadata
- Download URL: pyloid_server_adapter-0.1.0-py3-none-any.whl
- Upload date:
- Size: 12.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.6.17
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c156a8d1f4d3679210da08eb66210044aaca02823e3507066f64fa721036429a
|
|
| MD5 |
e4ebabbd6989c00e1006edcb8277f6e4
|
|
| BLAKE2b-256 |
a84f7b42639b24adf48e5085990048b15fd71d5c6b70dc4ea57253935575cc61
|