From cb5380f74176b6f70edb5c323b3408e84e4ce33f Mon Sep 17 00:00:00 2001 From: DW Talton Date: Tue, 5 Nov 2019 13:53:51 -0700 Subject: [PATCH] Add HTTPBasicAuth support to REST client Signed-off-by: DW Talton Change-Id: I6467c8aeb2b2f24800ab1912ec6b485693a3378f --- lftools/api/client.py | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/lftools/api/client.py b/lftools/api/client.py index b552e6d4..d42bb52d 100644 --- a/lftools/api/client.py +++ b/lftools/api/client.py @@ -27,20 +27,28 @@ class RestApi(object): if 'timeout' not in self.params: self.timeout = None + self.endpoint = self.creds['endpoint'] + + if self.creds['authtype'] == 'basic': + self.username = self.creds['username'] + self.password = self.creds['password'] + self.r = requests.Session() + self.r.auth = (self.username, self.password) + if self.creds['authtype'] == 'token': - self.endpoint = self.creds['endpoint'] self.token = self.creds['token'] self.r = requests.Session() - self.r.headers.update({'Authorization': 'Token {}'.format(self.token)}) # NOQA + self.r.headers.update({'Authorization': 'Token {}' + .format(self.token)}) self.r.headers.update({'Content-Type': 'application/json'}) def _request(self, url, method, data=None, timeout=10): - """Execute the requested request.""" - resp = self.r.request(method, self.endpoint + url, data=data, - timeout=timeout) + """Execute the request.""" + resp = self.r.request(method, self.endpoint + url, data=data, timeout=timeout) if resp.text: try: + print(resp.text) if resp.headers['Content-Type'] == 'application/json': body = json.loads(resp.text) else: -- 2.16.6