Skip to content

Commit

Permalink
Merge pull request #34 from Celeo/refresh_token_headers
Browse files Browse the repository at this point in the history
Refresh token headers when refresh -> access tkn
  • Loading branch information
Celeo authored Dec 20, 2024
2 parents 320172c + a8a8b3c commit 6121bcd
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 24 deletions.
23 changes: 5 additions & 18 deletions preston/preston.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ def __init__(self, **kwargs: Any) -> None:
self._kwargs = kwargs
if not kwargs.get("no_update_token", False):
self._try_refresh_access_token()
self._update_access_token_header()

def copy(self) -> "Preston":
"""Creates a copy of this Preston object.
Expand Down Expand Up @@ -130,6 +129,8 @@ def _try_refresh_access_token(self) -> None:
then the refresh token is in the API call to get a new access token. If
successful, this instance is modified in-place with that new access token.
Also updates the `requests` session with the new header.
Args:
None
Expand All @@ -143,6 +144,9 @@ def _try_refresh_access_token(self) -> None:
self.access_expiration,
) = self._get_access_from_refresh()
self.access_expiration = time.time() + self.access_expiration
self.session.headers.update(
{"Authorization": f"Bearer {self.access_token}"}
)

def _is_access_token_expired(self) -> bool:
"""Returns true if the stored access token has expired.
Expand Down Expand Up @@ -204,23 +208,6 @@ def authenticate(self, code: str) -> "Preston":
new_kwargs["refresh_token"] = response_data["refresh_token"]
return Preston(**new_kwargs)

def _update_access_token_header(self) -> None:
"""Updates the requests session with the access token header.
This method does nothing if this instance does not have a
stored access token.
Args:
None
Returns:
None
"""
if self.access_token:
self.session.headers.update(
{"Authorization": f"Bearer {self.access_token}"}
)

def _get_spec(self) -> dict:
"""Fetches the OpenAPI spec from the server.
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "preston"
version = "4.6.0"
version = "4.7.0"
description = "EVE ESI API access tool"
authors = ["Celeo <[email protected]>"]
license = "MIT"
Expand Down
21 changes: 16 additions & 5 deletions tests/test_preston.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,6 @@ def test_get_authorize_url(sample):
expected_multiple_scope = "https://login.eveonline.com/oauth/authorize?response_type=code&redirect_uri=4&client_id=2&scope=scope%20scope1"
assert sample_multiple_scopes.get_authorize_url() == expected_multiple_scope

def test_update_access_token_header(sample):
assert "Authorization" not in sample.session.headers
sample._update_access_token_header()
assert sample.session.headers["Authorization"] == "Bearer 6"


def test_insert_vars(empty):
data = dict(foo="bar", bar="baz")
Expand All @@ -114,3 +109,19 @@ def test_insert_vars_missing(empty):

def test_whoami_unauthorized(empty):
assert empty.whoami() == {}


def test_try_refresh_access_token_empty(empty):
empty._try_refresh_access_token()
assert empty.refresh_token is None
assert empty.access_token is None


def test_try_refresh_access_token_has(empty):
empty.refresh_token = 'abc123'
empty.access_token = None
empty._is_access_token_expired = lambda: False
empty.access_expiration = 1.0
empty._get_access_from_refresh = lambda: ('def', 456)
empty._try_refresh_access_token()
assert empty.access_token == 'def'

0 comments on commit 6121bcd

Please sign in to comment.