Skip to content

Commit 8462e6c

Browse files
authored
Merge pull request #186 from statisticsnorway/add-dapla-lab-email
Fix 'fetch_email_from_credentials' for Dapla Lab
2 parents 8153a3a + 0a06d35 commit 8462e6c

File tree

5 files changed

+39
-34
lines changed

5 files changed

+39
-34
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "dapla-toolbelt"
3-
version = "3.1.2"
3+
version = "3.2.0"
44
description = "Dapla Toolbelt"
55
authors = ["Dapla Developers <dapla-platform-developers@ssb.no>"]
66
license = "MIT"

src/dapla/auth.py

Lines changed: 7 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
from collections.abc import Sequence
66
from datetime import datetime
77
from datetime import timedelta
8-
from enum import Enum
98
from functools import lru_cache
109
from functools import partial
1110
from typing import Any
@@ -19,6 +18,10 @@
1918
from IPython.display import display
2019
from jupyterhub.services.auth import HubAuth
2120

21+
from dapla.const import DaplaEnvironment
22+
from dapla.const import DaplaRegion
23+
from dapla.const import DaplaService
24+
2225
logger = logging.getLogger(__name__)
2326

2427
# Refresh window was modified in: https://github.com/googleapis/google-auth-library-python/commit/c6af1d692b43833baca978948376739547cf685a
@@ -29,33 +32,6 @@
2932
google.auth._helpers.REFRESH_THRESHOLD = timedelta(seconds=20)
3033

3134

32-
class DaplaEnvironment(Enum):
33-
"""Represents the 'DAPLA_ENVIRONMENT' environment variable."""
34-
35-
DEV = "DEV"
36-
STAGING = "STAGING"
37-
TEST = "TEST"
38-
PROD = "PROD"
39-
40-
41-
class DaplaService(Enum):
42-
"""Represents the 'DAPLA_SERVICE' environment variable."""
43-
44-
JUPYTERLAB = "JUPYTERLAB"
45-
VS_CODE = "VS_CODE"
46-
R_STUDIO = "R_STUDIO"
47-
CLOUD_RUN = "CLOUD_RUN"
48-
49-
50-
class DaplaRegion(Enum):
51-
"""Represents the 'DAPLA_REGION' environment variable."""
52-
53-
ON_PREM = "ON_PREM"
54-
DAPLA_LAB = "DAPLA_LAB"
55-
BIP = "BIP"
56-
CLOUD_RUN = "CLOUD_RUN"
57-
58-
5935
class AuthClient:
6036
"""Client for retrieving authentication information."""
6137

@@ -347,6 +323,9 @@ def fetch_personal_token() -> str:
347323
@lru_cache(maxsize=1)
348324
def fetch_email_from_credentials() -> Optional[str]:
349325
"""Retrieves an e-mail based on current Google Credentials. Potentially makes a Google API call."""
326+
if os.getenv("DAPLA_REGION") == str(DaplaRegion.DAPLA_LAB):
327+
return os.getenv("DAPLA_USER")
328+
350329
credentials = AuthClient.fetch_google_credentials()
351330
response = requests.get(
352331
url=f"https://oauth2.googleapis.com/tokeninfo?access_token={credentials.token}"

src/dapla/const.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from enum import Enum
2+
3+
4+
class DaplaEnvironment(Enum):
5+
"""Represents the 'DAPLA_ENVIRONMENT' environment variable."""
6+
7+
DEV = "DEV"
8+
STAGING = "STAGING"
9+
TEST = "TEST"
10+
PROD = "PROD"
11+
12+
13+
class DaplaService(Enum):
14+
"""Represents the 'DAPLA_SERVICE' environment variable."""
15+
16+
JUPYTERLAB = "JUPYTERLAB"
17+
VS_CODE = "VS_CODE"
18+
R_STUDIO = "R_STUDIO"
19+
CLOUD_RUN = "CLOUD_RUN"
20+
21+
22+
class DaplaRegion(Enum):
23+
"""Represents the 'DAPLA_REGION' environment variable."""
24+
25+
ON_PREM = "ON_PREM"
26+
DAPLA_LAB = "DAPLA_LAB"
27+
BIP = "BIP"
28+
CLOUD_RUN = "CLOUD_RUN"

src/dapla/doctor.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from gcsfs.retry import HttpError
99

1010
from dapla.auth import AuthClient
11-
from dapla.auth import DaplaRegion
11+
from dapla.const import DaplaRegion
1212

1313
logger = logging.getLogger(__name__)
1414

tests/test_pubsub.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import pytest
77
from google.api_core.exceptions import NotFound
88
from google.auth.exceptions import DefaultCredentialsError
9-
from google.cloud import pubsub_v1
9+
from google.cloud.pubsub_v1.publisher.futures import Future
1010

1111
import dapla.pubsub
1212
from dapla.pubsub import _extract_env
@@ -55,9 +55,7 @@ def test_publish_gcs_objects_to_pubsub(self, mock_list: Mock) -> None:
5555
)
5656

5757
def test_get_callback(self) -> None:
58-
publish_future = MagicMock(
59-
side_effect=pubsub_v1.publisher.futures.Future.result
60-
)
58+
publish_future = MagicMock(side_effect=Future.result)
6159
# Create a callback function using the _get_callback helper function
6260
callback = _get_callback(publish_future, "blob_name", timeout=1)
6361

0 commit comments

Comments
 (0)