Gracefully handle API connect timeouts
Prevents dumping a massive stacktrace if connection fails.
This commit is contained in:
parent
ab28bf40d1
commit
ebfd9c1882
|
@ -31,53 +31,61 @@ 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
|
||||||
if operation == 'get':
|
try:
|
||||||
response = requests.get(
|
if operation == 'get':
|
||||||
uri,
|
response = requests.get(
|
||||||
headers=headers,
|
uri,
|
||||||
params=params,
|
headers=headers,
|
||||||
data=data
|
params=params,
|
||||||
)
|
data=data
|
||||||
if operation == 'post':
|
)
|
||||||
response = requests.post(
|
if operation == 'post':
|
||||||
uri,
|
response = requests.post(
|
||||||
headers=headers,
|
uri,
|
||||||
params=params,
|
headers=headers,
|
||||||
data=data
|
params=params,
|
||||||
)
|
data=data
|
||||||
if operation == 'put':
|
)
|
||||||
response = requests.put(
|
if operation == 'put':
|
||||||
uri,
|
response = requests.put(
|
||||||
headers=headers,
|
uri,
|
||||||
params=params,
|
headers=headers,
|
||||||
data=data
|
params=params,
|
||||||
)
|
data=data
|
||||||
if operation == 'patch':
|
)
|
||||||
response = requests.patch(
|
if operation == 'patch':
|
||||||
uri,
|
response = requests.patch(
|
||||||
headers=headers,
|
uri,
|
||||||
params=params,
|
headers=headers,
|
||||||
data=data
|
params=params,
|
||||||
)
|
data=data
|
||||||
if operation == 'delete':
|
)
|
||||||
response = requests.delete(
|
if operation == 'delete':
|
||||||
uri,
|
response = requests.delete(
|
||||||
headers=headers,
|
uri,
|
||||||
params=params,
|
headers=headers,
|
||||||
data=data
|
params=params,
|
||||||
)
|
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