api: Add timeout parameter to do_api_query and call_endpoint.

This commit is contained in:
neiljp (Neil Pilgrim) 2019-01-14 12:16:16 -08:00 committed by Tim Abbott
parent 3f2b11c345
commit f858e48468

View file

@ -472,8 +472,9 @@ class Client(object):
vendor_version=vendor_version, vendor_version=vendor_version,
) )
def do_api_query(self, orig_request, url, method="POST", longpolling=False, files=None): def do_api_query(self, orig_request, url, method="POST",
# type: (Mapping[str, Any], str, str, bool, Optional[List[IO[Any]]]) -> Dict[str, Any] longpolling=False, files=None, timeout=None):
# type: (Mapping[str, Any], str, str, bool, Optional[List[IO[Any]]], Optional[float]) -> Dict[str, Any]
if files is None: if files is None:
files = [] files = []
@ -481,10 +482,10 @@ class Client(object):
# When long-polling, set timeout to 90 sec as a balance # When long-polling, set timeout to 90 sec as a balance
# between a low traffic rate and a still reasonable latency # between a low traffic rate and a still reasonable latency
# time in case of a connection failure. # time in case of a connection failure.
request_timeout = 90 request_timeout = 90.
else: else:
# Otherwise, 15s should be plenty of time. # Otherwise, 15s should be plenty of time.
request_timeout = 15 request_timeout = 15. if not timeout else timeout
request = {} request = {}
req_files = [] req_files = []
@ -606,8 +607,9 @@ class Client(object):
return {'msg': "Unexpected error from the server", "result": "http-error", return {'msg': "Unexpected error from the server", "result": "http-error",
"status_code": res.status_code} "status_code": res.status_code}
def call_endpoint(self, url=None, method="POST", request=None, longpolling=False, files=None): def call_endpoint(self, url=None, method="POST", request=None,
# type: (Optional[str], str, Optional[Dict[str, Any]], bool, Optional[List[IO[Any]]]) -> Dict[str, Any] longpolling=False, files=None, timeout=None):
# type: (Optional[str], str, Optional[Dict[str, Any]], bool, Optional[List[IO[Any]]], Optional[float]) -> Dict[str, Any]
if request is None: if request is None:
request = dict() request = dict()
marshalled_request = {} marshalled_request = {}
@ -616,7 +618,7 @@ class Client(object):
marshalled_request[k] = v marshalled_request[k] = v
versioned_url = API_VERSTRING + (url if url is not None else "") versioned_url = API_VERSTRING + (url if url is not None else "")
return self.do_api_query(marshalled_request, versioned_url, method=method, return self.do_api_query(marshalled_request, versioned_url, method=method,
longpolling=longpolling, files=files) longpolling=longpolling, files=files, timeout=timeout)
def call_on_each_event(self, callback, event_types=None, narrow=None): def call_on_each_event(self, callback, event_types=None, narrow=None):
# type: (Callable[[Dict[str, Any]], None], Optional[List[str]], Optional[List[List[str]]]) -> None # type: (Callable[[Dict[str, Any]], None], Optional[List[str]], Optional[List[List[str]]]) -> None