From 01aac931f90c1251ed6b3c30df672cf3073b16c5 Mon Sep 17 00:00:00 2001 From: Artur Malabarba Date: Sun, 4 Jan 2015 23:03:53 -0200 Subject: Implement sx-request-get-data for getting data from the data branch. --- sx-request.el | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) (limited to 'sx-request.el') diff --git a/sx-request.el b/sx-request.el index ebc16d2..e91ac18 100644 --- a/sx-request.el +++ b/sx-request.el @@ -212,6 +212,35 @@ require authentication. Currently returns nil." '(())) + +;;; Our own generated data +(defvar sx-request--data-url-format + "https://raw.githubusercontent.com/vermiculus/sx.el/data/data/%s.el" + "Url of the \"data\" directory inside the SX `data' branch.") + +(defun sx-request-get-data (file) + "Fetch and return data stored online by SX. +FILE is a string or symbol, the name of the file which holds the +desired data, relative to `sx-request--data-url-format'. For +instance, `tags/emacs' returns the list of tags on Emacs.SE." + (let* ((url-automatic-caching t) + (url-inhibit-uncompression t) + (request-url (format sx-request--data-url-format file)) + (url-request-method "GET") + (url-request-extra-headers + '(("Content-Type" . "application/x-www-form-urlencoded"))) + (response-buffer (url-retrieve-synchronously request-url))) + (if (not response-buffer) + (error "Something went wrong in `url-retrieve-synchronously'") + (with-current-buffer response-buffer + (progn + (goto-char (point-min)) + (if (not (search-forward "\n\n" nil t)) + (error "Headers missing; response corrupt") + (when (looking-at-p "Not Found") (error "Page not found.")) + (prog1 (read (current-buffer)) + (kill-buffer (current-buffer))))))))) + ;;; Support Functions (defun sx-request--build-keyword-arguments (alist &optional kv-sep) -- cgit v1.2.3 From f099001018a8436f1843002e9192a4bf160e5a76 Mon Sep 17 00:00:00 2001 From: Sean Allred Date: Tue, 6 Jan 2015 10:43:31 -0500 Subject: Hotfix kill response buffers after request --- sx-request.el | 1 + 1 file changed, 1 insertion(+) (limited to 'sx-request.el') diff --git a/sx-request.el b/sx-request.el index 0d618d5..6f95687 100644 --- a/sx-request.el +++ b/sx-request.el @@ -191,6 +191,7 @@ the main content of the response is returned." ;; RESPONSE to 'corrupt or something (response (with-demoted-errors "`json' error: %S" (json-read-from-string data)))) + (kill-buffer response-buffer) (when (and (not response) (string-equal data "{}")) (sx-message "Unable to parse response: %S" response) (error "Response could not be read by `json-read-from-string'")) -- cgit v1.2.3