Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add timeout duration for the requests #32

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions doc/http-client.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
18 changes: 17 additions & 1 deletion plugin/http_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand All @@ -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:
Expand Down Expand Up @@ -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
Expand Down