[Feature] Response handlers #100
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR adds possibility to parse response and extract info which can be used in following requests.
(Implements #99)
It adds support of response handlers.
How it works
Response handler is effectively a comment of following format:
where
{prefix}
defines the scope of handler (see below),{target}
defines what to do with result, and{pattern}
defines how to parse response to get the result.There are three types of handler depending on
{target}
:{target}
is non-empty string it is considered a substitution handler. It replaces remaining part of the line with{pattern}
output. e.g. when{target}
isAuthorization:
then lines which definesAuthorization
header will be replaced with respective value extracted from response.{target}
is empty string it is considered trace handler. It outputs all pattern results to quickfix list.{target}
is?
string it is considered assertion handler. In this case output of{pattern}
is ignored but response code is considered. When response code isfalse
(i.e. non-0
) then it terminates execution of current block. See examples below.{pattern}
is any shell command which consumes response and produces some output used by{target}
.Handler can have one of following scopes depending on
{prefix}
value:>
- local scope. This handler consumes output of current request and makes substitutions (when it is substitution handler) only in current block and in global section. These handlers executed automatically right after request where they are defined. For this handler if target is?
and{pattern}
exit code is non-0
then it terminates current block execution.%
- global scope. This handler consumes whole content of VRC output buffer and makes substitutions globally. It is ignored during usual request launch. These handlers can be invoked with:call VrcHandleResponse()
for current line or selected region.Examples
here we have
Authorization
header defined in global scope. First request returns some json withaccess_token
field which can be used as bearer token for call which requires auth. This handler executed right after first request, extracts access token and put it in defined header. Second request will be executed with respective Authorization header.First we GET "user" resource. Handler extracts it's
id
from response and place it inuserId
parameter. In second request we use thisuserId
to update resource.in this example second request will be executed ONLY when first responds with 404.
This handler won't be executed automatically. Put cursor on it (or visually select line) and
:call VrcHandleResponse()
. It will show in quickfix list something like:i.e. first and second resource exist. Last one - not. This types of handlers can be used to generate some kind of report after execution of series of requests. Note: this handler uses as input whole content of VRC output buffer.
You can automate it even more. For example you need to quickly ensure that all requests from your batch finished with success (e.g. 200), then you can do:
it will produce in quickfix list:
if all requests respond with 200, or
False
otherwise.You can visually select several handlers and
:call VrcHandleResponse()
for region. They will be executed line by line. Output will be appended to quickfix list.It is possible to use some external scripts in handlers which can do even more sophisticated logic.