Skip to content

Commit 64373cd

Browse files
committed
Add default 5s requests timeout and make configurable
1 parent 8481517 commit 64373cd

2 files changed

Lines changed: 36 additions & 13 deletions

File tree

alertaclient/api.py

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,15 @@ def __call__(self, r):
3131

3232
class ApiClient(object):
3333

34-
def __init__(self, endpoint=None, key=None, ssl_verify=True):
34+
def __init__(self, endpoint=None, key=None, timeout=5.0, ssl_verify=True):
3535

3636
self.endpoint = endpoint or os.environ.get('ALERTA_ENDPOINT', "http://localhost:8080")
3737
self.key = key or os.environ.get('ALERTA_API_KEY', '')
38-
self.ssl_verify = ssl_verify # or REQUESTS_CA_BUNDLE env var
38+
3939
self.session = requests.Session()
40+
self.session.verify = ssl_verify # or use REQUESTS_CA_BUNDLE env var
41+
42+
self.timeout = float(timeout)
4043

4144
def __repr__(self):
4245

@@ -176,7 +179,12 @@ def _get(self, path, query=None):
176179
query = query or tuple()
177180

178181
url = self.endpoint + path + '?' + urlencode(query, doseq=True)
179-
response = self.session.get(url, auth=ApiAuth(self.key), verify=self.ssl_verify)
182+
183+
try:
184+
response = self.session.get(url, auth=ApiAuth(self.key), timeout=self.timeout)
185+
except requests.exceptions.RequestException as e:
186+
LOG.error(e)
187+
sys.exit(1)
180188

181189
LOG.debug('Request Headers: %s', response.request.headers)
182190

@@ -190,7 +198,11 @@ def _post(self, path, data=None):
190198
url = self.endpoint + path
191199
headers = {'Content-Type': 'application/json'}
192200

193-
response = self.session.post(url, data=data, headers=headers, auth=ApiAuth(self.key), verify=self.ssl_verify)
201+
try:
202+
response = self.session.post(url, data=data, headers=headers, auth=ApiAuth(self.key), timeout=self.timeout)
203+
except requests.exceptions.RequestException as e:
204+
LOG.error(e)
205+
sys.exit(1)
194206

195207
LOG.debug('Request Headers: %s', response.request.headers)
196208
LOG.debug('Request Body: %s', data)
@@ -205,8 +217,11 @@ def _put(self, path, data=None):
205217
url = self.endpoint + path
206218
headers = {'Content-Type': 'application/json'}
207219

208-
response = self.session.put(url, data=data, headers=headers, auth=ApiAuth(self.key), verify=self.ssl_verify)
209-
220+
try:
221+
response = self.session.put(url, data=data, headers=headers, auth=ApiAuth(self.key), timeout=self.timeout)
222+
except requests.exceptions.RequestException as e:
223+
LOG.error(e)
224+
sys.exit(1)
210225
LOG.debug('Request Headers: %s', response.request.headers)
211226
LOG.debug('Request Body: %s', data)
212227

@@ -218,7 +233,12 @@ def _put(self, path, data=None):
218233
def _delete(self, path):
219234

220235
url = self.endpoint + path
221-
response = self.session.delete(url, auth=ApiAuth(self.key), verify=self.ssl_verify)
236+
237+
try:
238+
response = self.session.delete(url, auth=ApiAuth(self.key), timeout=self.timeout)
239+
except requests.exceptions.RequestException as e:
240+
LOG.error(e)
241+
sys.exit(1)
222242

223243
LOG.debug('Request Headers: %s', response.request.headers)
224244

alertaclient/shell.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
'endpoint': 'http://localhost:8080',
4040
'key': '',
4141
'timezone': 'Europe/London',
42+
'timeout': 5.0,
4243
'sslverify': True,
4344
'output': 'text',
4445
'color': True,
@@ -71,11 +72,11 @@ class AlertCommand(object):
7172

7273
def __init__(self):
7374

74-
self.api = ApiClient()
75+
self.api = None
7576

76-
def set(self, endpoint, key, ssl_verify=True):
77+
def set(self, endpoint, key, timeout, ssl_verify=True):
7778

78-
self.api = ApiClient(endpoint=endpoint, key=key, ssl_verify=ssl_verify)
79+
self.api = ApiClient(endpoint=endpoint, key=key, timeout=timeout, ssl_verify=ssl_verify)
7980

8081
def send(self, args):
8182

@@ -94,7 +95,7 @@ def send(self, args):
9495
attributes=dict([attrib.split('=') for attrib in args.attributes]),
9596
origin=args.origin,
9697
event_type=args.event_type,
97-
timeout=args.timeout,
98+
timeout=args.expires,
9899
raw_data=args.raw_data
99100
)
100101

@@ -113,7 +114,7 @@ def heartbeat(self, args):
113114
heartbeat = Heartbeat(
114115
origin=args.origin,
115116
tags=args.tags,
116-
timeout=args.timeout
117+
timeout=args.expires
117118
)
118119

119120
response = self.api.send(heartbeat)
@@ -934,6 +935,7 @@ def run(self):
934935
)
935936
parser_send.add_argument(
936937
'--timeout',
938+
dest='expires',
937939
default=DEFAULT_TIMEOUT,
938940
help='Timeout in seconds before an "open" alert will be automatically "expired" or "deleted"',
939941
type=int
@@ -1292,6 +1294,7 @@ def run(self):
12921294
)
12931295
parser_heartbeat.add_argument(
12941296
'--timeout',
1297+
dest='expires',
12951298
default=None,
12961299
help='Timeout in seconds before a heartbeat will be considered stale',
12971300
type=int
@@ -1460,7 +1463,7 @@ def run(self):
14601463
args.filters += ['id='+i for i in args.ids]
14611464
args.output = 'json' if args.json else args.output
14621465

1463-
cli.set(endpoint=args.endpoint, key=args.key, ssl_verify=args.sslverify)
1466+
cli.set(endpoint=args.endpoint, key=args.key, timeout=args.timeout, ssl_verify=args.sslverify)
14641467

14651468
if hasattr(args, 'func'):
14661469
args.func(args)

0 commit comments

Comments
 (0)