Skip to content

Commit 7fb7857

Browse files
committed
Copy session_utils.create_session_from_gcp_credentials() to ext.gcp.session_utils and add deprecation warning to the original.
1 parent 0a3e9c6 commit 7fb7857

File tree

3 files changed

+50
-11
lines changed

3 files changed

+50
-11
lines changed

docs/package.rst

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,23 @@ dicomweb\_client.session_utils module
4545
:show-inheritance:
4646

4747
dicomweb\_client.uri module
48-
+++++++++++++++++++++++++++++++++++++
48+
+++++++++++++++++++++++++++
4949

5050
.. automodule:: dicomweb_client.uri
5151
:members:
5252
:undoc-members:
5353
:show-inheritance:
5454

55+
dicomweb\_client.ext.gcp.session_utils module
56+
+++++++++++++++++++++++++++++++++++++++++++++
57+
58+
.. automodule:: dicomweb_client.ext.gcp.session_utils
59+
:members:
60+
:undoc-members:
61+
:show-inheritance:
62+
5563
dicomweb\_client.ext.gcp.uri module
56-
+++++++++++++++++++++++++++++++++++++
64+
+++++++++++++++++++++++++++++++++++
5765

5866
.. automodule:: dicomweb_client.ext.gcp.uri
5967
:members:
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
"""Session management utilities for Google Cloud Platform (GCP)."""
2+
from typing import Optional, Any
3+
4+
import google.auth
5+
from google.auth.transport import requests as google_requests
6+
import requests
7+
8+
9+
def create_session_from_gcp_credentials(
10+
google_credentials: Optional[Any] = None
11+
) -> requests.Session:
12+
"""Creates an authorized session for Google Cloud Platform.
13+
14+
Parameters
15+
----------
16+
google_credentials: Any
17+
Google Cloud credentials.
18+
(see https://cloud.google.com/docs/authentication/production
19+
for more information on Google Cloud authentication).
20+
If not set, will be initialized to ``google.auth.default()``.
21+
22+
Returns
23+
-------
24+
requests.Session
25+
Google Cloud authorized session.
26+
"""
27+
if google_credentials is None:
28+
google_credentials, _ = google.auth.default(
29+
scopes=['https://www.googleapis.com/auth/cloud-platform']
30+
)
31+
return google_requests.AuthorizedSession(google_credentials)

src/dicomweb_client/session_utils.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import logging
22
import os
33
from typing import Optional, Any
4+
import warnings
45

56
import requests
67

@@ -128,20 +129,19 @@ def create_session_from_gcp_credentials(
128129
-------
129130
requests.Session
130131
Google cloud authorized session
131-
132132
"""
133+
warnings.warn(
134+
'This method shall be deprecated in a future release. Prefer using the '
135+
'underlying implementation directly, now moved to '
136+
'`dicomweb_client.ext.gcp.session_utils`.',
137+
DeprecationWarning)
133138
try:
134-
from google.auth.transport import requests as google_requests
135-
if google_credentials is None:
136-
import google.auth
137-
google_credentials, _ = google.auth.default(
138-
scopes=['https://www.googleapis.com/auth/cloud-platform']
139-
)
139+
import dicomweb_client.ext.gcp.session_utils as gcp_session_utils
140140
except ImportError:
141141
raise ImportError(
142142
'The dicomweb-client package needs to be installed with the '
143143
'"gcp" extra requirements to support interaction with the '
144144
'Google Cloud Healthcare API: pip install dicomweb-client[gcp]'
145145
)
146-
logger.debug('initialize, authenticate and authorize HTTP session')
147-
return google_requests.AuthorizedSession(google_credentials)
146+
return gcp_session_utils.create_session_from_gcp_credentials(
147+
google_credentials)

0 commit comments

Comments
 (0)