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:
Nathaniel Case 2018-08-13 10:25:06 -04:00 committed by GitHub
parent 65772ede26
commit a3385a60b4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 62 additions and 23 deletions

View file

@ -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."""