federicotdn / verb

Organize and send HTTP requests from Emacs
https://melpa.org/#/verb
GNU General Public License v3.0
540 stars 20 forks source link

Looping over results, executing request for every item #26

Closed hjudt closed 4 years ago

hjudt commented 4 years ago

Is it somehow possible to loop over results, sending requests for each item in the results? For example, i have a list of ids which i fetch via get, then for each of these i want to execute a delete request.

federicotdn commented 4 years ago

Hey @hjudt, I think you can achieve this by combining the Verb-Store property and a bit of Elisp.

First you would need to store the result of GETting the IDs using Verb-Store under, say, my-ids:

* Example          :verb:
:properties:
:Verb-Store: my-ids
:end:
get http://example.com/api/get-ids

Now, calling (verb-stored-response "my-ids") will return a verb-response EIEIO object, with all the information about that response. You can access JSON information inside there using:

(verb-json-get (oref (verb-stored-response "my-ids") body) "ids")

...assuming the response JSON was:

{"ids": [1, 2, 3, 4]}

...then the above Elisp will return:

[1 2 3 4]

From there, you can write an Elisp function yourself that calls verb-send-request-on-point with WHERE = t, calling (verb-set-var "next-id" ...) with each ID, and having the point on top of the following request specification:

* Example          :verb:
delete http://example.com/api/erase-id/{{(verb-var "next-id")}}