想到了一个奇葩的解决办法,用全局变量,我看了Info文档,似乎参数可以先eval的。然后我用如下的解决办法:
(defmacro syncthing--rest-api (method endpoint &optional body &rest arglist)
"The Syncthing REST API macro to construct HTTP METHOD, ENDPOINT with BODY and ARGLIST."
`(defun ,(intern (format "syncthing-api-%s-%s" (eval method) (eval endpoint))) ,arglist
(let ((url-request-method ,method)
(url-request-extra-headers '(("X-API-Key" . ,syncthing-api-key))))
(with-current-buffer
(url-retrieve-synchronously
(format "%s:%s%s" syncthing-server syncthing-port ,endpoint))
(goto-char (point-min))
(re-search-forward "^$")
(let ((result (json-read)))
result))
,@body)))
;;; TODO remove this temporary setting after finished
(setq syncthing-api-key "n9mrv83iyememn3sgbc7z291k6o07r")
(defvar syncthing--http-rest-endpoints-alist
'(("GET" . "/rest/system/browse")
("GET" . "/rest/system/config")
("GET" . "/rest/system/config/isync")))
(defvar syncthing--http-rest-endpoint nil
"A temporary variable used in mapcar loop.")
(defun syncthing--http-rest-endpoints-initialize ()
"Initialize HTTP RESTful endpoints API functions."
(mapcar
(lambda (pair)
(setq syncthing--http-rest-endpoint pair)
;; (syncthing--rest-api "GET" "/rest/system/config")
(syncthing--rest-api (car syncthing--http-rest-endpoint) (cdr syncthing--http-rest-endpoint)))
syncthing--http-rest-endpoints-alist))