[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/plz 7eff40f276 44/81: Add: plz-finally
From: |
ELPA Syncer |
Subject: |
[elpa] externals/plz 7eff40f276 44/81: Add: plz-finally |
Date: |
Wed, 11 May 2022 17:58:00 -0400 (EDT) |
branch: externals/plz
commit 7eff40f27686fb2e61bebc43f66c39e00e51a711
Author: Adam Porter <adam@alphapapa.net>
Commit: Adam Porter <adam@alphapapa.net>
Add: plz-finally
---
plz.el | 28 +++++++++++++++++++++-------
tests/test-plz.el | 12 ++++++++++++
2 files changed, 33 insertions(+), 7 deletions(-)
diff --git a/plz.el b/plz.el
index e9b8d90d20..cc23f0e29e 100644
--- a/plz.el
+++ b/plz.el
@@ -161,6 +161,10 @@
(defvar-local plz-then nil
"Callback function for successful completion of request in current curl
process buffer.")
+(defvar-local plz-finally nil
+ "Function called unconditionally after completion of request, after the
then/else function.
+It is called without arguments outside the curl process buffer.")
+
;;;; Customization
(defgroup plz nil
@@ -265,7 +269,7 @@ the initial connection attempt."
:decode (if (and decode-s (not decode)) nil decode)
:as as :then then :else else))
-(cl-defun plz (method url &key headers body as then else
+(cl-defun plz (method url &key headers body as then else finally
(connect-timeout plz-connect-timeout)
(decode t decode-s))
"Request BODY with METHOD to URL with curl.
@@ -293,6 +297,9 @@ an error is signaled when the request fails, either
`plz-curl-error' or `plz-http-error' as appropriate, with a
`plz-error' struct as the error data.
+FINALLY is an optional function called without argument after
+THEN or ELSE, as appropriate.
+
HEADERS may be an alist of extra headers to send with the
request. CONNECT-TIMEOUT may be a number of seconds to timeout
the initial connection attempt."
@@ -302,7 +309,7 @@ the initial connection attempt."
:headers headers
:connect-timeout connect-timeout
:decode (if (and decode-s (not decode)) nil decode)
- :as as :then then :else else))
+ :as as :then then :else else :finally finally))
(cl-defun plz-get-sync (url &key headers as
(connect-timeout plz-connect-timeout)
@@ -342,7 +349,7 @@ the initial connection attempt."
;; Functions for calling and handling curl processes.
(cl-defun plz--curl (method url &key body headers connect-timeout
- decode as then else)
+ decode as then else finally)
"Make HTTP METHOD request to URL with curl.
AS selects the kind of result to pass to the callback function
@@ -366,6 +373,9 @@ an error is signaled when the request fails, either
`plz-curl-error' or `plz-http-error' as appropriate, with a
`plz-error' struct as the error data.
+FINALLY is an optional function called without argument after
+THEN or ELSE, as appropriate.
+
BODY may be a string or buffer to send as the request body.
HEADERS may be an alist of extra headers to send with the
@@ -413,7 +423,8 @@ the initial connection attempt."
(decode-coding-region (point)
(point-max) coding-system))
(funcall then (funcall as))))))))
(setf plz-then then
- plz-else else)
+ plz-else else
+ plz-finally finally)
(when body
(cl-typecase body
(string (process-send-string process body)
@@ -486,9 +497,10 @@ If PROCESS-OR-BUFFER if a process, uses its buffer; if a
buffer,
uses it. STATUS should be the process's event string (see info
node `(elisp) Sentinels'). Kills the buffer before returning."
;; Inspired by and some code copied from `elfeed-curl--sentinel'.
- (let ((buffer (cl-etypecase process-or-buffer
- (process (process-buffer process-or-buffer))
- (buffer process-or-buffer))))
+ (let* ((buffer (cl-etypecase process-or-buffer
+ (process (process-buffer process-or-buffer))
+ (buffer process-or-buffer)))
+ (finally (buffer-local-value 'plz-finally buffer)))
(unwind-protect
(with-current-buffer buffer
(pcase-exhaustive status
@@ -519,6 +531,8 @@ node `(elisp) Sentinels'). Kills the buffer before
returning."
(pcase-exhaustive plz-else
(`nil (signal 'plz-curl-error err))
((pred functionp) (funcall plz-else err)))))))
+ (when finally
+ (funcall finally))
(kill-buffer buffer))))
;;;;;; HTTP Responses
diff --git a/tests/test-plz.el b/tests/test-plz.el
index 925175ec8c..8ef261c3b8 100644
--- a/tests/test-plz.el
+++ b/tests/test-plz.el
@@ -221,6 +221,18 @@
(plz-response-p (plz-error-response (cdr err)))
(eq 404 (plz-response-status (plz-error-response (cdr
err))))))))
+;;;;; Finally
+
+(ert-deftest plz-get-finally nil
+ (let* ((finally-null t)
+ (process (plz 'get "https://httpbin.org/get"
+ :as 'string
+ :then #'ignore
+ :finally (lambda ()
+ (setf finally-null nil)))))
+ (plz-test-wait process)
+ (should-not finally-null)))
+
;;;;; Binary
(ert-deftest plz-get-jpeg ()
- [elpa] externals/plz 3cc6747035 28/81: Meta: Add comm keyword, (continued)
- [elpa] externals/plz 3cc6747035 28/81: Meta: Add comm keyword, ELPA Syncer, 2022/05/11
- [elpa] externals/plz 51404566fa 29/81: Notes: Add fetch.el, ELPA Syncer, 2022/05/11
- [elpa] externals/plz b77283748b 31/81: Meta: Update makem.sh, ELPA Syncer, 2022/05/11
- [elpa] externals/plz 56c66c4421 32/81: Add/Change: :as 'binary, ELPA Syncer, 2022/05/11
- [elpa] externals/plz 1bb701f1c1 36/81: Change/Fix: Downcase and intern header keys, ELPA Syncer, 2022/05/11
- [elpa] externals/plz 6f8dfb83de 34/81: Tests: (plz-test-get-response) Change to macro, ELPA Syncer, 2022/05/11
- [elpa] externals/plz baf93bb80a 39/81: Fix: (plz--curl) Header args, ELPA Syncer, 2022/05/11
- [elpa] externals/plz 33438b7105 35/81: CI: Enable testing of all branches, ELPA Syncer, 2022/05/11
- [elpa] externals/plz 439fb383d9 37/81: Notes: Add idea about finalizers, ELPA Syncer, 2022/05/11
- [elpa] externals/plz 65dbc0f219 41/81: Change/Fix: (plz--sentinel) Use exhaustive pcase, ELPA Syncer, 2022/05/11
- [elpa] externals/plz 7eff40f276 44/81: Add: plz-finally,
ELPA Syncer <=
- [elpa] externals/plz 27010c20e1 45/81: Meta: Update makem.sh, etc., ELPA Syncer, 2022/05/11
- [elpa] externals/plz f34efa052c 48/81: Fix: set-buffer-multibyte nil for binary, ELPA Syncer, 2022/05/11
- [elpa] externals/plz e977a046a3 50/81: Change: (plz-http-response-status-line-regexp) Match at BOL, ELPA Syncer, 2022/05/11
- [elpa] externals/plz f12f907719 52/81: Change/Fix: (plz--curl) Disable Expect header, ELPA Syncer, 2022/05/11
- [elpa] externals/plz 701e79c3e8 55/81: Tidy: Remove obsolete functions, ELPA Syncer, 2022/05/11
- [elpa] externals/plz b412258c9d 56/81: Add: :timeout argument (curl's --max-time), ELPA Syncer, 2022/05/11
- [elpa] externals/plz 87007fc612 57/81: Add: FIXMEs, ELPA Syncer, 2022/05/11
- [elpa] externals/plz 709a14740f 58/81: Tidy: Docstrings, ELPA Syncer, 2022/05/11
- [elpa] externals/plz 93a2ccaaa8 59/81: Fix: Run curl in temporary-file-directory, ELPA Syncer, 2022/05/11
- [elpa] externals/plz f2176dc56c 60/81: Change/Fix: Avoid command-line arguments, ELPA Syncer, 2022/05/11