[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/plz 4a246e24f6 54/81: Add: Upload binary files
From: |
ELPA Syncer |
Subject: |
[elpa] externals/plz 4a246e24f6 54/81: Add: Upload binary files |
Date: |
Wed, 11 May 2022 17:58:01 -0400 (EDT) |
branch: externals/plz
commit 4a246e24f6d0d7568151409638dad1bfb24b5f61
Author: Adam Porter <adam@alphapapa.net>
Commit: Adam Porter <adam@alphapapa.net>
Add: Upload binary files
---
plz.el | 16 +++++++++++++---
tests/test-plz.el | 11 +++++++----
2 files changed, 20 insertions(+), 7 deletions(-)
diff --git a/plz.el b/plz.el
index c7b50b667d..e547a7f85f 100644
--- a/plz.el
+++ b/plz.el
@@ -274,6 +274,7 @@ NOQUERY is passed to `make-process', which see."
:as as :then then :else else :noquery noquery))
(cl-defun plz (method url &key headers body as then else finally noquery
+ (body-type 'text)
(connect-timeout plz-connect-timeout)
(decode t decode-s))
"Request BODY with METHOD to URL with curl.
@@ -308,10 +309,13 @@ 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.
+BODY-TYPE may be `text' to send BODY as text, or `binary' to send
+it as binary.
+
NOQUERY is passed to `make-process', which see."
(declare (indent defun))
(plz--curl method url
- :body body
+ :body body :body-type body-type
:headers headers
:connect-timeout connect-timeout
:decode (if (and decode-s (not decode)) nil decode)
@@ -355,7 +359,8 @@ 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 finally noquery)
+ decode as then else finally noquery
+ (body-type 'text))
"Make HTTP METHOD request to URL with curl.
AS selects the kind of result to pass to the callback function
@@ -383,6 +388,8 @@ 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.
+BODY-TYPE may be `text' to send BODY as text, or `binary' to send
+it as binary.
HEADERS may be an alist of extra headers to send with the
request. CONNECT-TIMEOUT may be a number of seconds to timeout
@@ -399,13 +406,16 @@ NOQUERY is passed to `make-process', which see."
(push (cons "Expect" "") headers)
(let* ((header-args (cl-loop for (key . value) in headers
append (list "--header" (format "%s: %s" key
value))))
+ (data-arg (pcase-exhaustive body-type
+ ('binary "--data-binary")
+ ('text "--data")))
(curl-args (append plz-curl-default-args header-args
(when connect-timeout
(list "--connect-timeout" (number-to-string
connect-timeout)))
(pcase method
((or 'put 'post)
(cl-assert body)
- (list "--data" "@-" "--request" (upcase
(symbol-name method)))))
+ (list data-arg "@-" "--request" (upcase
(symbol-name method)))))
(list url)))
(decode (pcase as
('binary nil)
diff --git a/tests/test-plz.el b/tests/test-plz.el
index d622fabc08..50f79bc88b 100644
--- a/tests/test-plz.el
+++ b/tests/test-plz.el
@@ -120,20 +120,23 @@
(should (string= "value" (alist-get 'key (json-read-from-string
.data)))))))
(ert-deftest plz-post-jpeg-string nil
- (let* ((jpeg-string (plz-get-sync "https://httpbin.org/image/jpeg"
- :as 'binary))
+ (let* ((jpeg-to-upload (plz-get-sync "https://httpbin.org/image/jpeg"
+ :as 'binary))
(response-jpeg)
(process (plz 'post "https://httpbin.org/post"
:headers '(("Content-Type" . "image/jpeg"))
- :body jpeg-string
+ :body jpeg-to-upload :body-type 'binary
:as #'json-read
:then (lambda (json)
(setf response-jpeg
(base64-decode-string
(string-remove-prefix
"data:application/octet-stream;base64,"
(alist-get 'data
json))))))))
+ (should (equal 'jpeg (image-type-from-data jpeg-to-upload)))
(plz-test-wait process)
- (should (equal 'jpeg (image-type-from-data response-jpeg)))))
+ (should (equal 'jpeg (image-type-from-data response-jpeg)))
+ (should (equal (length jpeg-to-upload) (length response-jpeg)))
+ (should (equal jpeg-to-upload response-jpeg))))
;; TODO: POST JSON buffer.
- [elpa] externals/plz 19a0110109 33/81: Notes: Add ToC, (continued)
- [elpa] externals/plz 19a0110109 33/81: Notes: Add ToC, ELPA Syncer, 2022/05/11
- [elpa] externals/plz 9a1b119eff 38/81: Meta: Ignore sandbox/, ELPA Syncer, 2022/05/11
- [elpa] externals/plz 0301272d8d 40/81: Add: plz-put, ELPA Syncer, 2022/05/11
- [elpa] externals/plz a5f22b23e1 42/81: Add: (plz), ELPA Syncer, 2022/05/11
- [elpa] externals/plz 430ceffd1d 43/81: Change: Handle killed processes, ELPA Syncer, 2022/05/11
- [elpa] externals/plz f40d3ecbdd 47/81: Add/Change: :noquery argument for make-process, ELPA Syncer, 2022/05/11
- [elpa] externals/plz 1884d038ae 46/81: Notes: Add note about Lars Ingebrigtsen's with-url macro, ELPA Syncer, 2022/05/11
- [elpa] externals/plz 7478d43668 51/81: Revert "Add/Change: Handle LF-only HTTP responses", ELPA Syncer, 2022/05/11
- [elpa] externals/plz 8d2654bba7 49/81: Add/Change: Handle LF-only HTTP responses, ELPA Syncer, 2022/05/11
- [elpa] externals/plz 220f882ee4 53/81: Tests: (plz-post-jpeg-string) Add, ELPA Syncer, 2022/05/11
- [elpa] externals/plz 4a246e24f6 54/81: Add: Upload binary files,
ELPA Syncer <=
- [elpa] externals/plz 7c27e4bdcd 61/81: Change: Sync with accept-process-output, ELPA Syncer, 2022/05/11
- [elpa] externals/plz 65030d5cc1 64/81: Tidy: Docstring, ELPA Syncer, 2022/05/11
- [elpa] externals/plz dd92f48895 65/81: Change: Default to :then 'sync and :as 'string, ELPA Syncer, 2022/05/11
- [elpa] externals/plz cddccccf81 67/81: Docs: Fix link, ELPA Syncer, 2022/05/11
- [elpa] externals/plz f1a89a8816 68/81: Add: Download :as file, ELPA Syncer, 2022/05/11
- [elpa] externals/plz 8cd1ef481d 73/81: Notes: Add links to skeeto's feedback, ELPA Syncer, 2022/05/11
- [elpa] externals/plz dd491941ab 79/81: Meta: Update makem.sh, ELPA Syncer, 2022/05/11
- [elpa] externals/plz da503527d2 76/81: Change: (plz--response) Error if unable to parse HTTP response, ELPA Syncer, 2022/05/11
- [elpa] externals/plz 0a860d94dc 71/81: Tests: "There be dragons.", ELPA Syncer, 2022/05/11
- [elpa] externals/plz ce7afc17f9 63/81: Meta: Ignore .sandbox/, ELPA Syncer, 2022/05/11