aboutsummaryrefslogtreecommitdiff
path: root/sx-request.el
diff options
context:
space:
mode:
Diffstat (limited to 'sx-request.el')
-rw-r--r--sx-request.el29
1 files changed, 29 insertions, 0 deletions
diff --git a/sx-request.el b/sx-request.el
index 6f95687..f892367 100644
--- a/sx-request.el
+++ b/sx-request.el
@@ -216,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.