dotemacs

My Emacs configuration
git clone git://git.entf.net/dotemacs
Log | Files | Refs | LICENSE

restclient-jq.el (2890B)


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