Skip to content

Commit

Permalink
[PT-5377] Support deep copy for auth (#635)
Browse files Browse the repository at this point in the history
* Support deep copy in Up42Auth and finalize job query parameter names

* Bump version
  • Loading branch information
javidq authored Jun 17, 2024
1 parent ffd2f57 commit d64ef41
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 10 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ You can check your current version with the following command:
```

For more information, see [UP42 Python package description](https://pypi.org/project/up42-py/).
## 1.0.4a21

**Jun 17, 2024**

- Support deep copy in `Up42Auth` to be compliant with pystac client.
- Align processing job query parameter names.

## 1.0.4a20

**Jun 17, 2024**
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 = "up42-py"
version = "1.0.4a20"
version = "1.0.4a21"
description = "Python SDK for UP42, the geospatial marketplace and developer platform."
authors = ["UP42 GmbH <[email protected]>"]
license = "https://github.com/up42/up42-py/blob/master/LICENSE"
Expand Down
9 changes: 7 additions & 2 deletions tests/http/test_oauth.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import copy
import dataclasses
import random
import time
Expand Down Expand Up @@ -65,7 +66,6 @@ def test_should_fetch_token_when_created(self):
up42_auth = oauth.Up42Auth(retrieve=retrieve, token_settings=token_settings)
up42_auth(mock_request)
assert mock_request.headers["Authorization"] == f"Bearer {TOKEN_VALUE}"
assert up42_auth.token.access_token == TOKEN_VALUE
retrieve.assert_called_once()
assert TOKEN_URL, HTTP_TIMEOUT == retrieve.call_args.args[1:]

Expand All @@ -77,10 +77,15 @@ def test_should_fetch_token_when_expired(self):
up42_auth(mock_request)

assert mock_request.headers["Authorization"] == f"Bearer {second_token}"
assert up42_auth.token.access_token == second_token
assert TOKEN_URL, HTTP_TIMEOUT == retrieve.call_args.args[1:]
assert retrieve.call_count == 2

def test_should_deepcopy_itself(self):
retrieve = mock.MagicMock(return_value=TOKEN_VALUE)
up42_auth = oauth.Up42Auth(retrieve=retrieve, token_settings=token_settings)
assert copy.deepcopy(up42_auth) == up42_auth
retrieve.assert_called_once()


class TestDetectSettings:
def test_should_detect_account_credentials(self):
Expand Down
4 changes: 2 additions & 2 deletions tests/test_processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -377,9 +377,9 @@ def test_should_get_all_jobs(
):
query_params: dict[str, Any] = {}
if process_id:
query_params["processID"] = process_id
query_params["processId"] = process_id
if workspace_id:
query_params["workspaceID"] = workspace_id
query_params["workspaceId"] = workspace_id
if status:
query_params["status"] = [entry.value for entry in status]
if min_duration:
Expand Down
12 changes: 9 additions & 3 deletions up42/http/oauth.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def __init__(
self._lock = threading.Lock()

def __call__(self, request: requests.PreparedRequest) -> requests.PreparedRequest:
request.headers["Authorization"] = f"Bearer {self.token.access_token}"
request.headers["Authorization"] = f"Bearer {self._access_token}"
return request

def _fetch_token(self):
Expand All @@ -75,11 +75,17 @@ def _fetch_token(self):
return Token(access_token=access_token, expires_on=expires_on)

@property
def token(self) -> Token:
def _access_token(self) -> Token:
with self._lock:
if self._token.has_expired:
self._token = self._fetch_token()
return self._token
return self._token.access_token

def __deepcopy__(self, memo: dict):
# Pystac client deep copies the request modifier this class is used for.
# We return the same instance to share authentication settings between
# possible parallel calls to avoid multiple token generation.
return self


def detect_settings(
Expand Down
4 changes: 2 additions & 2 deletions up42/processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,8 @@ def all(
query_params = {
key: str(value)
for key, value in {
"workspaceID": workspace_id,
"processID": process_id,
"workspaceId": workspace_id,
"processId": process_id,
"status": [entry.value for entry in status] if status else None,
"minDuration": min_duration,
"maxDuration": max_duration,
Expand Down

0 comments on commit d64ef41

Please sign in to comment.