File tree Expand file tree Collapse file tree 2 files changed +34
-5
lines changed Expand file tree Collapse file tree 2 files changed +34
-5
lines changed Original file line number Diff line number Diff line change 7
7
import certifi
8
8
import json
9
9
import logging
10
+ import os
10
11
import requests
11
12
12
13
logger = logging .getLogger ('intercom.request' )
13
14
14
15
16
+ def configure_timeout ():
17
+ """Configure the request timeout."""
18
+ timeout = os .getenv ('INTERCOM_REQUEST_TIMEOUT' , '90' )
19
+ try :
20
+ return int (timeout )
21
+ except ValueError :
22
+ logger .warning ('%s is not a valid timeout value.' , timeout )
23
+ return 90
24
+
25
+
15
26
class Request (object ):
16
27
17
- timeout = 10
28
+ timeout = configure_timeout ()
18
29
19
30
def __init__ (self , http_method , path , http_session = None ):
20
31
self .http_method = http_method
21
32
self .path = path
22
33
self .http_session = http_session
23
34
24
-
25
35
def execute (self , base_url , auth , params ):
26
36
return self .send_request_to_path (base_url , auth , params )
27
37
Original file line number Diff line number Diff line change @@ -332,6 +332,25 @@ def it_needs_encoding_or_apparent_encoding(self):
332
332
@istest
333
333
def it_allows_the_timeout_to_be_changed (self ):
334
334
from intercom .request import Request
335
- eq_ (10 , Request .timeout )
336
- Request .timeout = 3
337
- eq_ (3 , Request .timeout )
335
+ try :
336
+ eq_ (90 , Request .timeout )
337
+ Request .timeout = 3
338
+ eq_ (3 , Request .timeout )
339
+ finally :
340
+ Request .timeout = 90
341
+
342
+ @istest
343
+ def it_allows_the_timeout_to_be_configured (self ):
344
+ import os
345
+ from intercom .request import configure_timeout
346
+
347
+ # check the default
348
+ eq_ (90 , configure_timeout ())
349
+
350
+ # override the default
351
+ os .environ ['INTERCOM_REQUEST_TIMEOUT' ] = '20'
352
+ eq_ (20 , configure_timeout ())
353
+
354
+ # ignore bad timeouts, reset to default 90
355
+ os .environ ['INTERCOM_REQUEST_TIMEOUT' ] = 'abc'
356
+ eq_ (90 , configure_timeout ())
You can’t perform that action at this time.
0 commit comments