@@ -255,10 +255,7 @@ def close(self):
255
255
super (Connection , self ).close ()
256
256
257
257
@ensure_connect
258
- def send (self , path , data , retries = None , ** kwargs ):
259
- """
260
- Sends the command to the device over api
261
- """
258
+ def _open_url (self , path , data , retries = None , ** kwargs ):
262
259
url_kwargs = dict (
263
260
timeout = self .get_option ("persistent_command_timeout" ),
264
261
validate_certs = self .get_option ("validate_certs" ),
@@ -276,13 +273,13 @@ def send(self, path, data, retries=None, **kwargs):
276
273
url_kwargs ["url_username" ] = self .get_option ("remote_user" )
277
274
url_kwargs ["url_password" ] = self .get_option ("password" )
278
275
276
+ url = self ._url + path
277
+ self ._log_messages (
278
+ "send url '%s' with data '%s' and kwargs '%s'"
279
+ % (url , data , url_kwargs )
280
+ )
279
281
try :
280
- url = self ._url + path
281
- self ._log_messages (
282
- "send url '%s' with data '%s' and kwargs '%s'"
283
- % (url , data , url_kwargs )
284
- )
285
- response = open_url (url , data = data , ** url_kwargs )
282
+ return open_url (url , data = data , ** url_kwargs )
286
283
except HTTPError as exc :
287
284
is_handled = self .handle_httperror (exc )
288
285
if is_handled is True :
@@ -294,14 +291,21 @@ def send(self, path, data, retries=None, **kwargs):
294
291
raise
295
292
if is_handled is False :
296
293
raise
297
- response = is_handled
294
+ return is_handled
298
295
except URLError as exc :
299
296
raise AnsibleConnectionFailure (
300
297
"Could not connect to {0}: {1}" .format (
301
298
self ._url + path , exc .reason
302
299
)
303
300
)
304
301
302
+ @ensure_connect
303
+ def send (self , path , data , retries = None , ** kwargs ):
304
+ """
305
+ Sends the command to the device over api
306
+ """
307
+ response = self ._open_url (path , data , ** kwargs )
308
+
305
309
response_buffer = BytesIO ()
306
310
resp_data = response .read ()
307
311
self ._log_messages ("received response: '%s'" % resp_data )
@@ -314,6 +318,18 @@ def send(self, path, data, retries=None, **kwargs):
314
318
315
319
return response , response_buffer
316
320
321
+ def get_file (self , path , dest , ** kwargs ):
322
+ """Download the contents of path to dest"""
323
+ response = self ._open_url (path , ** kwargs )
324
+ self ._log_messages ("writing response to '%s'" % dest )
325
+
326
+ buffer_size = 65536
327
+ data = response .read (buffer_size )
328
+ with open (dest , "wb" ) as output_file :
329
+ while data :
330
+ output_file .write (data )
331
+ data = response .read (buffer_size )
332
+
317
333
def transport_test (self , connect_timeout ):
318
334
"""This method enables wait_for_connection to work.
319
335
0 commit comments