From 149148487ba7390d8c6cae74a3a20868e6247640 Mon Sep 17 00:00:00 2001 From: cloverr Date: Sun, 15 Jan 2023 15:25:51 +0530 Subject: [PATCH] Add timeout duration for the requests This can be set using the TIMEOUT_S variable (in seconds). Add the docs as well. --- README.md | 6 ++++++ doc/http-client.txt | 7 +++++++ plugin/http_client.py | 18 +++++++++++++++++- 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 3281c84..b82166b 100644 --- a/README.md +++ b/README.md @@ -77,6 +77,12 @@ If you'd like to pass form-encoded data, set your body like this: You can also send files using absolute path to file: `!file(PATH_TO_FILE)` or by simply providing it's content: `!content(my file content)`. +To set timeout for the requests (in seconds) at global level or inidvidual request level use the variable TIMEOUT_S set to an integer value. +``` +# $TIMEOUT_S = 30 (sets 30seconds timeout for all requests) +# :TIMEOUT_S = 30 (sets 30seconds timeout on individual blocks) +``` + Example: ``` POST http://httpbin.org/post diff --git a/doc/http-client.txt b/doc/http-client.txt index 520afde..9c234a7 100644 --- a/doc/http-client.txt +++ b/doc/http-client.txt @@ -83,6 +83,13 @@ If you'd like to pass form-encoded data, set your body like this: > See `examples/examples.txt` for more examples. +Use the variable TIMEOUT_S either as global or local variable to get a timeout +if there's no response from the server or the connection can't be made to the +server for the given number of seconds. The timeout value should be an integer. + +Set value as # $TIMEOUT_S=30 to get a global timeout of 30 seconds for all +requests and # :TIMEOUT_S=30 in a block to set timeout on a individual request. + The output appears in a new split. Syntax highlighting is interpreted from the Content-Type header of the result. It currently supports XML, JSON, and HTML; all others will get ft=text. diff --git a/plugin/http_client.py b/plugin/http_client.py index 6c152e0..274ebc0 100644 --- a/plugin/http_client.py +++ b/plugin/http_client.py @@ -18,6 +18,8 @@ GLOBAL_VAR_REGEX = re.compile('^# ?(\$[^$ ]+)\\s*=\\s*(.+)$') FILE_REGEX = re.compile("!((?:file)|(?:(?:content)))\((.+)\)") JSON_REGEX = re.compile("(javascript|json)$", re.IGNORECASE) +GLOBAL_TIMEOUT_REGEX = re.compile(r"^#\s*\$TIMEOUT_S\s*=\s*(\d+)") +TIMEOUT_REGEX = re.compile(r"^#\s*:TIMEOUT_S\s*=\s*(\d+)") verify_ssl = vim.eval('g:http_client_verify_ssl') == '1' @@ -32,10 +34,23 @@ def is_comment(s): return s.startswith('#') +def get_timeout(buffer, regex): + timeout = None + for line in buffer: + match = regex.match(line) + if match: + timeout = int(match.groups()[0]) + return timeout + + def do_request(block, buf): variables = dict((m.groups() for m in (GLOBAL_VAR_REGEX.match(l) for l in buf) if m)) variables.update(dict((m.groups() for m in (VAR_REGEX.match(l) for l in block) if m))) + global_timeout = get_timeout(buf, GLOBAL_TIMEOUT_REGEX) + local_timeout = get_timeout(block, TIMEOUT_REGEX) + timeout = local_timeout if local_timeout is not None else global_timeout + block = [line for line in block if not is_comment(line) and line.strip() != ''] if len(block) == 0: @@ -87,7 +102,8 @@ def to_file(expr): json_data = json.loads(data) data = None - response = requests.request(method, url, verify=verify_ssl, headers=headers, data=data, files=files, json=json_data) + response = requests.request(method, url, verify=verify_ssl, headers=headers, + data=data, files=files, json=json_data, timeout=timeout) content_type = response.headers.get('Content-Type', '').split(';')[0] response_body = response.text