Skip to content

Commit 2a95446

Browse files
committed
2.0.4 (2025-05-17)
------------------ **Changed:** `FortiGateBase.get_session()`, private to public
1 parent c44f138 commit 2a95446

File tree

6 files changed

+34
-25
lines changed

6 files changed

+34
-25
lines changed

CHANGELOG.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@ Unreleased
1313
**New:** `FortiGateAPI.monitor` connectors, to work with all `Monitor API` endpoints.
1414

1515

16+
2.0.4 (2025-05-17)
17+
------------------
18+
19+
**Changed:** `FortiGateBase.get_session()`, private to public
20+
21+
**Added:** example policy_filter_efilter.py
22+
23+
1624
2.0.3 (2024-12-30)
1725
------------------
1826

docs/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
project = "fortigate-api"
99
copyright = "2021, Vladimirs Prusakovs"
1010
author = "Vladimirs Prusakovs"
11-
release = "2.0.3"
11+
release = "2.0.4"
1212

1313
extensions = [
1414
"sphinx.ext.autodoc",

fortigate_api/fortigate_base.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -197,13 +197,13 @@ def _bearer_token(self) -> DStr:
197197
"""Return bearer token."""
198198
return {"Authorization": f"Bearer {self.token}"}
199199

200-
def _get_session(self) -> Session:
200+
def get_session(self) -> Session:
201201
"""Return an existing session or create a new one."""
202202
if not self.is_connected:
203203
self.login()
204204
session = self._session
205205
if not isinstance(session, Session):
206-
raise TypeError(f"{session=} {Session} expected.")
206+
raise TypeError(f"{Session.__name__} expected.")
207207
return session
208208

209209
@staticmethod
@@ -291,7 +291,7 @@ def _response(self, method: Method, url: str, data: ODAny = None) -> Response:
291291
if self.token:
292292
params["headers"] = self._bearer_token()
293293

294-
session: Session = self._get_session()
294+
session: Session = self.get_session()
295295
method_: Callable = getattr(session, method)
296296
try:
297297
response: Response = method_(**params)

pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "fortigate_api"
3-
version = "2.0.3"
3+
version = "2.0.4"
44
description = "Python package to configure Fortigate (Fortios) devices using REST API and SSH"
55
authors = ["Vladimirs Prusakovs <vladimir.prusakovs@gmail.com>"]
66
readme = "README.rst"
@@ -62,7 +62,7 @@ test = ["pytest"]
6262

6363
[tool.poetry.urls]
6464
"Bug Tracker" = "https://github.com/vladimirs-git/fortigate-api/issues"
65-
"Download URL" = "https://github.com/vladimirs-git/fortigate-api/archive/refs/tags/2.0.3.tar.gz"
65+
"Download URL" = "https://github.com/vladimirs-git/fortigate-api/archive/refs/tags/2.0.4.tar.gz"
6666

6767
[tool.pylint]
6868
max-line-length = 100

tests/test__connector.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ def test__get_uid(connectors_name: list, uid, data, expected: Any):
212212
"""Connector._get_uid()"""
213213
connector = connectors_name[0]
214214
connector.uid = uid
215+
215216
if isinstance(expected, str):
216217
actual = connector._get_uid(data=data)
217218
assert actual == expected

tests/test__fortigate_base.py

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import requests_mock
77
from pytest_mock import MockerFixture
88
from requests import Session
9-
from requests_mock import Mocker
109

1110
from fortigate_api import fortigate_base
1211
from fortigate_api.fortigate import FortiGate
@@ -16,8 +15,8 @@
1615

1716

1817
@pytest.fixture
19-
def api():
20-
"""Init FortiGate"""
18+
def api() -> FortiGate:
19+
"""Init FortiGate."""
2120
return FortiGate(host="host")
2221

2322

@@ -63,7 +62,7 @@ def test__enter__(mocker: MockerFixture):
6362
session_m = mocker.Mock()
6463
mocker.patch(target="requests.Session.post", return_value=session_m)
6564
mocker.patch(target="requests.Session.get", return_value=tst.crate_response(200))
66-
with patch("fortigate_api.FortiGate._get_token_from_cookies", return_value="token"):
65+
with patch("fortigate_api.FortiGate._get_token_from_cookies", return_value="TOKEN"):
6766
with FortiGate(host="host") as fgt:
6867
session = fgt._session
6968
assert isinstance(session, Session) is True
@@ -125,21 +124,22 @@ def test__login(token, expected, headers):
125124

126125
# =========================== helpers ============================
127126

128-
def test__get_session(api: FortiGate, mocker: MockerFixture):
129-
"""FortiGateBase._get_session()"""
130-
# session
131-
api._session = Session()
132-
session = api._get_session()
133-
assert isinstance(session, Session)
134-
135-
# login
136-
api._session = None
137-
mock_response = mocker.Mock()
138-
mocker.patch(target="requests.Session.post", return_value=mock_response)
139-
mocker.patch(target="requests.Session.get", return_value=tst.crate_response(200))
140-
with patch("fortigate_api.FortiGate._get_token_from_cookies", return_value="token"):
141-
session = api._get_session()
142-
assert isinstance(session, Session) is True
127+
@pytest.mark.parametrize("session, expected", [
128+
(Session(), True),
129+
(None, TypeError),
130+
])
131+
def test__get_session(api: FortiGate, session, expected):
132+
"""FortiGateBase.get_session()"""
133+
api._session = session
134+
135+
if isinstance(expected, bool):
136+
session = api.get_session()
137+
assert isinstance(session, Session)
138+
139+
else:
140+
with pytest.raises(expected):
141+
with patch.object(FortiGate, "login", return_value=None):
142+
api.get_session()
143143

144144

145145
@pytest.mark.parametrize("name, expected", [

0 commit comments

Comments
 (0)