Skip to content

Commit d74becc

Browse files
authored
Merge pull request #101 from RomanSemkin/using_cookies_for_sdk
ability to use cookies for sdk was added
2 parents ab469b2 + 610ef45 commit d74becc

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

privx_api/base.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ def __init__(
7070
oauth_client_id: str,
7171
oauth_client_secret: str,
7272
re_auth_margin: int = 3,
73+
use_cookies=False,
7374
) -> None:
7475
self._access_token = ""
7576
self._oauth_client_id = oauth_client_id
@@ -84,6 +85,8 @@ def __init__(
8485
self._re_auth_deadline = None
8586
self._access_token_age = None
8687
self._re_auth_margin = re_auth_margin
88+
self._use_cookies = use_cookies
89+
self._cookies = None
8790

8891
def _authenticate(self, username: str, password: str) -> None:
8992
# saving the creds for the re-auth purposes
@@ -122,6 +125,11 @@ def _authenticate(self, username: str, password: str) -> None:
122125
except (JSONDecodeError, TypeError) as e:
123126
raise InternalAPIException(e) from e
124127

128+
# save and never change cookie`s value,
129+
# in order to communicate with the same node
130+
if self._cookies is None and self._use_cookies:
131+
self._cookies = response.getheader("Set-Cookie")
132+
125133
# privx response includes access token age in seconds
126134
self._access_token_age = data.get("expires_in")
127135
self._re_auth_deadline = (
@@ -174,6 +182,8 @@ def _get_headers(self) -> dict:
174182
}
175183
if self._access_token:
176184
headers["Authorization"] = "Bearer {}".format(self._access_token)
185+
if self._cookies:
186+
headers["Cookie"] = self._cookies
177187
return headers
178188

179189
def _get_search_params(self, **kwargs: Union[str, int]) -> dict:

0 commit comments

Comments
 (0)