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'],
|
config['api_prefix'],
|
||||||
request_uri
|
request_uri
|
||||||
)
|
)
|
||||||
|
|
||||||
# Craft the authentication header if required
|
# Craft the authentication header if required
|
||||||
if config['api_key']:
|
if config['api_key']:
|
||||||
headers = {'X-Api-Key': config['api_key']}
|
headers = {'X-Api-Key': config['api_key']}
|
||||||
else:
|
else:
|
||||||
headers = None
|
headers = None
|
||||||
|
|
||||||
# Determine the request type and hit the API
|
# Determine the request type and hit the API
|
||||||
|
try:
|
||||||
if operation == 'get':
|
if operation == 'get':
|
||||||
response = requests.get(
|
response = requests.get(
|
||||||
uri,
|
uri,
|
||||||
|
@ -72,12 +75,17 @@ def call_api(config, operation, request_uri, params=None, data=None):
|
||||||
params=params,
|
params=params,
|
||||||
data=data
|
data=data
|
||||||
)
|
)
|
||||||
|
except Exception as e:
|
||||||
|
click.echo('Failed to connect to the API: {}'.format(e))
|
||||||
|
exit(1)
|
||||||
|
|
||||||
# Display debug output
|
# Display debug output
|
||||||
if config['debug']:
|
if config['debug']:
|
||||||
click.echo('API endpoint: {}'.format(uri), err=True)
|
click.echo('API endpoint: {}'.format(uri), err=True)
|
||||||
click.echo('Response code: {}'.format(response.status_code), err=True)
|
click.echo('Response code: {}'.format(response.status_code), err=True)
|
||||||
click.echo('Response headers: {}'.format(response.headers), err=True)
|
click.echo('Response headers: {}'.format(response.headers), err=True)
|
||||||
click.echo(err=True)
|
click.echo(err=True)
|
||||||
|
|
||||||
# Return the response object
|
# Return the response object
|
||||||
return response
|
return response
|
||||||
|
|
||||||
|
|
|
@ -33,7 +33,6 @@ import colorama
|
||||||
import yaml
|
import yaml
|
||||||
import json
|
import json
|
||||||
import lxml.etree as etree
|
import lxml.etree as etree
|
||||||
import requests
|
|
||||||
|
|
||||||
from distutils.util import strtobool
|
from distutils.util import strtobool
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue