14
14
from azure .core .pipeline .transport import RequestsTransport
15
15
from azure .servicebus import ServiceBusSharedKeyCredential
16
16
from msrest .serialization import Model
17
+ from msrest .exceptions import ValidationError
17
18
18
19
from .._common .constants import JWT_TOKEN_SCOPE
19
20
from .._common .utils import parse_conn_str
@@ -44,13 +45,13 @@ def _handle_response_error():
44
45
raise new_response_error
45
46
46
47
47
- def _convert_xml_to_object (queue_name , et , clazz ):
48
+ def _convert_xml_to_object (queue_name , et , class_ ):
48
49
# type: (str, Union[Element, ElementTree], Type[Model]) -> Union[QueueDescription, QueueRuntimeInfo]
49
50
content_ele = cast (ElementTree , et ).find (constants .CONTENT_TAG )
50
51
if not content_ele :
51
52
raise ResourceNotFoundError ("Queue '{}' does not exist" .format (queue_name ))
52
53
qc_ele = content_ele .find (constants .QUEUE_DESCRIPTION_TAG )
53
- obj = clazz .deserialize (qc_ele )
54
+ obj = class_ .deserialize (qc_ele )
54
55
obj .queue_name = queue_name
55
56
return obj
56
57
@@ -116,7 +117,7 @@ def from_connection_string(cls, connection_string, **kwargs):
116
117
endpoint = endpoint [endpoint .index ("//" )+ 2 :]
117
118
return cls (endpoint , ServiceBusSharedKeyCredential (shared_access_key_name , shared_access_key ))
118
119
119
- def _get_queue_object (self , queue_name , clazz ):
120
+ def _get_queue_object (self , queue_name , class_ ):
120
121
# type: (str, Type[Model]) -> Union[QueueDescription, QueueRuntimeInfo]
121
122
122
123
if not queue_name :
@@ -127,10 +128,10 @@ def _get_queue_object(self, queue_name, clazz):
127
128
ElementTree ,
128
129
self ._impl .queue .get (queue_name , enrich = False , api_version = constants .API_VERSION )
129
130
)
130
- return _convert_xml_to_object (queue_name , et , clazz )
131
+ return _convert_xml_to_object (queue_name , et , class_ )
131
132
132
- def _list_queues (self , skip , max_count , clazz ):
133
- # type: (int, int, Type[Model]) -> Union[List[QueueDescription], List[QueueRuntimeInfo]]
133
+ def _list_queues (self , skip , max_count , class_ ):
134
+ # type: (int, int, Type[Model]) -> Union[List[QueueDescription], List[QueueRuntimeInfo])
134
135
with _handle_response_error ():
135
136
et = cast (
136
137
ElementTree ,
@@ -145,7 +146,7 @@ def _list_queues(self, skip, max_count, clazz):
145
146
queue_description = _convert_xml_to_object (
146
147
entity_name , # type: ignore
147
148
cast (Element , entry ),
148
- clazz
149
+ class_
149
150
)
150
151
queues .append (queue_description )
151
152
return queues
@@ -169,36 +170,40 @@ def get_queue_runtime_info(self, queue_name):
169
170
def create_queue (self , queue ):
170
171
# type: (Union[str, QueueDescription]) -> QueueDescription
171
172
"""Create a queue
172
-
173
+
173
174
:param queue: The queue name or a `QueueDescription` instance. When it's a str, it will be the name
174
175
of the created queue. Other properties of the created queue will have default values decided by the
175
176
ServiceBus. Use a `QueueDesceiption` if you want to set queue properties other than the queue name.
176
177
:type queue: Union[str, QueueDescription].
177
178
:returns: `QueueDescription` returned from ServiceBus.
178
179
"""
179
-
180
- try : # queue is a QueueDescription
181
- queue_name = queue .queue_name
180
+ queue_name = None
181
+ try :
182
+ queue_name = queue .queue_name # type: ignore
182
183
to_create = copy (queue )
183
- to_create .queue_name = None
184
- except AttributeError : # str expected. But if not str, it might work and might not work.
184
+ to_create .queue_name = None
185
+ except AttributeError :
185
186
queue_name = queue
186
- to_create = QueueDescription ()
187
- if queue_name is None :
188
- raise ValueError ("queue should be a non-empty str or a QueueDescription with non-empty queue_name" )
187
+ to_create = QueueDescription () # Use an empty queue description.
189
188
190
189
create_entity_body = CreateQueueBody (
191
190
content = CreateQueueBodyContent (
192
191
queue_description = to_create ,
193
192
)
194
193
)
195
194
request_body = create_entity_body .serialize (is_xml = True )
195
+ try :
196
+ with _handle_response_error ():
197
+ et = cast (
198
+ ElementTree ,
199
+ self ._impl .queue .put (queue_name , request_body , api_version = constants .API_VERSION )
200
+ )
201
+ except ValidationError as e :
202
+ # post-hoc try to give a somewhat-justifiable failure reason.
203
+ if isinstance (queue , str ) or (isinstance (queue , QueueDescription ) and isinstance (queue .queue_name , str )):
204
+ raise ValueError ("queue must be a non-empty str or a QueueDescription with non-empty str queue_name" , e )
205
+ raise TypeError ("queue must be a non-empty str or a QueueDescription with non-empty str queue_name" , e )
196
206
197
- with _handle_response_error ():
198
- et = cast (
199
- ElementTree ,
200
- self ._impl .queue .put (queue_name , request_body , api_version = constants .API_VERSION )
201
- )
202
207
return _convert_xml_to_object (queue_name , et , QueueDescription )
203
208
204
209
def update_queue (self , queue_description ):
0 commit comments