How to connect emacs before json service?

I don't know much about emacs programming, although I know how to program in Lisp languages. I have a service that monitors the project directory and gives results through a web interface similar to rest. I would like to connect emacs to this service and possibly use this circuit to program some functions for emacs. I'm not quite sure where to start with emacs. So which libraries are best suited for working with a web service?

+4
source share
2 answers

Emacs has pretty good built-in support for these kinds of things, at least at the basic level of parsing requests and responses. Use url-retrieveor url-retrieve-synchronouslyto retrieve data from a remote service. As the names show, one is an asynchronous call that receives a callback, and the other is a blocking call that returns a buffer containing the response. Download them by including them (require 'url)in your Elisp file.

url- Elisp "URL", . GET, URL-, url-build-query-string . POST, PUT, DELETE url-request-data, url-request-method url-request-extra-headers. .

URL- HTTP , HTTP , , , . - () url-http-end-of-headers, , , .

json-read JSON json-array-type, json-object-type json-key-type JSON Lisp. (require 'json). XML- xml-parse-region libxml-xml-parse-region. , Emacs libxml, Elisp.

, JSON :

(url-retrieve
 "http://example.com/api/some/request"
 (lambda (events)
   (goto-char url-http-end-of-headers)
   (let ((json-object-type 'plist)
         (json-key-type 'symbol)
         (json-array-type 'vector))
     (let ((result (json-read)))
       ;; Do something with RESULT here
       ))))

result, Lisp. plist-get, pcase cl-destructuring-bind . (require 'pcase), pcase, (require 'cl-lib), cl-destructuring-bind.

+8

, json webservice rate-exchange.appspot.com:

(defun my-json-get (url)
  (interactive)
  (progn
    (require 'json)
    (with-current-buffer (url-retrieve-synchronously url)
      (goto-char (point-min))
      (re-search-forward "^$")
      (json-read))))
(defun my-currency-exchange-rate (from to)
  (let ((xurl (format "http://rate-exchange.appspot.com/currency?from=%s&to=%s" from to)))
    (assoc-default 'rate (my-json-get xurl))))

:

(my-currency-exchange-rate "USD" "SEK")
0

All Articles