Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(rest): Added a new method to refresh the session token #1190

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changes/unreleased/Added-20240921-220814.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
kind: Added
body: Added a new `RESTClient`` method to refresh the session token
time: 2024-09-21T22:08:14.334533-06:00
custom:
Issue: "1190"
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ max_supported_python = "3.14"
[tool.pytest.ini_options]
addopts = [
"-vvv",
"--capture=no", # show console output
"--reverse",
"-ra", # show extra test summary info for all except passed
"--strict-config",
Expand Down
18 changes: 13 additions & 5 deletions src/citric/_rest/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,16 @@ def __init__(
self.url = url
self._session = requests_session or requests.session()
self._session.headers["User-Agent"] = self.USER_AGENT
self._session_id: str | None
self.__session_id: str | None = None

self.authenticate(
username=username,
password=password,
)
self.authenticate(username=username, password=password)
self._session.auth = self._auth

@property
def session_id(self) -> str | None:
"""Session ID."""
return self.__session_id

def authenticate(self, username: str, password: str) -> None:
"""Authenticate with the REST API.

Expand All @@ -72,6 +74,12 @@ def authenticate(self, username: str, password: str) -> None:
response.raise_for_status()
self.__session_id = response.json()

def refresh_token(self) -> None:
"""Refresh the session token."""
response = self._session.put(url=f"{self.url}/rest/v1/session")
response.raise_for_status()
self.__session_id = response.json()["token"]

def close(self) -> None:
"""Delete the session."""
response = self._session.delete(f"{self.url}/rest/v1/session")
Expand Down
10 changes: 10 additions & 0 deletions tests/integration/test_rest_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,17 @@ def rest_client(
integration_username,
integration_password,
) as client:
print(f"Session started with ID: {client.session_id}") # noqa: T201
yield client
print(f"Session ended with ID: {client.session_id}") # noqa: T201


@pytest.mark.integration_test
def test_refresh_token(rest_client: RESTClient) -> None:
"""Test refreshing the token."""
session_id = rest_client.session_id
rest_client.refresh_token()
assert session_id != rest_client.session_id


@pytest.mark.integration_test
Expand Down
7 changes: 2 additions & 5 deletions tests/test_rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,8 @@ def rest_client(
httpserver.expect_request(
"/rest/v1/session",
method="POST",
json={
"username": username,
"password": password,
},
).respond_with_json('"my-session-id"')
json={"username": username, "password": password},
).respond_with_json("my-session-id")
httpserver.expect_request("/rest/v1/session", method="DELETE").respond_with_data("")

with RESTClient(httpserver.url_for("").rstrip("/"), username, password) as client:
Expand Down
Loading