diff options
author | Ian Eure <ian@retrospec.tv> | 2020-05-03 13:56:14 -0700 |
---|---|---|
committer | mousebot <mousebot@riseup.net> | 2021-05-09 11:22:59 +0200 |
commit | 4b621f58d294d7ab67ee4c800cd2777541bc1bee (patch) | |
tree | 1625541bdc91c7a30d9725027ef755c6cafb4183 /lisp/mastodon-http.el | |
parent | 416709661936d16a854b15c0622ae5d29e2f50c8 (diff) |
SWAG at moving to an async network model.
Diffstat (limited to 'lisp/mastodon-http.el')
-rw-r--r-- | lisp/mastodon-http.el | 63 |
1 files changed, 51 insertions, 12 deletions
diff --git a/lisp/mastodon-http.el b/lisp/mastodon-http.el index a6e9c92..a5f88d7 100644 --- a/lisp/mastodon-http.el +++ b/lisp/mastodon-http.el @@ -113,18 +113,57 @@ Pass response buffer to CALLBACK function." (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 - (decode-coding-string - (buffer-substring-no-properties (point) (point-max)) - 'utf-8))) - (kill-buffer) - (json-read-from-string json-string))))) - json-vector)) + "Make GET request to URL. Return JSON response" + (with-current-buffer (mastodon-http--get url) + (mastodon-http--process-json))) + +(defun mastodon-http--process-json () + (goto-char (point-min)) + (re-search-forward "^$" nil 'move) + (let ((json-string + (decode-coding-string + (buffer-substring-no-properties (point) (point-max)) + 'utf-8))) + (kill-buffer) + (json-read-from-string json-string))) + + ;; Asynchronous functions + +(defun mastodon-http--get-async (url &optional callback &rest cbargs) + "Make GET request to URL. + +Pass response buffer to CALLBACK function." + (let ((url-request-method "GET") + (url-request-extra-headers + `(("Authorization" . ,(concat "Bearer " + (mastodon-auth--access-token)))))) + (url-retrieve url callback cbargs mastodon-http--timeout))) + +(defun mastodon-http--get-json-async (url &optional callback &rest args) + "Make GET request to URL. Call CALLBACK with json-vector and ARGS." + (mastodon-http--get-async + url + (lambda (status) + (apply callback (mastodon-http--process-json) args)))) + +(defun mastodon-http--post-async (url args headers &optional callback &rest cbargs) + "POST asynchronously to URL with ARGS and HEADERS. + +Authorization header is included by default unless UNAUTHENTICED-P is non-nil." + (let ((url-request-method "POST") + (url-request-data + (when args + (mapconcat (lambda (arg) + (concat (url-hexify-string (car arg)) + "=" + (url-hexify-string (cdr arg)))) + args + "&"))) + (url-request-extra-headers + (append `(("Authorization" . ,(concat "Bearer " (mastodon-auth--access-token)))) + headers))) + (with-temp-buffer + (url-retrieve url callback cbargs mastodon-http--timeout)))) (provide 'mastodon-http) ;;; mastodon-http.el ends here |