From 7747ea7d9fa5c9a00b1d6a5b873569f32b54c3d4 Mon Sep 17 00:00:00 2001 From: MOIS3Y Date: Sun, 25 Sep 2022 18:13:23 +0900 Subject: [PATCH] Add try/except to get_key method --- main.py | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/main.py b/main.py index b34cafd..813d11a 100644 --- a/main.py +++ b/main.py @@ -21,7 +21,6 @@ class Access(object): if platform == 'dci': self.user_table = AuthUser self.platform = platform - self.input_container = 'input' def get_admin(self): """ @@ -66,13 +65,28 @@ class Access(object): Returns: _dict_: _access key_ """ - host = self.input_container + host = 'input' + if self.platform == 'dci': + host = 'dci_input_1' # because this hostname + url = 'http://{}:1500/api/auth/v4/user/{}/key'.format( host, admin['id'] ) headers = {"Internal-Auth": "on", "Accept": "application/json"} - req = requests.post(url, headers=headers, data={}) - key = req.json() + + check_key = 0 + + while check_key != 3: + try: + req = requests.post(url, headers=headers, data={}) + answer = req.json() + key = answer['key'] + print("Key will be used:") + print(key, "\n") + break + except Exception: + check_key += 1 + key = 'not_found_try_again' return key @@ -92,10 +106,7 @@ def main(): key = access.get_key(account) - print("Key will be used:") - print(key, "\n") - - access_link = 'https://{}/auth/key-v4/{}'.format(CLIENT_HOST, key['key']) + access_link = 'https://{}/auth/key-v4/{}'.format(CLIENT_HOST, key) print("Your access link: \n") print(access_link, "\n")