Skip to content

Commit 40f18da

Browse files
committed
Move requests (required for SSR only) to an extra
1 parent dafda82 commit 40f18da

File tree

5 files changed

+19
-7
lines changed

5 files changed

+19
-7
lines changed

.github/workflows/pytest.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ jobs:
2222
run: |
2323
python -m pip install -U pip
2424
pip install poetry
25-
poetry install
25+
poetry install -E ssr
2626
- name: Run python tests
2727
run: |
2828
poetry run pytest

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -297,11 +297,14 @@ class LogoutView(auth_views.LogoutView):
297297
return response
298298
```
299299

300-
### SSR
300+
### SSR
301301

302302
#### Backend
303303

304-
Enable SSR via the `INERTIA_SSR_URL` and `INERTIA_SSR_ENABLED` settings
304+
* Ensure `requests` is installed, so inertia-django can do SSR requests.
305+
* `requests` is configured as a dependency if you install the `[ssr]` extra,
306+
e.g. `inertia-django[ssr]` in your requirements.
307+
* Enable SSR via the `INERTIA_SSR_URL` and `INERTIA_SSR_ENABLED` settings.
305308

306309
#### Frontend
307310

inertia/http.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,21 @@
22
from http import HTTPStatus
33
from json import dumps as json_encode
44

5-
import requests
65
from django.http import HttpResponse
76
from django.template.loader import render_to_string
87

98
from .helpers import deep_transform_callables, validate_type
109
from .prop_classes import DeferredProp, IgnoreOnFirstLoadProp, MergeableProp
1110
from .settings import settings
1211

12+
try:
13+
# Must be early-imported so tests can patch it with
14+
# a mock module
15+
import requests
16+
except ImportError:
17+
requests = None
18+
19+
1320
INERTIA_REQUEST_ENCRYPT_HISTORY = "_inertia_encrypt_history"
1421
INERTIA_SESSION_CLEAR_HISTORY = "_inertia_clear_history"
1522

inertia/tests/test_ssr.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
from unittest.mock import Mock, patch
33

44
from django.test import override_settings
5-
from requests.exceptions import RequestException
65

76
from inertia.test import InertiaTestCase, inertia_div, inertia_page
87

@@ -66,7 +65,7 @@ def test_it_uses_inertia_if_inertia_requests_are_made(self, mock_requests):
6665
@patch("inertia.http.requests")
6766
def test_it_fallsback_on_failure(self, mock_requests):
6867
def uh_oh(*args, **kwargs):
69-
raise RequestException()
68+
raise ValueError() # all exceptions are caught and ignored
7069

7170
mock_response = Mock()
7271
mock_response.raise_for_status.side_effect = uh_oh

pyproject.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@ packages = [
2222
[tool.poetry.dependencies]
2323
python = "^3.8"
2424
django = ">=4"
25-
requests = "^2"
25+
requests = {version = "^2", optional = true}
26+
27+
[tool.poetry.extras]
28+
ssr = ["requests"]
2629

2730
[tool.poetry.dev-dependencies]
2831
pytest = "^7.1.2"

0 commit comments

Comments
 (0)