ob-restclient.el (5030B)
1 ;;; ob-restclient.el --- org-babel functions for restclient-mode 2 3 ;; Copyright (C) Alf Lervåg 4 5 ;; Author: Alf Lervåg 6 ;; Keywords: literate programming, reproducible research 7 ;; Package-Version: 20220202.1609 8 ;; Package-Commit: 586f1fa07f76aaca13cb3f86945759f4b9fb8db7 9 ;; Homepage: https://github.com/alf/ob-restclient.el 10 ;; Version: 0.02 11 ;; Package-Requires: ((restclient "0")) 12 13 ;;; License: 14 15 ;; This program is free software; you can redistribute it and/or modify 16 ;; it under the terms of the GNU General Public License as published by 17 ;; the Free Software Foundation; either version 3, or (at your option) 18 ;; any later version. 19 ;; 20 ;; This program is distributed in the hope that it will be useful, 21 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of 22 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 23 ;; GNU General Public License for more details. 24 ;; 25 ;; You should have received a copy of the GNU General Public License 26 ;; along with GNU Emacs; see the file COPYING. If not, write to the 27 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 28 ;; Boston, MA 02110-1301, USA. 29 30 ;;; Commentary: 31 ;; This is a very simple first iteration at integrating restclient.el 32 ;; and org-mode. 33 34 ;;; Requirements: 35 ;; restclient.el 36 37 ;;; Code: 38 (require 'ob) 39 (require 'ob-ref) 40 (require 'ob-comint) 41 (require 'ob-eval) 42 (require 'restclient) 43 44 (defvar org-babel-default-header-args:restclient 45 `((:results . "raw")) 46 "Default arguments for evaluating a restclient block.") 47 48 (defcustom org-babel-restclient--jq-path "jq" 49 "The path to `jq', for post-processing. Uses the PATH by default") 50 51 ;;;###autoload 52 (defun org-babel-execute:restclient (body params) 53 "Execute a block of Restclient code with org-babel. 54 This function is called by `org-babel-execute-src-block'" 55 (message "executing Restclient source code block") 56 (with-temp-buffer 57 (let ((results-buffer (current-buffer)) 58 (restclient-same-buffer-response t) 59 (restclient-same-buffer-response-name (buffer-name)) 60 (display-buffer-alist 61 (cons 62 '("\\*temp\\*" display-buffer-no-window (allow-no-window . t)) 63 display-buffer-alist))) 64 65 (insert (buffer-name)) 66 (with-temp-buffer 67 (dolist (p params) 68 (let ((key (car p)) 69 (value (cdr p))) 70 (when (eql key :var) 71 (insert (format ":%s = <<\n%s\n#\n" (car value) (cdr value)))))) 72 (insert body) 73 (goto-char (point-min)) 74 (delete-trailing-whitespace) 75 (goto-char (point-min)) 76 (restclient-http-parse-current-and-do 77 'restclient-http-do (org-babel-restclient--raw-payload-p params) t)) 78 79 (while restclient-within-call 80 (sleep-for 0.05)) 81 82 (goto-char (point-min)) 83 (when (equal (buffer-name) (buffer-string)) 84 (error "Restclient encountered an error")) 85 86 (when (or (org-babel-restclient--return-pure-payload-result-p params) 87 (assq :noheaders params) 88 (assq :jq params)) 89 (org-babel-restclient--hide-headers)) 90 91 (when-let* ((jq-header (assoc :jq params)) 92 (jq-path "jq")) 93 (shell-command-on-region 94 (point-min) 95 (point-max) 96 (format "%s %s" org-babel-restclient--jq-path 97 (shell-quote-argument (cdr jq-header))) 98 (current-buffer) 99 t)) 100 101 ;; widen if jq but not pure payload 102 (when (and (assq :jq params) 103 (not (assq :noheaders params)) 104 (not (org-babel-restclient--return-pure-payload-result-p params))) 105 (widen)) 106 107 (when (not (org-babel-restclient--return-pure-payload-result-p params)) 108 (org-babel-restclient--wrap-result)) 109 110 (buffer-string)))) 111 112 (defun org-babel-restclient--wrap-result () 113 "Wrap the contents of the buffer in an `org-mode' src block." 114 (let ((mode-name (substring (symbol-name major-mode) 0 -5))) 115 (insert (format "#+BEGIN_SRC %s\n" mode-name)) 116 (goto-char (point-max)) 117 (insert "#+END_SRC\n"))) 118 119 (defun org-babel-restclient--hide-headers () 120 "Just return the payload." 121 (let ((comments-start 122 (save-excursion 123 (goto-char (point-max)) 124 (while (comment-only-p (line-beginning-position) (line-end-position)) 125 (forward-line -1)) 126 ;; Include the last line as well 127 (forward-line) 128 (point)))) 129 (narrow-to-region (point-min) comments-start))) 130 131 132 (defun org-babel-restclient--return-pure-payload-result-p (params) 133 "Return `t' if the `:results' key in PARAMS contains `value' or `table'." 134 (let ((result-type (cdr (assoc :results params)))) 135 (when result-type 136 (string-match "value\\|table" result-type)))) 137 138 139 (defun org-babel-restclient--raw-payload-p (params) 140 "Return t if the `:results' key in PARAMS contain `file'." 141 (let ((result-type (cdr (assoc :results params)))) 142 (when result-type 143 (string-match "file" result-type)))) 144 145 (provide 'ob-restclient) 146 ;;; ob-restclient.el ends here