Refactor API client to support more auth types 67/62167/1
authorDW Talton <dtalton@contractor.linuxfoundation.org>
Tue, 5 Nov 2019 16:55:01 +0000 (09:55 -0700)
committerDW Talton <dtalton@contractor.linuxfoundation.org>
Tue, 5 Nov 2019 18:20:20 +0000 (11:20 -0700)
Signed-off-by: DW Talton <dtalton@contractor.linuxfoundation.org>
Change-Id: Ic74499afa718f276db0007813cf77bf3c61d1ad8

lftools/api/client.py
lftools/api/endpoints/readthedocs.py
tests/test_api_client.py
tests/test_rtd.py

index 9677888..b552e6d 100644 (file)
@@ -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."""
index 55ccaa3..f9c47a3 100644 (file)
@@ -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):
index 55d7e2b..d8379cb 100644 (file)
@@ -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
index cf4b51a..5cb65b7 100644 (file)
@@ -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',)