Skip to content

Commit c2406a6

Browse files
committed
Add get_file to httpapi
1 parent 5018e7d commit c2406a6

File tree

2 files changed

+30
-11
lines changed

2 files changed

+30
-11
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
---
2+
minor_changes:
3+
- httpapi - New method `get_file()` to download large files from the remote API.

plugins/connection/httpapi.py

+27-11
Original file line numberDiff line numberDiff line change
@@ -273,10 +273,7 @@ def close(self):
273273
super(Connection, self).close()
274274

275275
@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):
280277
url_kwargs = dict(
281278
timeout=self.get_option("persistent_command_timeout"),
282279
validate_certs=self.get_option("validate_certs"),
@@ -307,13 +304,13 @@ def send(self, path, data, retries=None, **kwargs):
307304
url_kwargs["url_username"] = self.get_option("remote_user")
308305
url_kwargs["url_password"] = self.get_option("password")
309306

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+
)
310312
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)
317314
except HTTPError as exc:
318315
is_handled = self.handle_httperror(exc)
319316
if is_handled is True:
@@ -325,14 +322,21 @@ def send(self, path, data, retries=None, **kwargs):
325322
raise
326323
if is_handled is False:
327324
raise
328-
response = is_handled
325+
return is_handled
329326
except URLError as exc:
330327
raise AnsibleConnectionFailure(
331328
"Could not connect to {0}: {1}".format(
332329
self._url + path, exc.reason
333330
)
334331
)
335332

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+
336340
response_buffer = BytesIO()
337341
resp_data = response.read()
338342
self._log_messages("received response: '%s'" % resp_data)
@@ -345,6 +349,18 @@ def send(self, path, data, retries=None, **kwargs):
345349

346350
return response, response_buffer
347351

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+
348364
def transport_test(self, connect_timeout):
349365
"""This method enables wait_for_connection to work.
350366

0 commit comments

Comments
 (0)