From 9bd67acab3c4c81cf4bad14b2da8be835ae086e0 Mon Sep 17 00:00:00 2001 From: Artur Malabarba Date: Fri, 23 Jan 2015 23:29:54 -0200 Subject: Refactor part of request-get-data into request-get-url --- sx-request.el | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) (limited to 'sx-request.el') diff --git a/sx-request.el b/sx-request.el index 2650c55..77ae1d7 100644 --- a/sx-request.el +++ b/sx-request.el @@ -221,18 +221,14 @@ Currently returns nil." "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." +(defun sx-request-get-url (url) + "Fetch and return data stored online at URL." (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))) + (response-buffer (url-retrieve-synchronously url))) (if (not response-buffer) (error "Something went wrong in `url-retrieve-synchronously'") (with-current-buffer response-buffer @@ -241,9 +237,17 @@ instance, `tags/emacs' returns the list of tags on Emacs.SE." (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)) + (prog1 (buffer-substring (point) (point-max)) (kill-buffer (current-buffer))))))))) +(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." + (read (sx-request-get-url + (format sx-request--data-url-format file)))) + ;;; Support Functions (defun sx-request--build-keyword-arguments (alist &optional kv-sep) -- cgit v1.2.3 From 4bc72b0f622b2565e7b0d0263bb3053ca5252f63 Mon Sep 17 00:00:00 2001 From: Artur Malabarba Date: Fri, 23 Jan 2015 23:38:06 -0200 Subject: sx-request-get-url error unless code 200 --- sx-request.el | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'sx-request.el') diff --git a/sx-request.el b/sx-request.el index 77ae1d7..7f18a2b 100644 --- a/sx-request.el +++ b/sx-request.el @@ -234,9 +234,10 @@ Currently returns nil." (with-current-buffer response-buffer (progn (goto-char (point-min)) + (unless (string-match "200" (thing-at-point 'line)) + (error "Page not found.")) (if (not (search-forward "\n\n" nil t)) (error "Headers missing; response corrupt") - (when (looking-at-p "Not Found") (error "Page not found.")) (prog1 (buffer-substring (point) (point-max)) (kill-buffer (current-buffer))))))))) -- cgit v1.2.3 From 7ec0b2de5f1458354db7068e936d58ba9914c0a8 Mon Sep 17 00:00:00 2001 From: Artur Malabarba Date: Thu, 12 Feb 2015 21:05:03 -0200 Subject: Make sx-request-get-url support asynchronous fetching --- sx-request.el | 47 +++++++++++++++++++++++++++++++++-------------- 1 file changed, 33 insertions(+), 14 deletions(-) (limited to 'sx-request.el') diff --git a/sx-request.el b/sx-request.el index 7f18a2b..2e650b4 100644 --- a/sx-request.el +++ b/sx-request.el @@ -221,25 +221,44 @@ Currently returns nil." "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-url (url) - "Fetch and return data stored online at URL." +(defun sx-request--read-buffer-data () + "Return the buffer contents after any url headers. +Error if url headers are absent or if they indicate something +went wrong." + (goto-char (point-min)) + (unless (string-match "200" (thing-at-point 'line)) + (error "Page not found.")) + (if (not (search-forward "\n\n" nil t)) + (error "Headers missing; response corrupt") + (prog1 (buffer-substring (point) (point-max)) + (kill-buffer (current-buffer))))) + +(defun sx-request-get-url (url &optional callback) + "Fetch and return data stored online at URL. +If CALLBACK is nil, fetching is done synchronously and the +data (buffer contents sans headers) is returned as a string. + +Otherwise CALLBACK must be a function of a single argument. Then +`url-retrieve' is called asynchronously and CALLBACK is passed +the retrieved data." (let* ((url-automatic-caching t) (url-inhibit-uncompression t) (url-request-method "GET") (url-request-extra-headers '(("Content-Type" . "application/x-www-form-urlencoded"))) - (response-buffer (url-retrieve-synchronously url))) - (if (not response-buffer) - (error "Something went wrong in `url-retrieve-synchronously'") - (with-current-buffer response-buffer - (progn - (goto-char (point-min)) - (unless (string-match "200" (thing-at-point 'line)) - (error "Page not found.")) - (if (not (search-forward "\n\n" nil t)) - (error "Headers missing; response corrupt") - (prog1 (buffer-substring (point) (point-max)) - (kill-buffer (current-buffer))))))))) + (callback-internal + (when callback + ;; @TODO: Error check in STATUS. + (lambda (_status) + (funcall callback (sx-request--read-buffer-data))))) + (response-buffer + (if callback (url-retrieve url callback-internal nil 'silent) + (url-retrieve-synchronously url)))) + (unless callback + (if (not response-buffer) + (error "Something went wrong in `url-retrieve-synchronously'") + (with-current-buffer response-buffer + (sx-request--read-buffer-data)))))) (defun sx-request-get-data (file) "Fetch and return data stored online by SX. -- cgit v1.2.3