Skip to content

Commit 7de768e

Browse files
committed
Add documentation for new client constructor
1 parent 3b5f462 commit 7de768e

File tree

3 files changed

+93
-27
lines changed

3 files changed

+93
-27
lines changed

docs/package.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,10 @@ dicomweb\_client.log module
4444
:undoc-members:
4545
:show-inheritance:
4646

47+
dicomweb\_client.session_utils module
48+
+++++++++++++++++++++++++++++++++++++
49+
50+
.. automodule:: dicomweb_client.session_utils
51+
:members:
52+
:undoc-members:
53+
:show-inheritance:

docs/usage.rst

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ To interact with a publicly accessible server, you only need to provide the ``ur
1616
1717
from dicomweb_client.api import DICOMwebClient
1818
19-
client = DICOMwebClient("https://mydicomwebserver.com")
19+
client = DICOMwebClient(url="https://mydicomwebserver.com")
2020
2121
2222
Some servers expose the different DICOMweb RESTful services using different path prefixes.
@@ -41,25 +41,33 @@ To interact with servers requiring authentication, ``DICOMwebClient`` accepts ar
4141
4242
from requests.auth import HTTPBasicAuth
4343
from dicomweb_client.api import DICOMwebClient
44+
from dicomweb_client.session_utils import create_session_from_auth
45+
46+
auth=HTTPBasicAuth('myusername', 'mypassword')
47+
session = create_session_from_auth(auth)
4448
4549
client = DICOMwebClient(
4650
url="https://mydicomwebserver.com",
47-
auth=HTTPBasicAuth('myusername', 'mypassword')
51+
session=session
4852
)
4953
5054
To simplify usage for ``HTTPBasicAuth``, you may also directly provide a username and password using the corresponding arguments.
5155

5256
.. code-block:: python
5357
5458
from dicomweb_client.api import DICOMwebClient
59+
from dicomweb_client.session_utils import create_session_from_user_pass
60+
61+
session = create_session_from_user_pass(
62+
username='myusername',
63+
password='mypassword'
64+
)
5565
5666
client = DICOMwebClient(
5767
url="https://mydicomwebserver.com",
58-
username="myusername",
59-
password="mypassword"
68+
session=session
6069
)
6170
62-
6371
To interact with servers supporting token-based authorization, you can provide the access token using the ``headers`` argument (the header will be included in every client request message).
6472

6573
.. code-block:: python
@@ -78,13 +86,38 @@ To interact with servers requiring certificate-based authentication, you can pro
7886
.. code-block:: python
7987
8088
from dicomweb_client.api import DICOMwebClient
89+
from dicomweb_client.session_utils import (
90+
create_session,
91+
add_certs_to_session
92+
)
8193
82-
client = DICOMwebClient(
83-
url="https://mydicomwebserver.com",
94+
session = create_session()
95+
session = add_certs_to_session(
96+
session=session,
8497
ca_bundle="/path/to/ca.crt",
8598
cert="/path/to/cert.pem"
8699
)
87100
101+
client = DICOMwebClient(url="https://mydicomwebserver.com")
102+
103+
104+
To interact with a server of the Google Healthcare API requiring OpenID Connect based authentication and authorization provide a session authenticated using the Google Cloud Platform (GCP) credentials.
105+
See `GCP documentation <https://cloud.google.com/docs/authentication/production>`_ for details.
106+
107+
Note that GCP authentication requires installation of the package distribution with the ``gcp`` extra requirements: ``$ pip install dicomweb-client[gcp]``.
108+
109+
.. code-block:: python
110+
111+
from dicomweb_client.api import DICOMwebClient
112+
from dicomweb_client.session_utils import create_session_from_gcp_credentials
113+
114+
session = create_session_from_gcp_credentials()
115+
116+
client = DICOMwebClient(
117+
url="https://mydicomwebserver.com",
118+
session=session
119+
)
120+
88121
89122
.. _storeinstances:
90123

src/dicomweb_client/session_utils.py

Lines changed: 46 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,56 +7,79 @@
77
logger = logging.getLogger(__name__)
88

99

10+
def create_session() -> requests.Session:
11+
'''Creates an unauthorized session.
12+
13+
Returns
14+
-------
15+
requests.Session
16+
unauthorized session
17+
18+
'''
19+
logger.debug('initialize HTTP session')
20+
return requests.Session()
21+
22+
1023
def create_session_from_auth(
11-
auth: [requests.auth.AuthBase]) -> requests.Session:
12-
'''Creates a session from a gicen AuthBase object
24+
auth: requests.auth.AuthBase
25+
) -> requests.Session:
26+
'''Creates a session from a gicen AuthBase object.
27+
1328
Parameters
1429
----------
15-
auth: requests.auth.AuthBase, optional
30+
auth: requests.auth.AuthBase
1631
an implementation of `requests.auth.AuthBase` to be used for
1732
authentication with services
1833
1934
Returns
2035
-------
2136
requests.Session
22-
authenticated session
37+
authorized session
2338
2439
'''
2540
logger.debug('initialize HTTP session')
2641
session = requests.Session()
42+
logger.debug('authenticate HTTP session')
2743
session.auth = auth
2844
return session
2945

3046

31-
def create_session_from_user_pass(username: [str],
32-
password: [str]) -> requests.Session:
33-
'''Creates a session from a given username and password
47+
def create_session_from_user_pass(
48+
username: str,
49+
password: str
50+
) -> requests.Session:
51+
'''Creates a session from a given username and password.
52+
3453
Parameters
3554
----------
36-
username: str,
55+
username: str
3756
username for authentication with services
38-
password: str,
57+
password: str
3958
password for authentication with services
4059
4160
Returns
4261
-------
4362
requests.Session
44-
authenticated session
63+
authorized session
4564
4665
'''
4766
logger.debug('initialize HTTP session')
4867
session = requests.Session()
68+
logger.debug('authenticate and authorize HTTP session')
4969
session.auth = (username, password)
5070
return session
5171

5272

53-
def add_certs_to_session(session: [requests.Session],
54-
ca_bundle: Optional[str] = None,
55-
cert: Optional[str] = None) -> requests.Session:
56-
'''Adds ca_bundle and certificate to an existing session
73+
def add_certs_to_session(
74+
session: requests.Session,
75+
ca_bundle: Optional[str] = None,
76+
cert: Optional[str] = None
77+
) -> requests.Session:
78+
'''Adds CA bundle and certificate to an existing session.
79+
5780
Parameters
5881
----------
59-
session: requests.Session,
82+
session: requests.Session
6083
input session
6184
ca_bundle: str, optional
6285
path to CA bundle file
@@ -89,15 +112,17 @@ def add_certs_to_session(session: [requests.Session],
89112

90113

91114
def create_session_from_gcp_credentials(
92-
google_credentials: [Any] = None) -> requests.Session:
93-
'''Creates a session for Google Cloud Platform
115+
google_credentials: Optional[Any] = None
116+
) -> requests.Session:
117+
'''Creates an authorized session for Google Cloud Platform.
118+
94119
Parameters
95120
----------
96121
google_credentials: Any
97122
Google cloud credentials.
98123
(see https://cloud.google.com/docs/authentication/production
99124
for more information on Google cloud authentication).
100-
If not set, will be initialized to google.auth.default()
125+
If not set, will be initialized to ``google.auth.default()``
101126
102127
Returns
103128
-------
@@ -110,12 +135,13 @@ def create_session_from_gcp_credentials(
110135
if google_credentials is None:
111136
import google.auth
112137
google_credentials, _ = google.auth.default(
113-
scopes=['https://www.googleapis.com/auth/cloud-platform'])
138+
scopes=['https://www.googleapis.com/auth/cloud-platform']
139+
)
114140
except ImportError:
115141
raise ImportError(
116142
'The dicomweb-client package needs to be installed with the '
117143
'"gcp" extra requirements to support interaction with the '
118144
'Google Cloud Healthcare API: pip install dicomweb-client[gcp]'
119145
)
120-
logger.debug('initialize Google AuthorizedSession')
146+
logger.debug('initialize, authenticate and authorize HTTP session')
121147
return google_requests.AuthorizedSession(google_credentials)

0 commit comments

Comments
 (0)