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,53 +31,61 @@ 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
if operation == 'get':
response = requests.get(
uri,
headers=headers,
params=params,
data=data
)
if operation == 'post':
response = requests.post(
uri,
headers=headers,
params=params,
data=data
)
if operation == 'put':
response = requests.put(
uri,
headers=headers,
params=params,
data=data
)
if operation == 'patch':
response = requests.patch(
uri,
headers=headers,
params=params,
data=data
)
if operation == 'delete':
response = requests.delete(
uri,
headers=headers,
params=params,
data=data
)
try:
if operation == 'get':
response = requests.get(
uri,
headers=headers,
params=params,
data=data
)
if operation == 'post':
response = requests.post(
uri,
headers=headers,
params=params,
data=data
)
if operation == 'put':
response = requests.put(
uri,
headers=headers,
params=params,
data=data
)
if operation == 'patch':
response = requests.patch(
uri,
headers=headers,
params=params,
data=data
)
if operation == 'delete':
response = requests.delete(
uri,
headers=headers,
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