about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorDavid Morgan <djm_uk@protonmail.com>2023-05-26 16:21:09 +0100
committerDavid Morgan <djm_uk@protonmail.com>2023-05-26 16:21:09 +0100
commit9030d959d06adb448676f464830dabd52e63e1d9 (patch)
tree2d8d6464568af65a3fdb71a3cc3c43db9312a255
parent6ea07273e7d8b190e3fa8eb29634da89ecde3051 (diff)
downloaddotfiles-9030d959d06adb448676f464830dabd52e63e1d9.tar.gz
Extend restclient to allow saving requests and calling them
-rw-r--r--.emacs.d/lisp/init-misc.el32
1 files changed, 32 insertions, 0 deletions
diff --git a/.emacs.d/lisp/init-misc.el b/.emacs.d/lisp/init-misc.el
index d5e06a3..c5684f9 100644
--- a/.emacs.d/lisp/init-misc.el
+++ b/.emacs.d/lisp/init-misc.el
@@ -15,6 +15,38 @@
   :hook (elpaca-after-init . envrc-global-mode))
 
 (use-package restclient
+  :config
+  (defvar restclient-saved-requests nil)
+  (defun restclient-save-current (label)
+    "Save the current request as `label' (or use a default based on method, url and entity)."
+    (interactive "sLabel: ")
+    (restclient-http-parse-current-and-do
+     `(lambda (method url headers entity)
+        (let ((lab (if (string-empty-p ,label)
+                       (format "%s %s (%s ...)" method url (substring entity 0 (min (length entity) 200)))
+                     ,label)))
+          (push (cons lab (list method url headers entity nil nil)) restclient-saved-requests)))))
+
+  (defun restclient-delete-saved-request ()
+    "Delete a saved request."
+    (interactive)
+    (if (= 0 (length restclient-saved-requests))
+        (message "No saved restclient requests to delete.")
+      (setq restclient-saved-requests
+            (assoc-delete-all (completing-read "Delete: " (map-keys restclient-saved-requests)) restclient-saved-requests))))
+
+  (defun restclient-call-saved-request ()
+    "Call a saved request."
+    (interactive)
+    (if (= 0 (length restclient-saved-requests))
+        (message "No saved restclient requests found.")
+      (let ((args (if (= 1 (length restclient-saved-requests))
+                      (cdar restclient-saved-requests)
+                    (alist-get (completing-read "Call: " (map-keys restclient-saved-requests)) restclient-saved-requests nil nil 'string-equal))))
+        (apply 'restclient-http-do args))))
+  :bind
+  ("C-c C-h" . restclient-call-saved-request)
+  (:map restclient-mode-map ("C-c h" . restclient-save-current))
   :mode (("\\.http\\'" . restclient-mode)))
 
 (use-package restclient-jq