diff options
author | marty hiatt <martianhiatus [a t] riseup [d o t] net> | 2022-08-07 18:06:37 +0200 |
---|---|---|
committer | marty hiatt <martianhiatus [a t] riseup [d o t] net> | 2022-08-07 18:06:37 +0200 |
commit | a44de346afbce43e4cd0d79280a45e2d2cd68758 (patch) | |
tree | c99dde858f576b7620ef8b698737cb8d7326d8dc | |
parent | 44e8e75273b692fa69b9f9997046b342c364712d (diff) |
expand authorized-request macro to hand POST requests
-rw-r--r-- | lisp/mastodon-http.el | 53 |
1 files changed, 27 insertions, 26 deletions
diff --git a/lisp/mastodon-http.el b/lisp/mastodon-http.el index 9c09996..48046c2 100644 --- a/lisp/mastodon-http.el +++ b/lisp/mastodon-http.el @@ -97,38 +97,39 @@ Message status and JSON error from RESPONSE if unsuccessful." (insert-file-contents filename) (string-to-unibyte (buffer-string)))) -(defun mastodon-http--post (url args headers &optional unauthenticed-p) - "POST synchronously 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 - (unless unauthenticed-p - `(("Authorization" . ,(concat "Bearer " (mastodon-auth--access-token))))) - ;; pleroma compatibility: - (unless (assoc "Content-Type" headers) - '(("Content-Type" . "application/x-www-form-urlencoded"))) - headers))) - (with-temp-buffer - (mastodon-http--url-retrieve-synchronously url)))) - -(defmacro mastodon-http--authorized-request (method body) +(defmacro mastodon-http--authorized-request (method body &optional unauthenticated-p) "Make a METHOD request with Mastodon authorization." `(let ((url-request-method ,method) (url-request-extra-headers + (unless ,unauthenticated-p (("Authorization" - (concat "Bearer " (mastodon-auth--access-token)))))) + (concat "Bearer " (mastodon-auth--access-token))))))) ,body)) +(defun mastodon-http--post (url args headers &optional unauthenticated-p) + "POST synchronously to URL with ARGS and HEADERS. + +Authorization header is included by default unless UNAUTHENTICATED-P is non-nil." + (mastodon-http--authorized-request + "POST" + (let ((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 url-request-extra-headers ; auth set in macro + ;; pleroma compat: + (unless (assoc "Content-Type" headers) + '(("Content-Type" . "application/x-www-form-urlencoded"))) + headers))) + (with-temp-buffer + (mastodon-http--url-retrieve-synchronously url)) + unauthenticated-p))) + (defun mastodon-http--get (url) "Make synchronous GET request to URL. |