From d1fe1c403b4f5eecea5a659fcda689300ba4495a Mon Sep 17 00:00:00 2001 From: Archmonger <16909269+Archmonger@users.noreply.github.com> Date: Thu, 14 Sep 2023 22:45:29 -0700 Subject: [PATCH] use_origin and use_connection support --- src/reactpy_django/hooks.py | 26 ++++++++++++++++++-------- src/reactpy_django/types.py | 3 ++- 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/src/reactpy_django/hooks.py b/src/reactpy_django/hooks.py index c60bbf1c..f74f53d8 100644 --- a/src/reactpy_django/hooks.py +++ b/src/reactpy_django/hooks.py @@ -48,14 +48,24 @@ def use_origin() -> str | None: this will be None.""" scope = _use_scope() try: - return next( - ( - header[1].decode("utf-8") - for header in scope["headers"] - if header[0] == b"origin" - ), - None, - ) + if scope["type"] == "websocket": + return next( + ( + header[1].decode("utf-8") + for header in scope["headers"] + if header[0] == b"origin" + ), + None, + ) + if scope["type"] == "http": + host = next( + ( + header[1].decode("utf-8") + for header in scope["headers"] + if header[0] == b"host" + ) + ) + return f"{scope['scheme']}://{host}" if host else None except Exception: return None diff --git a/src/reactpy_django/types.py b/src/reactpy_django/types.py index 233fe432..251efc15 100644 --- a/src/reactpy_django/types.py +++ b/src/reactpy_django/types.py @@ -16,6 +16,7 @@ from django.db.models.base import Model from django.db.models.query import QuerySet +from django.http import HttpRequest from django.views.generic import View from reactpy.types import Connection as _Connection from typing_extensions import ParamSpec @@ -50,7 +51,7 @@ class ComponentWebsocket: dotted_path: str -Connection = _Connection[ComponentWebsocket] +Connection = _Connection[ComponentWebsocket | HttpRequest] @dataclass