Skip to content

Commit

Permalink
feat(rest): Added a new method to refresh the REST session token (#1190)
Browse files Browse the repository at this point in the history
* feat(rest): Added a new `RESTClient` method to refresh the session token

* Parse token response

* Revert "Parse token response"

This reverts commit b575488.

* Explicitly use application/json

* Revert "Explicitly use application/json"

This reverts commit 69a3804.

* Get token from response in refresh token request

* Restore login at initialization time

* Update test_rest_client.py

* Update Added-20240921-220814.yaml
  • Loading branch information
edgarrmondragon authored Oct 12, 2024
1 parent 4c00c7a commit c033062
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 10 deletions.
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"
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
8 changes: 8 additions & 0 deletions tests/integration/test_rest_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ def rest_client(
yield client


@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
def test_get_surveys(rest_client: RESTClient, survey_id: int) -> None:
"""Test getting surveys."""
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

0 comments on commit c033062

Please sign in to comment.