From 44e8e75273b692fa69b9f9997046b342c364712d Mon Sep 17 00:00:00 2001 From: marty hiatt Date: Sun, 7 Aug 2022 17:22:28 +0200 Subject: http: --authorized-request macro --- lisp/mastodon-http.el | 94 +++++++++++++++++++++++++-------------------------- 1 file changed, 46 insertions(+), 48 deletions(-) (limited to 'lisp/mastodon-http.el') diff --git a/lisp/mastodon-http.el b/lisp/mastodon-http.el index 35fd070..9c09996 100644 --- a/lisp/mastodon-http.el +++ b/lisp/mastodon-http.el @@ -117,19 +117,25 @@ Authorization header is included by default unless UNAUTHENTICED-P is non-nil." ;; pleroma compatibility: (unless (assoc "Content-Type" headers) '(("Content-Type" . "application/x-www-form-urlencoded"))) - headers))) + headers))) (with-temp-buffer (mastodon-http--url-retrieve-synchronously url)))) +(defmacro mastodon-http--authorized-request (method body) + "Make a METHOD request with Mastodon authorization." + `(let ((url-request-method ,method) + (url-request-extra-headers + (("Authorization" + (concat "Bearer " (mastodon-auth--access-token)))))) + ,body)) + (defun mastodon-http--get (url) "Make synchronous 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)))))) - (mastodon-http--url-retrieve-synchronously url))) +Pass response buffer to CALLBACK function." + (mastodon-http--authorized-request + "GET" + (mastodon-http--url-retrieve-synchronously url))) (defun mastodon-http--get-json (url) "Make synchronous GET request to URL. Return JSON response." @@ -138,6 +144,8 @@ Pass response buffer to CALLBACK function." (defun mastodon-http--process-json () "Process JSON response." + ;; view raw response: + ;; (switch-to-buffer (current-buffer)) (goto-char (point-min)) (re-search-forward "^$" nil 'move) (let ((json-string @@ -150,12 +158,10 @@ Pass response buffer to CALLBACK function." (defun mastodon-http--delete (url) "Make DELETE request to URL." - (let ((url-request-method "DELETE") - (url-request-extra-headers - `(("Authorization" . ,(concat "Bearer " - (mastodon-auth--access-token)))))) - (with-temp-buffer - (mastodon-http--url-retrieve-synchronously url)))) + (mastodon-http--authorized-request + "DELETE" + (with-temp-buffer + (mastodon-http--url-retrieve-synchronously url)))) (defun mastodon-http--append-query-string (url params) "Append PARAMS to URL as query strings and return it. @@ -187,14 +193,12 @@ PARAM is any extra parameters to send with the request." "Make GET request to BASE-URL, searching for QUERY. Pass response buffer to CALLBACK function. PARAM is a formatted request parameter, eg 'following=true'." - (let ((url-request-method "GET") - (url (if param - (concat base-url "?" param "&q=" (url-hexify-string query)) - (concat base-url "?q=" (url-hexify-string query)))) - (url-request-extra-headers - `(("Authorization" . ,(concat "Bearer " - (mastodon-auth--access-token)))))) - (mastodon-http--url-retrieve-synchronously url))) + (mastodon-http--authorized-request + "GET" + (let ((url (if param + (concat base-url "?" param "&q=" (url-hexify-string query)) + (concat base-url "?q=" (url-hexify-string query))))) + (mastodon-http--url-retrieve-synchronously url)))) ;; profile update functions @@ -208,25 +212,21 @@ PARAM is a formatted request parameter, eg 'following=true'." "Make synchronous PATCH request to BASE-URL. Optionally specify the NOTE to edit. Pass response buffer to CALLBACK function." - (let ((url-request-method "PATCH") - (url (if note + (mastodon-http--authorized-request + "PATCH" + (let ((url (if note (concat base-url "?note=" (url-hexify-string note)) - base-url)) - (url-request-extra-headers - `(("Authorization" . ,(concat "Bearer " - (mastodon-auth--access-token)))))) - (mastodon-http--url-retrieve-synchronously url))) + base-url))) + (mastodon-http--url-retrieve-synchronously url)))) ;; Asynchronous functions (defun mastodon-http--get-async (url &optional callback &rest cbargs) "Make GET request to URL. Pass response buffer to CALLBACK function with args CBARGS." - (let ((url-request-method "GET") - (url-request-extra-headers - `(("Authorization" . ,(concat "Bearer " - (mastodon-auth--access-token)))))) - (url-retrieve url callback cbargs))) + (mastodon-http--authorized-request + "GET" + (url-retrieve url callback cbargs))) (defun mastodon-http--get-json-async (url &optional callback &rest args) "Make GET request to URL. Call CALLBACK with json-vector and ARGS." @@ -240,21 +240,19 @@ Pass response buffer to CALLBACK function with args CBARGS." "POST asynchronously to URL with ARGS and HEADERS. Then run function CALLBACK with arguements CBARGS. Authorization header is included by default unless UNAUTHENTICED-P is non-nil." - (let ((url-request-method "POST") - (request-timeout 5) - (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--authorized-request + "POST" + (let ((request-timeout 5) + (url-request-data + (when args + (mapconcat (lambda (arg) + (concat (url-hexify-string (car arg)) + "=" + (url-hexify-string (cdr arg)))) + args + "&")))) + (with-temp-buffer + (url-retrieve url callback cbargs))))) ;; TODO: test for curl first? (defun mastodon-http--post-media-attachment (url filename caption) -- cgit v1.2.3 From a44de346afbce43e4cd0d79280a45e2d2cd68758 Mon Sep 17 00:00:00 2001 From: marty hiatt Date: Sun, 7 Aug 2022 18:06:37 +0200 Subject: expand authorized-request macro to hand POST requests --- lisp/mastodon-http.el | 53 ++++++++++++++++++++++++++------------------------- 1 file changed, 27 insertions(+), 26 deletions(-) (limited to 'lisp/mastodon-http.el') 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. -- cgit v1.2.3 From e59ced7c51403a1f8fb7b312dccfa1516162637a Mon Sep 17 00:00:00 2001 From: marty hiatt Date: Sun, 7 Aug 2022 18:17:39 +0200 Subject: restore (list (cons call in macro --- lisp/mastodon-http.el | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) (limited to 'lisp/mastodon-http.el') diff --git a/lisp/mastodon-http.el b/lisp/mastodon-http.el index 48046c2..8c55534 100644 --- a/lisp/mastodon-http.el +++ b/lisp/mastodon-http.el @@ -102,8 +102,8 @@ Message status and JSON error from RESPONSE if unsuccessful." `(let ((url-request-method ,method) (url-request-extra-headers (unless ,unauthenticated-p - (("Authorization" - (concat "Bearer " (mastodon-auth--access-token))))))) + (list (cons "Authorization" + (concat "Bearer " (mastodon-auth--access-token))))))) ,body)) (defun mastodon-http--post (url args headers &optional unauthenticated-p) @@ -112,23 +112,23 @@ Message status and JSON error from RESPONSE if unsuccessful." 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))) + (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. -- cgit v1.2.3 From d2c300322b5899a420c057301406ca0ad0c53f1f Mon Sep 17 00:00:00 2001 From: marty hiatt Date: Wed, 17 Aug 2022 17:32:03 +0200 Subject: docstrings --- lisp/mastodon-http.el | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'lisp/mastodon-http.el') diff --git a/lisp/mastodon-http.el b/lisp/mastodon-http.el index 8c55534..ec3b5e6 100644 --- a/lisp/mastodon-http.el +++ b/lisp/mastodon-http.el @@ -98,7 +98,8 @@ Message status and JSON error from RESPONSE if unsuccessful." (string-to-unibyte (buffer-string)))) (defmacro mastodon-http--authorized-request (method body &optional unauthenticated-p) - "Make a METHOD request with Mastodon authorization." + "Make a METHOD type request using BODY, with Mastodon authorization. +Unless UNAUTHENTICATED-P is non-nil." `(let ((url-request-method ,method) (url-request-extra-headers (unless ,unauthenticated-p @@ -133,7 +134,7 @@ Authorization header is included by default unless UNAUTHENTICATED-P is non-nil. (defun mastodon-http--get (url) "Make synchronous GET request to URL. -Pass response buffer to CALLBACK function." +Pass response buffer to CALLBACK function." (mastodon-http--authorized-request "GET" (mastodon-http--url-retrieve-synchronously url))) -- cgit v1.2.3