Skip to content

Commit 13149be

Browse files
committed
Updates readme and examples with VitreaAuth Fixes #12
1 parent b35f8ca commit 13149be

8 files changed

Lines changed: 70 additions & 32 deletions

File tree

README.md

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,12 @@ pip install dicomtrolley
2424
### Basic example
2525

2626
```python
27-
# Create a logged-in http session
28-
session = VitreaConnection(
29-
"https://server/login").log_in(user,password,realm)
27+
# Create a http session
28+
session = create_session("https://server/login",user,password,realm)
3029

3130
# Use this session to create a trolley using MINT and WADO
3231
trolley = Trolley(searcher=Mint(session, "https://server/mint"),
33-
wado=Wado(session, "https://server/wado"]))
32+
wado=Wado(session, "https://server/wado"))
3433

3534
# find some studies (using MINT)
3635
studies = trolley.find_studies(Query(PatientName='B*'))
@@ -60,11 +59,11 @@ studies = trolley.find_studies(
6059
To include series and instance level information as well, use the `queryLevel` parameter
6160

6261
```python
63-
studies = trolley.find_studies( # find studies series and instances
62+
studies = trolley.find_studies( # find studies series and instances
6463
Query(studyInstanceID='B*',
65-
query_level=QueryLevels.INSTANCE)
64+
query_level=QueryLevels.INSTANCE))
6665

67-
a_series = studies.series[0] # studies now contain series
66+
a_series = studies.series[0] # studies now contain series
6867
an_instance = a_series.instances[0] # and series contain instances
6968
```
7069

dicomtrolley/auth.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,41 @@
11
"""Authentication mechanisms for DICOM servers"""
2-
2+
import requests
33
from requests.auth import AuthBase
44
from requests.models import Request
55

66
from dicomtrolley.exceptions import DICOMTrolleyError
77

88

9+
def create_session(login_url, user, password, realm="DefaultSystemRealm"):
10+
"""Returns a requests Session that logs in to Vitrea automatically as needed.
11+
12+
Facilitates single-line session creation. For readable code
13+
14+
Parameters
15+
----------
16+
login_url: str
17+
Call this url to log in
18+
user: str
19+
username
20+
password: str
21+
password
22+
realm: str, optional
23+
Vitrea realm to pass when logging in. Defaults to 'DefaultSystemRealm'
24+
"""
25+
session = requests.Session()
26+
session.auth = VitreaAuth(
27+
login_url=login_url, user=user, password=password, realm=realm
28+
)
29+
return session
30+
31+
932
class VitreaAuth(AuthBase):
1033
"""Can log in to a server running Vitrea Connection 8.2.0.1
1134
35+
Usage
36+
-----
37+
38+
1239
Raises
1340
------
1441
DICOMTrolleyAuthError

dicomtrolley/servers.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ def __init__(self, login_url):
2020
login_url: str
2121
Full url of login method, including https://
2222
"""
23+
DeprecationWarning(
24+
"VitreaConnection will be removed in future versions. "
25+
"Use dicomtrolley.auth.VitreaAuth instead"
26+
)
2327
self.login_url = login_url
2428

2529
def log_in(self, user, password, realm):

examples/go_shopping.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,18 @@
1313
"""
1414
from os import environ
1515

16+
from dicomtrolley.auth import create_session
1617
from dicomtrolley.core import Query
1718
from dicomtrolley.mint import Mint, QueryLevels
18-
from dicomtrolley.servers import VitreaConnection
1919
from dicomtrolley.trolley import Trolley
2020
from dicomtrolley.wado import Wado
2121

22-
print("logging in")
23-
24-
session = VitreaConnection(environ["LOGIN_URL"]).log_in(
25-
environ["USER"], environ["PASSWORD"], environ["REALM"]
22+
print("Creating session")
23+
session = create_session(
24+
environ["LOGIN_URL"],
25+
environ["USER"],
26+
environ["PASSWORD"],
27+
environ["REALM"],
2628
)
2729

2830
trolley = Trolley(

examples/go_shopping_rad69.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,17 @@
1313
"""
1414
from os import environ
1515

16+
from dicomtrolley.auth import create_session
1617
from dicomtrolley.mint import Mint, MintQuery
1718
from dicomtrolley.rad69 import Rad69
18-
from dicomtrolley.servers import VitreaConnection
1919
from dicomtrolley.trolley import Trolley
2020

21-
print("logging in")
22-
23-
session = VitreaConnection(environ["LOGIN_URL"]).log_in(
24-
environ["USER"], environ["PASSWORD"], environ["REALM"]
21+
print("Creating session")
22+
session = create_session(
23+
environ["LOGIN_URL"],
24+
environ["USER"],
25+
environ["PASSWORD"],
26+
environ["REALM"],
2527
)
2628

2729
trolley = Trolley(

examples/search_for_studies_mint.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,20 @@
1313
from datetime import datetime
1414
from os import environ
1515

16+
from dicomtrolley.auth import create_session
1617
from dicomtrolley.mint import Mint, MintQuery, QueryLevels
17-
from dicomtrolley.servers import VitreaConnection
1818

19-
# Log in to get an authenticated session
20-
session = VitreaConnection(environ["LOGIN_URL"]).log_in(
21-
environ["USER"], environ["PASSWORD"], environ["REALM"]
19+
# Create auto-login session
20+
session = create_session(
21+
environ["LOGIN_URL"],
22+
environ["USER"],
23+
environ["PASSWORD"],
24+
environ["REALM"],
2225
)
2326

2427
# Using mint for search
2528
mint = Mint(session, environ["MINT_URL"])
2629

27-
2830
print("Quick search for some studies")
2931
studies = mint.find_studies(
3032
MintQuery(PatientName="B*", include_fields=["PatientBirthDate"])

examples/use_wado_only.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,16 @@
1111
"""
1212
from os import environ
1313

14+
from dicomtrolley.auth import create_session
1415
from dicomtrolley.core import InstanceReference
15-
from dicomtrolley.servers import VitreaConnection
1616
from dicomtrolley.wado import Wado
1717

18-
19-
# log in
20-
session = VitreaConnection(environ["LOGIN_URL"]).log_in(
21-
environ["USER"], environ["PASSWORD"], environ["REALM"]
18+
# Create auto-login session
19+
session = create_session(
20+
environ["LOGIN_URL"],
21+
environ["USER"],
22+
environ["PASSWORD"],
23+
environ["REALM"],
2224
)
2325

2426
wado = Wado(session, environ["WADO_URL"])

tests/test_storage.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@
1111
[
1212
(
1313
quick_dataset(
14-
StudyInstanceUID="A", SeriesInstanceUID="B", SOPInstanceUID="C"
14+
StudyInstanceUID="1", SeriesInstanceUID="2", SOPInstanceUID="3"
1515
),
16-
"A/B/C",
16+
"1/2/3",
1717
),
1818
(
19-
quick_dataset(StudyInstanceUID="A", SeriesInstanceUID="B"),
20-
"A/B/unknown",
19+
quick_dataset(StudyInstanceUID="1", SeriesInstanceUID="2"),
20+
"1/2/unknown",
2121
),
2222
(quick_dataset(), "unknown/unknown/unknown"),
2323
],

0 commit comments

Comments
 (0)