From f42d5e2666022c891f8561c24489bc52e9f4f642 Mon Sep 17 00:00:00 2001 From: Johnson Denen Date: Thu, 13 Apr 2017 08:59:34 -0400 Subject: Use synchronous calls for simpler JSON parsing --- lisp/mastodon-http.el | 19 ++++++++++++++++--- test/mastodon-http-tests.el | 5 +++-- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/lisp/mastodon-http.el b/lisp/mastodon-http.el index 91036be..b6cc879 100644 --- a/lisp/mastodon-http.el +++ b/lisp/mastodon-http.el @@ -109,12 +109,25 @@ If response code is not 2XX, switches to the response buffer created by `url-ret (funcall success) (switch-to-buffer (current-buffer)))) -(defun mastodon-http--get (url callback) +(defun mastodon-http--get (url) "Make GET request to URL. Pass response buffer to CALLBACK function." - (let ((url-request-method "GET")) - (url-retrieve url callback))) + (let ((url-request-method "GET") + (url-request-extra-headers + `(("Authorization" . ,(concat "Bearer " + (mastodon--access-token)))))) + (url-retrieve-synchronously url))) + +(defun mastodon-http--get-json (url) + "Make GET request to URL. Return JSON response vector." + (let ((json-vector + (with-current-buffer (mastodon-http--get url) + (goto-char (point-min)) + (re-search-forward "^$" nil 'move) + (let ((json-string (buffer-substring-no-properties (point) (point-max)))) + (json-read-from-string json-string))))) + json-vector)) (provide 'mastodon-http) ;;; mastodon-http.el ends here diff --git a/test/mastodon-http-tests.el b/test/mastodon-http-tests.el index afc8fe3..55f6bf3 100644 --- a/test/mastodon-http-tests.el +++ b/test/mastodon-http-tests.el @@ -6,5 +6,6 @@ "Should make a `url-retrieve' of the given URL." (let ((callback-double (lambda () "double"))) (with-mock - (mock (url-retrieve "https://foo.bar/baz" 'callback-double)) - (mastodon-http--get "https://foo.bar/baz" 'callback-double)))) + (mock (url-retrieve-synchronously "https://foo.bar/baz")) + (mock (mastodon--access-token) => "test-token") + (mastodon-http--get "https://foo.bar/baz")))) -- cgit v1.2.3