Gracefully handle API connect timeouts

Prevents dumping a massive stacktrace if connection fails.
This commit is contained in:
Joshua Boniface 2020-01-30 09:19:36 -05:00
parent ab28bf40d1
commit ebfd9c1882
2 changed files with 43 additions and 36 deletions

View File

@ -31,12 +31,15 @@ def call_api(config, operation, request_uri, params=None, data=None):
config['api_prefix'],
request_uri
)
# Craft the authentication header if required
if config['api_key']:
headers = {'X-Api-Key': config['api_key']}
else:
headers = None
# Determine the request type and hit the API
try:
if operation == 'get':
response = requests.get(
uri,
@ -72,12 +75,17 @@ def call_api(config, operation, request_uri, params=None, data=None):
params=params,
data=data
)
except Exception as e:
click.echo('Failed to connect to the API: {}'.format(e))
exit(1)
# Display debug output
if config['debug']:
click.echo('API endpoint: {}'.format(uri), err=True)
click.echo('Response code: {}'.format(response.status_code), err=True)
click.echo('Response headers: {}'.format(response.headers), err=True)
click.echo(err=True)
# Return the response object
return response

View File

@ -33,7 +33,6 @@ import colorama
import yaml
import json
import lxml.etree as etree
import requests
from distutils.util import strtobool