diff options
author | Artur Malabarba <bruce.connor.am@gmail.com> | 2015-01-07 22:50:58 -0200 |
---|---|---|
committer | Artur Malabarba <bruce.connor.am@gmail.com> | 2015-01-07 22:50:58 -0200 |
commit | 976cf5e95dce4fd4d6f74350c8b9ea66aa13c0fd (patch) | |
tree | 683e71610f1f8e0faf736e4b5c293d70c2caa69e /sx-request.el | |
parent | a11d7997aa61f6e5bcb5fb6974c25b914b3bd001 (diff) | |
parent | 95e9f6069ba328249c0e8ac86efdc888437ac187 (diff) |
Merge branch 'master' into vermiculus/bot-branch
Diffstat (limited to 'sx-request.el')
-rw-r--r-- | sx-request.el | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/sx-request.el b/sx-request.el index 0d618d5..f892367 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'")) @@ -215,6 +216,35 @@ 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) "Format ALIST as a key-value list joined with KV-SEP. |