user.py 文件源码

python
阅读 22 收藏 0 点赞 0 评论 0

项目:cli 作者: riseml 项目源码 文件源码
def check_api_config(api_url, api_key, timeout=180):
    print('Waiting %ss for successful login to %s with API key \'%s\' ...' % (timeout, api_url, api_key))
    config = Configuration()
    old_api_host = config.host
    old_api_key = config.api_key['api_key']
    config.host = api_url
    config.api_key['api_key'] = api_key
    api_client = ApiClient()
    client = AdminApi(api_client)
    start = time.time()
    while True:
        try:
            cluster_infos = client.get_cluster_infos()
            cluster_id = get_cluster_id(cluster_infos)
            print('Success! Cluster ID: %s' % cluster_id)
            config.api_key['api_key'] = old_api_key
            config.host = old_api_host
            return cluster_id
        except ApiException as exc:
            if exc.reason == 'UNAUTHORIZED':
                print(exc.status, 'Unauthorized - wrong api key?')
                sys.exit(1)
            elif time.time() - start < timeout:
                time.sleep(1)
                continue
            else:
                print(exc.status, exc.reason)
                sys.exit(1)
        except HTTPError as e:
            if time.time() - start < timeout:
                time.sleep(1)
                continue
            else:
                print('Unable to connecto to %s ' % api_url)
                # all uncaught http errors goes here
                print(e.reason)
                sys.exit(1)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号