From b799f2c54a72ccacbf4025b5019ac8b12a90f0f3 Mon Sep 17 00:00:00 2001 From: DW Talton Date: Tue, 5 Nov 2019 09:55:01 -0700 Subject: [PATCH] Refactor API client to support more auth types Signed-off-by: DW Talton Change-Id: Ic74499afa718f276db0007813cf77bf3c61d1ad8 --- lftools/api/client.py | 14 +++++++++----- lftools/api/endpoints/readthedocs.py | 13 +++++++++---- tests/test_api_client.py | 7 ++++++- tests/test_rtd.py | 8 ++++++-- 4 files changed, 30 insertions(+), 12 deletions(-) diff --git a/lftools/api/client.py b/lftools/api/client.py index 9677888b..b552e6d4 100644 --- a/lftools/api/client.py +++ b/lftools/api/client.py @@ -20,15 +20,19 @@ class RestApi(object): def __init__(self, **params): """Initialize the REST API class.""" self.params = params - self.endpoint = self.params['endpoint'] - self.token = self.params['token'] + + if params['creds']: + self.creds = params['creds'] if 'timeout' not in self.params: self.timeout = None - self.r = requests.Session() - self.r.headers.update({'Authorization': 'Token {}'.format(self.token)}) - self.r.headers.update({'Content-Type': 'application/json'}) + 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({'Content-Type': 'application/json'}) def _request(self, url, method, data=None, timeout=10): """Execute the requested request.""" diff --git a/lftools/api/endpoints/readthedocs.py b/lftools/api/endpoints/readthedocs.py index 55ccaa3d..f9c47a3d 100644 --- a/lftools/api/endpoints/readthedocs.py +++ b/lftools/api/endpoints/readthedocs.py @@ -25,10 +25,15 @@ class ReadTheDocs(client.RestApi): def __init__(self, **params): """Initialize the class.""" - if 'token' not in params: - params['token'] = config.get_setting('rtd', 'token') - if 'endpoint' not in params: - params['endpoint'] = config.get_setting('rtd', 'endpoint') + self.params = params + if 'creds' not in self.params: + creds = { + 'authtype': 'token', + 'token': config.get_setting('rtd', 'token'), + 'endpoint': config.get_setting('rtd', 'endpoint') + } + params['creds'] = creds + super(ReadTheDocs, self).__init__(**params) def project_list(self): diff --git a/tests/test_api_client.py b/tests/test_api_client.py index 55d7e2b7..d8379cb6 100644 --- a/tests/test_api_client.py +++ b/tests/test_api_client.py @@ -13,7 +13,12 @@ import responses import lftools.api.client as client -c = client.RestApi(endpoint='', token='xyz') +creds = { + 'authtype': 'token', + 'endpoint': '', + 'token': 'xyz' +} +c = client.RestApi(creds=creds) @responses.activate diff --git a/tests/test_rtd.py b/tests/test_rtd.py index cf4b51a9..5cb65b7b 100644 --- a/tests/test_rtd.py +++ b/tests/test_rtd.py @@ -16,8 +16,12 @@ import responses import lftools.api.endpoints.readthedocs as client -rtd = client.ReadTheDocs(endpoint='https://readthedocs.org/api/v3/', - token='xyz') +creds = { + 'authtype': 'token', + 'endpoint': 'https://readthedocs.org/api/v3/', + 'token': 'xyz' +} +rtd = client.ReadTheDocs(creds=creds) FIXTURE_DIR = os.path.join(os.path.dirname( os.path.realpath(__file__)), 'fixtures',) -- 2.16.6