Skip to content

Commit

Permalink
fix(sdk): Move local user to local
Browse files Browse the repository at this point in the history
  • Loading branch information
Lasse-numerous committed Oct 25, 2024
1 parent 9c396b2 commit 628a2da
Show file tree
Hide file tree
Showing 10 changed files with 26 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def add_marimo_cookie_middleware(
cookies = FileCookieStorage(cookies_dir, session_ident or _ident)
use_cookie_storage(cookies)

@app.middleware("http")
@app.middleware("http") # type: ignore[misc, unused-ignore]
async def middleware(
request: Request,
call_next: t.Callable[[Request], t.Awaitable[Response]],
Expand Down
4 changes: 2 additions & 2 deletions python/src/numerous/frameworks/fastapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from fastapi import Request

from numerous import user_session
from numerous.utils import is_local_mode
from numerous.local import is_local_mode, local_user


class FastapiCookieGetter:
Expand All @@ -16,7 +16,7 @@ def cookies(self) -> Dict[str, str]:
"""Get the cookies associated with the current request."""
if is_local_mode():
# Update the cookies on the fastapi server
self.request.cookies.update(user_session.get_encoded_user_info())
self.request.cookies.update(user_session.get_encoded_user_info(local_user))

return {str(key): str(val) for key, val in self.request.cookies.items()}

Expand Down
4 changes: 2 additions & 2 deletions python/src/numerous/frameworks/flask.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from flask import request

from numerous import user_session
from numerous.utils import is_local_mode
from numerous.local import is_local_mode, local_user


class FlaskCookieGetter:
Expand All @@ -14,7 +14,7 @@ def cookies(self) -> Dict[str, str]:
local_cookies = {}
if is_local_mode():
# Update the cookies on the flask server
local_cookies = user_session.get_encoded_user_info()
local_cookies = user_session.get_encoded_user_info(local_user)
return {
**local_cookies,
**{str(key): str(val) for key, val in request.cookies.items()},
Expand Down
4 changes: 2 additions & 2 deletions python/src/numerous/frameworks/marimo.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@

from numerous import user_session
from numerous.experimental import marimo
from numerous.utils import is_local_mode
from numerous.local import is_local_mode, local_user


class MarimoCookieGetter:
def cookies(self) -> Dict[str, Any]:
"""Get the cookies associated with the current request."""
if is_local_mode():
# Update the cookies on the marimo server
marimo.cookies().update(user_session.get_encoded_user_info())
marimo.cookies().update(user_session.get_encoded_user_info(local_user))
return {key: str(val) for key, val in marimo.cookies().items()}


Expand Down
4 changes: 2 additions & 2 deletions python/src/numerous/frameworks/panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import panel as pn

from numerous import user_session
from numerous.utils import is_local_mode
from numerous.local import is_local_mode, local_user


class PanelCookieGetter:
Expand All @@ -14,7 +14,7 @@ def cookies() -> Dict[str, str]:
"""Get the cookies associated with the current request."""
if is_local_mode():
# Add the user info to the cookies on panel server
pn.state.cookies.update(user_session.get_encoded_user_info())
pn.state.cookies.update(user_session.get_encoded_user_info(local_user))

if pn.state.curdoc and pn.state.curdoc.session_context:
return {key: str(val) for key, val in pn.state.cookies.items()}
Expand Down
4 changes: 2 additions & 2 deletions python/src/numerous/frameworks/streamlit.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import streamlit as st

from numerous import user_session
from numerous.utils import is_local_mode
from numerous.local import is_local_mode, local_user


class StreamlitCookieGetter:
Expand All @@ -14,7 +14,7 @@ def cookies(self) -> Dict[str, str]:
local_cookies = {}
if is_local_mode():
# Update the cookies on the streamlit server
local_cookies = user_session.get_encoded_user_info()
local_cookies = user_session.get_encoded_user_info(local_user)
return {
**local_cookies,
**{key: str(val) for key, val in st.context.cookies.items()},
Expand Down
13 changes: 13 additions & 0 deletions python/src/numerous/local.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
"""Local mode utilities."""

from os import getenv

from numerous.user import User


local_user = User(id="local_user", name="Local User")


def is_local_mode() -> bool:
url = getenv("NUMEROUS_API_URL")
return url is None
6 changes: 2 additions & 4 deletions python/src/numerous/user_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@ def encode_user_info(user_id: str, name: str) -> str:
return base64.b64encode(user_info_json.encode("utf-8")).decode("utf-8")


def get_encoded_user_info(
user_id: str = "local_user", name: str = "Local User"
) -> Dict[str, str]:
return {"numerous_user_info": encode_user_info(user_id, name)}
def get_encoded_user_info(user: User) -> Dict[str, str]:
return {"numerous_user_info": encode_user_info(user.id, user.name)}


class Session:
Expand Down
6 changes: 0 additions & 6 deletions python/src/numerous/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"""Miscellaneous utilities."""

from os import getenv
from typing import TypeVar


Expand All @@ -11,8 +10,3 @@ class _MissingType:
MISSING = _MissingType()

AppT = TypeVar("AppT")


def is_local_mode() -> bool:
url = getenv("NUMEROUS_API_URL")
return url is None
1 change: 0 additions & 1 deletion src/numerous/experimental/marimo/_cookies/fastapi.py

This file was deleted.

0 comments on commit 628a2da

Please sign in to comment.