Remove the dependency on request.el

This commit is contained in:
louie 2020-02-10 19:51:02 +08:00
parent 0d219e4118
commit df2a90c9b8

View file

@ -5,7 +5,7 @@
;; Description: Make Anki Cards in Org-mode ;; Description: Make Anki Cards in Org-mode
;; Author: Lei Tan ;; Author: Lei Tan
;; Version: 0.3.3 ;; Version: 0.3.3
;; Package-Requires: ((emacs "25.1") (request "0.3.0")) ;; Package-Requires: ((emacs "25.1"))
;; URL: https://github.com/louietan/anki-editor ;; URL: https://github.com/louietan/anki-editor
;; ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
@ -63,7 +63,6 @@
(require 'org-element) (require 'org-element)
(require 'ox) (require 'ox)
(require 'ox-html) (require 'ox-html)
(require 'request)
(defconst anki-editor-prop-note-type "ANKI_NOTE_TYPE") (defconst anki-editor-prop-note-type "ANKI_NOTE_TYPE")
(defconst anki-editor-prop-note-id "ANKI_NOTE_ID") (defconst anki-editor-prop-note-id "ANKI_NOTE_ID")
@ -118,6 +117,40 @@ form entries."
;;; AnkiConnect ;;; AnkiConnect
(cl-defun anki-editor--fetch (url
&rest settings
&key type data success error parser
&allow-other-keys)
"This is a simplistic little function to make http requests using cURL.
The api is borrowed from request.el. It exists because
request.el's sync mode calls cURL asynchronously under the hood,
which doesn't work on some machines (like mine) where the process
sentinel never gets called. After some debugging of Emacs, it
seems that in 'process.c' the pselect syscall to the file
descriptor of inotify used by 'autorevert' always returns a
nonzero value and causes 'status_notify' never being called. To
determine whether it's a bug in Emacs and make a patch requires
more digging."
(let ((tempfile (make-temp-file "emacs-anki-editor"))
(responsebuf (generate-new-buffer " *anki-editor-curl*")))
(with-temp-file tempfile
(setq buffer-file-coding-system 'utf-8)
(set-buffer-multibyte t)
(insert data))
(unwind-protect
(with-current-buffer responsebuf
(apply #'call-process "curl" nil t nil (list
url
"--silent"
"-X" type
"--data-binary"
(concat "@" tempfile)))
(goto-char (point-min))
(apply success (list :data (funcall parser))))
(kill-buffer responsebuf)
(delete-file tempfile))))
(defun anki-editor-api-call (action &rest params) (defun anki-editor-api-call (action &rest params)
"Invoke AnkiConnect with ACTION and PARAMS." "Invoke AnkiConnect with ACTION and PARAMS."
(let ((payload (list :action action :version anki-editor-api-version)) (let ((payload (list :action action :version anki-editor-api-version))
@ -128,17 +161,17 @@ form entries."
(when params (when params
(plist-put payload :params params)) (plist-put payload :params params))
(request (format "http://%s:%s" (anki-editor--fetch (format "http://%s:%s"
anki-editor-api-host anki-editor-api-host
anki-editor-api-port) anki-editor-api-port)
:type "POST" :type "POST"
:parser 'json-read :parser 'json-read
:data (json-encode payload) :data (json-encode payload)
:success (cl-function (lambda (&key data &allow-other-keys) :success (cl-function (lambda (&key data &allow-other-keys)
(setq reply data))) (setq reply data)))
:error (cl-function (lambda (&key _ &key error-thrown &allow-other-keys) :error (cl-function (lambda (&key error-thrown &allow-other-keys)
(setq err (string-trim (cdr error-thrown))))) (setq err (string-trim (cdr error-thrown)))))
:sync t) :sync t)
(when err (error "Error communicating with AnkiConnect using cURL: %s" err)) (when err (error "Error communicating with AnkiConnect using cURL: %s" err))
(or reply (error "Got empty reply from AnkiConnect")))) (or reply (error "Got empty reply from AnkiConnect"))))