Skip to content

Commit 6a6c7cb

Browse files
committed
Add get_file to httpapi
1 parent 2a750bd commit 6a6c7cb

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
@@ -255,10 +255,7 @@ def close(self):
255255
super(Connection, self).close()
256256

257257
@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):
262259
url_kwargs = dict(
263260
timeout=self.get_option("persistent_command_timeout"),
264261
validate_certs=self.get_option("validate_certs"),
@@ -276,13 +273,13 @@ def send(self, path, data, retries=None, **kwargs):
276273
url_kwargs["url_username"] = self.get_option("remote_user")
277274
url_kwargs["url_password"] = self.get_option("password")
278275

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+
)
279281
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)
286283
except HTTPError as exc:
287284
is_handled = self.handle_httperror(exc)
288285
if is_handled is True:
@@ -294,14 +291,21 @@ def send(self, path, data, retries=None, **kwargs):
294291
raise
295292
if is_handled is False:
296293
raise
297-
response = is_handled
294+
return is_handled
298295
except URLError as exc:
299296
raise AnsibleConnectionFailure(
300297
"Could not connect to {0}: {1}".format(
301298
self._url + path, exc.reason
302299
)
303300
)
304301

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+
305309
response_buffer = BytesIO()
306310
resp_data = response.read()
307311
self._log_messages("received response: '%s'" % resp_data)
@@ -314,6 +318,18 @@ def send(self, path, data, retries=None, **kwargs):
314318

315319
return response, response_buffer
316320

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

0 commit comments

Comments
 (0)