Skip to content

Commit a2c3acb

Browse files
committed
refactor: Rename context setting functions and update their docstrings for clarity.
1 parent ab5c8c1 commit a2c3acb

File tree

3 files changed

+11
-19
lines changed

3 files changed

+11
-19
lines changed

openlibrary/asgi_app.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from uvicorn.middleware.proxy_headers import ProxyHeadersMiddleware
1414

1515
import infogami
16-
from openlibrary.utils.async_utils import set_context_for_async_request
16+
from openlibrary.utils.async_utils import set_context_from_fastapi
1717
from openlibrary.utils.sentry import Sentry, init_sentry
1818

1919
logger = logging.getLogger("openlibrary.asgi_app")
@@ -166,7 +166,7 @@ async def add_fastapi_header(request: Request, call_next):
166166

167167
@app.middleware("http")
168168
async def set_context(request: Request, call_next):
169-
set_context_for_async_request(request)
169+
set_context_from_fastapi(request)
170170
response = await call_next(request)
171171
return response
172172

openlibrary/plugins/openlibrary/code.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
from openlibrary.core.models import Edition
4949
from openlibrary.plugins.openlibrary import processors
5050
from openlibrary.plugins.openlibrary.stats import increment_error_count
51-
from openlibrary.utils.async_utils import set_context_for_sync_request
51+
from openlibrary.utils.async_utils import set_context_from_legacy_web_py
5252
from openlibrary.utils.isbn import canonical, isbn_10_to_isbn_13, isbn_13_to_isbn_10
5353

5454
delegate.app.add_processor(processors.ReadableUrlProcessor())
@@ -113,7 +113,7 @@ def contextvars_processor(handler):
113113

114114
def inner():
115115
# Now we are inside the new, empty context
116-
set_context_for_sync_request()
116+
set_context_from_legacy_web_py()
117117
return handler()
118118

119119
return ctx.run(inner)

openlibrary/utils/async_utils.py

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,7 @@ def wrapper(*args: P.args, **kwargs: P.kwargs) -> T:
4545

4646
async_bridge = AsyncBridge()
4747

48-
# ContextVariables
49-
# These are scoped to a specific request
50-
# Defining them here for now to keep things close together
48+
################### ContextVariables ###################
5149

5250

5351
@dataclass(frozen=True)
@@ -67,12 +65,9 @@ class RequestContextVars:
6765
site: ContextVar[Site] = ContextVar("site")
6866

6967

70-
def set_context_for_sync_request():
68+
def set_context_from_legacy_web_py() -> None:
7169
"""
72-
Sets the contextvars for the current request when called from web.py
73-
74-
create_site is ultimately temporary as we'll need an async version of site
75-
That version should be usable without instantiating a site
70+
Extracts context from the global web.ctx (sync) and populates ContextVars.
7671
"""
7772
site.set(create_site())
7873
req_context.set(
@@ -83,15 +78,12 @@ def set_context_for_sync_request():
8378
)
8479

8580

86-
def set_context_for_async_request(request: Request):
81+
def set_context_from_fastapi(request: Request) -> None:
8782
"""
88-
These are set automatically in the middleware of fastapi
89-
They are made available to all requests
90-
91-
BEFORE ADDING:
92-
Please consider if we could feasibly just pass it down to the function instead
93-
This is preferable for testable and maintainable code.
83+
Extracts context from a FastAPI request (async) and populates ContextVars.
84+
Should be called within the middleware stack.
9485
"""
86+
# NOTE: Avoid adding new fields here if they can be passed as function arguments instead.
9587
site.set(create_site())
9688
req_context.set(
9789
RequestContextVars(

0 commit comments

Comments
 (0)