branch: externals/plz commit 42584e1af5618a370e5ad2ffac16672733214941 Author: Adam Porter <a...@alphapapa.net> Commit: Adam Porter <a...@alphapapa.net>
Merge: (plz) :body (file FILENAME) And docstring fix. --- README.org | 10 +++++++++- plz.el | 10 +++++++++- plz.info | 50 +++++++++++++++++++++++++++++++------------------- tests/test-plz.el | 18 ++++++++++++++++++ 4 files changed, 67 insertions(+), 21 deletions(-) diff --git a/README.org b/README.org index 6c08b4f204..2a486f0806 100644 --- a/README.org +++ b/README.org @@ -46,6 +46,8 @@ The main public function is ~plz~, which sends an HTTP request and returns either the result of the specified type (for a synchronous request), or the ~curl~ process object (for asynchronous requests). For asynchronous requests, callback, error-handling, and finalizer functions may be specified, as well as various other options. +# TODO: Add ":body (file FILENAME)" to docs. + ** Examples Synchronously =GET= a URL and return the response body as a decoded string (here, raw JSON): @@ -108,6 +110,8 @@ Synchronously download a JPEG file, then create an Emacs image object from the d ~HEADERS~ may be an alist of extra headers to send with the request. + ~BODY~ may be a string, a buffer, or a list like ~(file FILENAME)~ to upload a file from disk. + ~BODY-TYPE~ may be ~text~ to send ~BODY~ as text, or ~binary~ to send it as binary. ~AS~ selects the kind of result to pass to the callback function ~THEN~, or the kind of result to return for synchronous requests. It may be: @@ -175,7 +179,11 @@ You may also clear a queue with ~plz-clear~, which cancels any active or queued ** 0.6-pre -Nothing new yet. +*Additions* ++ Function ~plz~'s ~:body~ argument now accepts a list like ~(file FILENAME)~ to upload a file from disk (by passing the filename to curl, rather than reading its content into Emacs and sending it to curl through the pipe). + +*Fixes* ++ Function ~plz~'s docstring now mentions that the ~:body~ argument may also be a buffer (an intentional feature that was accidentally undocumented). ** 0.5.4 diff --git a/plz.el b/plz.el index 108533d45f..c66f486e89 100644 --- a/plz.el +++ b/plz.el @@ -278,6 +278,9 @@ selected result. HEADERS may be an alist of extra headers to send with the request. +BODY may be a string, a buffer, or a list like `(file FILENAME)' +to upload a file from disk. + BODY-TYPE may be `text' to send BODY as text, or `binary' to send it as binary. @@ -369,7 +372,12 @@ NOQUERY is passed to `make-process', which see." (cons "--request" (upcase (symbol-name method))) ;; It appears that this must be the last argument ;; in order to pass data on the rest of STDIN. - (cons data-arg "@-"))) + (pcase body + (`(file ,filename) + ;; Use `expand-file-name' because curl doesn't + ;; expand, e.g. "~" into "/home/...". + (cons "--upload-file" (expand-file-name filename))) + (_ (cons data-arg "@-"))))) ('delete (list (cons "--dump-header" "-") (cons "--request" (upcase (symbol-name method))))) diff --git a/plz.info b/plz.info index 5ce233a9ee..357e1c98a9 100644 --- a/plz.info +++ b/plz.info @@ -175,6 +175,9 @@ File: README.info, Node: Functions, Next: Queueing, Prev: Examples, Up: Usag ‘HEADERS’ may be an alist of extra headers to send with the request. + ‘BODY’ may be a string, a buffer, or a list like ‘(file FILENAME)’ + to upload a file from disk. + ‘BODY-TYPE’ may be ‘text’ to send ‘BODY’ as text, or ‘binary’ to send it as binary. @@ -294,7 +297,16 @@ File: README.info, Node: 06-pre, Next: 054, Up: Changelog 3.1 0.6-pre =========== -Nothing new yet. +*Additions* + • Function ‘plz’’s ‘:body’ argument now accepts a list like ‘(file + FILENAME)’ to upload a file from disk (by passing the filename to + curl, rather than reading its content into Emacs and sending it to + curl through the pipe). + + *Fixes* + • Function ‘plz’’s docstring now mentions that the ‘:body’ argument + may also be a buffer (an intentional feature that was accidentally + undocumented). File: README.info, Node: 054, Next: 053, Prev: 06-pre, Up: Changelog @@ -477,24 +489,24 @@ Node: Manual1566 Node: Usage1872 Node: Examples2373 Node: Functions3740 -Node: Queueing6734 -Node: Tips7992 -Node: Changelog8293 -Node: 06-pre8551 -Node: 0548658 -Node: 0538905 -Node: 0529221 -Node: 0519428 -Node: 059680 -Node: 049886 -Node: 0310792 -Node: 02111240 -Node: 0211389 -Node: 0111520 -Node: Credits11616 -Node: Development11982 -Node: Copyright assignment12496 -Node: License13084 +Node: Queueing6848 +Node: Tips8106 +Node: Changelog8407 +Node: 06-pre8665 +Node: 0549212 +Node: 0539459 +Node: 0529775 +Node: 0519982 +Node: 0510234 +Node: 0410440 +Node: 0311346 +Node: 02111794 +Node: 0211943 +Node: 0112074 +Node: Credits12170 +Node: Development12536 +Node: Copyright assignment13050 +Node: License13638 End Tag Table diff --git a/tests/test-plz.el b/tests/test-plz.el index 05001435ef..f78f6e4417 100644 --- a/tests/test-plz.el +++ b/tests/test-plz.el @@ -503,6 +503,24 @@ (when (file-exists-p filename) (delete-file filename))))) +(plz-deftest plz-upload-file-by-name () + (let ((filename (make-temp-file "plz-")) + response-json process) + (unwind-protect + (progn + (with-temp-file filename + (insert "deadbeef")) + (setf process + (plz 'put "https://httpbin.org/put" + :body `(file ,filename) + :as #'json-read + :then (lambda (json) + (setf response-json json)))) + (plz-test-wait process) + (should (equal "deadbeef" (alist-get 'data response-json))) + (should-not (alist-get 'files response-json))) + (delete-file filename)))) + ;;;;; Queue ;; TODO: Test that limit is enforced (though it seems to work fine).