Skip to content

Commit

Permalink
Add get_file to httpapi
Browse files Browse the repository at this point in the history
  • Loading branch information
Qalthos committed Jun 1, 2022
1 parent 2a750bd commit 6a6c7cb
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 11 deletions.
3 changes: 3 additions & 0 deletions changelogs/fragments/httpapi-get_file.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
minor_changes:
- httpapi - New method `get_file()` to download large files from the remote API.
38 changes: 27 additions & 11 deletions plugins/connection/httpapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,10 +255,7 @@ def close(self):
super(Connection, self).close()

@ensure_connect
def send(self, path, data, retries=None, **kwargs):
"""
Sends the command to the device over api
"""
def _open_url(self, path, data, retries=None, **kwargs):
url_kwargs = dict(
timeout=self.get_option("persistent_command_timeout"),
validate_certs=self.get_option("validate_certs"),
Expand All @@ -276,13 +273,13 @@ def send(self, path, data, retries=None, **kwargs):
url_kwargs["url_username"] = self.get_option("remote_user")
url_kwargs["url_password"] = self.get_option("password")

url = self._url + path
self._log_messages(
"send url '%s' with data '%s' and kwargs '%s'"
% (url, data, url_kwargs)
)
try:
url = self._url + path
self._log_messages(
"send url '%s' with data '%s' and kwargs '%s'"
% (url, data, url_kwargs)
)
response = open_url(url, data=data, **url_kwargs)
return open_url(url, data=data, **url_kwargs)
except HTTPError as exc:
is_handled = self.handle_httperror(exc)
if is_handled is True:
Expand All @@ -294,14 +291,21 @@ def send(self, path, data, retries=None, **kwargs):
raise
if is_handled is False:
raise
response = is_handled
return is_handled
except URLError as exc:
raise AnsibleConnectionFailure(
"Could not connect to {0}: {1}".format(
self._url + path, exc.reason
)
)

@ensure_connect
def send(self, path, data, retries=None, **kwargs):
"""
Sends the command to the device over api
"""
response = self._open_url(path, data, **kwargs)

response_buffer = BytesIO()
resp_data = response.read()
self._log_messages("received response: '%s'" % resp_data)
Expand All @@ -314,6 +318,18 @@ def send(self, path, data, retries=None, **kwargs):

return response, response_buffer

def get_file(self, path, dest, **kwargs):
"""Download the contents of path to dest"""
response = self._open_url(path, **kwargs)
self._log_messages("writing response to '%s'" % dest)

buffer_size = 65536
data = response.read(buffer_size)
with open(dest, "wb") as output_file:
while data:
output_file.write(data)
data = response.read(buffer_size)

def transport_test(self, connect_timeout):
"""This method enables wait_for_connection to work.
Expand Down

0 comments on commit 6a6c7cb

Please sign in to comment.