14
14
Dict ,
15
15
Iterator ,
16
16
List ,
17
+ Mapping ,
17
18
Optional ,
18
19
Set ,
19
20
Sequence ,
@@ -857,7 +858,9 @@ def _build_accept_header_field_value(
857
858
def _build_multipart_accept_header_field_value (
858
859
cls ,
859
860
media_types : Union [Tuple [Union [str , Tuple [str , str ]], ...], None ],
860
- supported_media_types : Union [Dict [str , str ], Set [str ]]
861
+ supported_media_types : Union [
862
+ Mapping [str , Union [str , Tuple [str , ...]]], Set [str ]
863
+ ]
861
864
) -> str :
862
865
"""Build an accept header field value for a multipart request message.
863
866
@@ -866,7 +869,9 @@ def _build_multipart_accept_header_field_value(
866
869
media_types: Union[Tuple[Union[str, Tuple[str, str]], ...], None]
867
870
Acceptable media types and optionally the UIDs of the corresponding
868
871
transfer syntaxes
869
- supported_media_types: Union[Dict[str, str], Set[str]]
872
+ supported_media_types: Union[
873
+ Mapping[str, Union[str, Tuple[str, ...]]], Set[str]
874
+ ]
870
875
Set of supported media types or mapping of transfer syntaxes
871
876
to their corresponding media types
872
877
@@ -894,7 +899,13 @@ def _build_multipart_accept_header_field_value(
894
899
cls ._assert_media_type_is_valid (media_type )
895
900
field_value = f'multipart/related; type="{ media_type } "'
896
901
if isinstance (supported_media_types , dict ):
897
- if media_type not in supported_media_types .values ():
902
+ media_type_in_supported_media_types = any (
903
+ media_type == supported_media_types
904
+ if isinstance (supported_media_types , str )
905
+ else media_type in supported_media_types
906
+ for supported_media_types in supported_media_types .values ()
907
+ )
908
+ if not media_type_in_supported_media_types :
898
909
if not (media_type .endswith ('/*' ) or
899
910
media_type .endswith ('/' )):
900
911
raise ValueError (
@@ -908,14 +919,23 @@ def _build_multipart_accept_header_field_value(
908
919
f'Transfer syntax "{ transfer_syntax_uid } " '
909
920
'is not supported for requested resource.'
910
921
)
911
- expected_media_type = supported_media_types [
922
+ expected_media_types = supported_media_types [
912
923
transfer_syntax_uid
913
924
]
914
- if expected_media_type != media_type :
915
- have_same_type = (
916
- cls ._parse_media_type (media_type )[0 ] ==
917
- cls ._parse_media_type (expected_media_type )[0 ]
925
+ if not isinstance (expected_media_types , tuple ):
926
+ expected_media_types = (expected_media_types , )
927
+ if media_type not in expected_media_types :
928
+ have_same_type = next (
929
+ (
930
+ cls ._same_media_type (
931
+ media_type , expected_media_type
932
+ )
933
+ for expected_media_type
934
+ in expected_media_types
935
+ ),
936
+ False
918
937
)
938
+
919
939
if (have_same_type and
920
940
(media_type .endswith ('/*' ) or
921
941
media_type .endswith ('/' ))):
@@ -1057,7 +1077,7 @@ def _http_get_multipart(
1057
1077
default_media_type = '*/*'
1058
1078
supported_media_types = {
1059
1079
'1.2.840.10008.1.2.1' : 'application/octet-stream' ,
1060
- '1.2.840.10008.1.2.5' : 'image/x-dicom-rle' ,
1080
+ '1.2.840.10008.1.2.5' : ( 'image/x-dicom-rle' , 'image/dicom-rle' ) ,
1061
1081
'1.2.840.10008.1.2.4.50' : 'image/jpeg' ,
1062
1082
'1.2.840.10008.1.2.4.51' : 'image/jpeg' ,
1063
1083
'1.2.840.10008.1.2.4.57' : 'image/jpeg' ,
@@ -1181,7 +1201,7 @@ def _http_get_multipart_image(
1181
1201
""" # noqa: E501
1182
1202
headers = {}
1183
1203
supported_media_types = {
1184
- '1.2.840.10008.1.2.5' : 'image/x-dicom-rle' ,
1204
+ '1.2.840.10008.1.2.5' : ( 'image/x-dicom-rle' , 'image/dicom-rle' ) ,
1185
1205
'1.2.840.10008.1.2.4.50' : 'image/jpeg' ,
1186
1206
'1.2.840.10008.1.2.4.51' : 'image/jpeg' ,
1187
1207
'1.2.840.10008.1.2.4.57' : 'image/jpeg' ,
@@ -1653,6 +1673,27 @@ def search_for_studies(
1653
1673
get_remaining = get_remaining
1654
1674
)
1655
1675
1676
+ @classmethod
1677
+ def _same_media_type (cls , first : str , second : str ) -> bool :
1678
+ """Check if two media types have the same type.
1679
+
1680
+ Parameters
1681
+ ----------
1682
+ first: str
1683
+ First media type
1684
+ second: str
1685
+ Second media type
1686
+
1687
+ Returns
1688
+ -------
1689
+ bool
1690
+ Whether media types have the same type
1691
+
1692
+ """
1693
+ return (
1694
+ cls ._parse_media_type (first )[0 ] == cls ._parse_media_type (second )[0 ]
1695
+ )
1696
+
1656
1697
@classmethod
1657
1698
def _parse_media_type (cls , media_type : str ) -> Tuple [str , str ]:
1658
1699
"""Parse media type and extract its type and subtype.
0 commit comments