forked from pashky/restclient.el
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrestclient-jq.el
81 lines (68 loc) · 2.58 KB
/
restclient-jq.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
;;; restclient-jq.el --- Support for setting restclient vars from jq expressions -*- lexical-binding: t; -*-
;;
;; Public domain.
;; URL: https://github.com/pashky/restclient.el
;; Author: Cameron Dorrat <[email protected]>
;; Maintainer: Cameron Dorrat <[email protected]>
;; Created: 26 Apr 2020
;; Keywords: http jq
;; Package-Requires: ((restclient "20200502.831") (jq-mode "0.4.1") (emacs "24.4"))
;; This file is not part of GNU Emacs.
;; This file is public domain software. Do what you want.
;;; Commentary:
;;
;; This is a companion to restclient.el to add support for setting variables from results using jq expressions
;;; Code:
;;
(require 'jq-mode)
;; --- jq support
(defun restclient-jq-result-end-point ()
(save-excursion
(goto-char (point-max))
(or (and (re-search-backward "^[^/].*" nil t)
(line-end-position))
(point-max))))
(defun restclient-jq-get-var (jq-pattern)
(with-temp-buffer
(let ((output (current-buffer)))
(with-current-buffer restclient-same-buffer-response-name
(call-process-region
(point-min)
(restclient-jq-result-end-point)
shell-file-name
nil
output
nil
shell-command-switch
(format "%s %s %s"
jq-interactive-command
"-r"
(shell-quote-argument jq-pattern))))
(string-trim (buffer-string)))))
(defun restclient-jq-json-var-function (args args-offset)
(save-match-data
(and (string-match "\\(:[^: \n]+\\) \\(.*\\)$" args)
(let ((var-name (match-string 1 args))
(jq-patt (match-string 2 args)))
(lambda ()
(let ((resp-val (restclient-jq-get-var jq-patt)))
(restclient-remove-var var-name)
(restclient-set-var var-name resp-val)
(message "restclient var [%s = \"%s\"] " var-name resp-val)))))))
(defun restclient-jq-interactive-result ()
(interactive)
(flush-lines "^//.*") ;; jq doesnt like comments
(jq-interactively (point-min) (restclient-jq-result-end-point)))
(provide 'restclient-jq)
;; todo: eval-after-load should be used in configuration, not
;; packages. Replace with a better solution.
(eval-after-load 'restclient
'(progn
(restclient-register-result-func
"jq-set-var" #'restclient-jq-json-var-function
"Set a restclient variable with the value jq expression,
takes var & jq expression as args.
eg. -> jq-set-var :my-token .token")
(define-key restclient-response-mode-map (kbd "C-c C-j") #'restclient-jq-interactive-result)))
(provide 'restclient-jq)
;;; restclient-jq.el ends here