mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-04-27 04:41:26 -07:00
httpapi: let httpapi plugin handle HTTPErrors other than 401 (#43436)
* Hold httpapi response in BytesIO * Let httpapi plugin deal with HTTP codes if it wants * Python 3.5 won't json.loads() bytes * Don't modify headers passed to send * Move code handling back to send() but let httpapi plugin have a say on how it happens
This commit is contained in:
parent
65772ede26
commit
a3385a60b4
4 changed files with 62 additions and 23 deletions
|
@ -11,6 +11,8 @@ from ansible.plugins import AnsiblePlugin
|
|||
|
||||
class HttpApiBase(AnsiblePlugin):
|
||||
def __init__(self, connection):
|
||||
super(HttpApiBase, self).__init__()
|
||||
|
||||
self.connection = connection
|
||||
self._become = False
|
||||
self._become_pass = ''
|
||||
|
@ -49,6 +51,30 @@ class HttpApiBase(AnsiblePlugin):
|
|||
|
||||
return None
|
||||
|
||||
def handle_httperror(self, exc):
|
||||
"""Overridable method for dealing with HTTP codes.
|
||||
|
||||
This method will attempt to handle known cases of HTTP status codes.
|
||||
If your API uses status codes to convey information in a regular way,
|
||||
you can override this method to handle it appropriately.
|
||||
|
||||
:returns:
|
||||
* True if the code has been handled in a way that the request
|
||||
may be resent without changes.
|
||||
* False if this code indicates a fatal or unknown error which
|
||||
cannot be handled by the plugin. This will result in an
|
||||
AnsibleConnectionFailure being raised.
|
||||
* Any other response passes the HTTPError along to the caller to
|
||||
deal with as appropriate.
|
||||
"""
|
||||
if exc.code == 401 and self.connection._auth:
|
||||
# Stored auth appears to be invalid, clear and retry
|
||||
self.connection._auth = None
|
||||
self.login(self.connection.get_option('remote_user'), self.connection.get_option('password'))
|
||||
return True
|
||||
|
||||
return False
|
||||
|
||||
@abstractmethod
|
||||
def send_request(self, data, **message_kwargs):
|
||||
"""Prepares and sends request(s) to device."""
|
||||
|
|
|
@ -30,13 +30,16 @@ class HttpApi(HttpApiBase):
|
|||
request = request_builder(data, output)
|
||||
headers = {'Content-Type': 'application/json-rpc'}
|
||||
|
||||
response, response_text = self.connection.send('/command-api', request, headers=headers, method='POST')
|
||||
try:
|
||||
response_text = json.loads(response_text)
|
||||
except ValueError:
|
||||
raise ConnectionError('Response was not valid JSON, got {0}'.format(response_text))
|
||||
response, response_data = self.connection.send('/command-api', request, headers=headers, method='POST')
|
||||
|
||||
results = handle_response(response_text)
|
||||
try:
|
||||
response_data = json.loads(to_text(response_data.getvalue()))
|
||||
except ValueError:
|
||||
raise ConnectionError('Response was not valid JSON, got {0}'.format(
|
||||
to_text(response_data.getvalue())
|
||||
))
|
||||
|
||||
results = handle_response(response_data)
|
||||
|
||||
if self._become:
|
||||
results = results[1:]
|
||||
|
|
|
@ -27,13 +27,16 @@ class HttpApi(HttpApiBase):
|
|||
request = request_builder(queue, output)
|
||||
headers = {'Content-Type': 'application/json'}
|
||||
|
||||
response, response_text = self.connection.send('/ins', request, headers=headers, method='POST')
|
||||
try:
|
||||
response_text = json.loads(response_text)
|
||||
except ValueError:
|
||||
raise ConnectionError('Response was not valid JSON, got {0}'.format(response_text))
|
||||
response, response_data = self.connection.send('/ins', request, headers=headers, method='POST')
|
||||
|
||||
results = handle_response(response_text)
|
||||
try:
|
||||
response_data = json.loads(to_text(response_data.getvalue()))
|
||||
except ValueError:
|
||||
raise ConnectionError('Response was not valid JSON, got {0}'.format(
|
||||
to_text(response_data.getvalue())
|
||||
))
|
||||
|
||||
results = handle_response(response_data)
|
||||
|
||||
if self._become:
|
||||
results = results[1:]
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue