-
Notifications
You must be signed in to change notification settings - Fork 20
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
Set a verb-var from the result of a request #59
Comments
That is a good idea! In order to make it more consistent with (defun example (res)
(verb-set-var 'token (verb-json-get (oref res body) "access_token"))) And then:
A lambda could also be specified there. |
I think I would either define a But in the end anything that allows setting a "token" variable would do the job for me :-). |
A bit late to the party, but (defun verb--apply-response-fn ()
"Apply function in VERB-MAP-RESPONSE property to response object."
(when-let ((res verb-last)
(rs (oref res request))
(form (thread-first
(format "%smap-response" verb--metadata-prefix)
(assoc-string (oref rs metadata) t)
cdr
verb--nonempty-string))
(fn-form (verb--try-read-fn-form form))
(fn (cond
((functionp form) form)
((listp form) (eval form)))))
(with-current-buffer (other-buffer (current-buffer) 'visible-ok)
(condition-case nil
(funcall fn res)
(error (message "Couldn't parse `%s' as a function." fn))))))
(add-hook 'verb-post-response-hook #'verb--apply-response-fn) I have been using that particular piece of code for over a year now with no hiccups as far as I remember. |
Nice! Would you mind opening a PR for it? Note that the |
Definitely could, but do we really need to add it to the code base? I would think that adding a section to the README would be enough for whoever wishes to have that behaviour to add that function to the response hook.
LOL I added that function and forgot to update my hook :P |
Yes we could add it to the README, but I feel like this function specifically is useful enough to have it integrated in the package (for example, last week I also ran into a case where it would've been useful to have it). Since Verb as a package is relatively small I don't mind adding more stuff for the moment - and I also like it being "batteries included" when possible. |
Yes we could add it to the README, but I feel like this function specifically is useful enough to have it integrated in the package (for example, last week I also ran into a case where it would've been useful to have it). Since Verb as a package is relatively small I don't mind adding more stuff for the moment - and I also like it being "batteries included" when possible.
All right, I'll be creating the PR soon then.
…--
João Pedro de A. Paula
IT bachelors at Universidade Federal do Rio Grande do Norte (UFRN)
|
I have created the PR to get some early reviews, but I still need to add tests for the new function that parses forms into function objects plus fixing the tests for the previous implementation of |
Amazing! Thank you, let me know when I can review it |
This is similar but not quite the same as https://github.com/federicotdn/verb#storing-responses-by-key.
Suppose, for instance, that one would like to extract a login token and store it in a verb-var automatically after calling a login endpoint. It seems that a nice way to do this (and many other things) would be by specifying, as a request property, a hook that is run after the request completes:
What do you think?
The text was updated successfully, but these errors were encountered: