@@ -273,10 +273,7 @@ def close(self):
273
273
super (Connection , self ).close ()
274
274
275
275
@ensure_connect
276
- def send (self , path , data , retries = None , ** kwargs ):
277
- """
278
- Sends the command to the device over api
279
- """
276
+ def _open_url (self , path , data , retries = None , ** kwargs ):
280
277
url_kwargs = dict (
281
278
timeout = self .get_option ("persistent_command_timeout" ),
282
279
validate_certs = self .get_option ("validate_certs" ),
@@ -307,13 +304,13 @@ def send(self, path, data, retries=None, **kwargs):
307
304
url_kwargs ["url_username" ] = self .get_option ("remote_user" )
308
305
url_kwargs ["url_password" ] = self .get_option ("password" )
309
306
307
+ url = self ._url + path
308
+ self ._log_messages (
309
+ "send url '%s' with data '%s' and kwargs '%s'"
310
+ % (url , data , url_kwargs )
311
+ )
310
312
try :
311
- url = self ._url + path
312
- self ._log_messages (
313
- "send url '%s' with data '%s' and kwargs '%s'"
314
- % (url , data , url_kwargs )
315
- )
316
- response = open_url (url , data = data , ** url_kwargs )
313
+ return open_url (url , data = data , ** url_kwargs )
317
314
except HTTPError as exc :
318
315
is_handled = self .handle_httperror (exc )
319
316
if is_handled is True :
@@ -325,14 +322,21 @@ def send(self, path, data, retries=None, **kwargs):
325
322
raise
326
323
if is_handled is False :
327
324
raise
328
- response = is_handled
325
+ return is_handled
329
326
except URLError as exc :
330
327
raise AnsibleConnectionFailure (
331
328
"Could not connect to {0}: {1}" .format (
332
329
self ._url + path , exc .reason
333
330
)
334
331
)
335
332
333
+ @ensure_connect
334
+ def send (self , path , data , retries = None , ** kwargs ):
335
+ """
336
+ Sends the command to the device over api
337
+ """
338
+ response = self ._open_url (path , data , ** kwargs )
339
+
336
340
response_buffer = BytesIO ()
337
341
resp_data = response .read ()
338
342
self ._log_messages ("received response: '%s'" % resp_data )
@@ -345,6 +349,18 @@ def send(self, path, data, retries=None, **kwargs):
345
349
346
350
return response , response_buffer
347
351
352
+ def get_file (self , path , dest , ** kwargs ):
353
+ """Download the contents of path to dest"""
354
+ response = self ._open_url (path , ** kwargs )
355
+ self ._log_messages ("writing response to '%s'" % dest )
356
+
357
+ buffer_size = 65536
358
+ data = response .read (buffer_size )
359
+ with open (dest , "wb" ) as output_file :
360
+ while data :
361
+ output_file .write (data )
362
+ data = response .read (buffer_size )
363
+
348
364
def transport_test (self , connect_timeout ):
349
365
"""This method enables wait_for_connection to work.
350
366
0 commit comments