Gracefully handle API connect timeouts
Prevents dumping a massive stacktrace if connection fails.
This commit is contained in:
parent
ab28bf40d1
commit
ebfd9c1882
|
@ -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
|
||||
|
||||
|
|
|
@ -33,7 +33,6 @@ import colorama
|
|||
import yaml
|
||||
import json
|
||||
import lxml.etree as etree
|
||||
import requests
|
||||
|
||||
from distutils.util import strtobool
|
||||
|
||||
|
|
Loading…
Reference in New Issue