[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/plz 0301272d8d 40/81: Add: plz-put
From: |
ELPA Syncer |
Subject: |
[elpa] externals/plz 0301272d8d 40/81: Add: plz-put |
Date: |
Wed, 11 May 2022 17:58:00 -0400 (EDT) |
branch: externals/plz
commit 0301272d8d72691b807e44402f6ea801fdca6d5d
Author: Adam Porter <adam@alphapapa.net>
Commit: Adam Porter <adam@alphapapa.net>
Add: plz-put
---
plz.el | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++---
tests/test-plz.el | 15 +++++++++++++++
2 files changed, 69 insertions(+), 3 deletions(-)
diff --git a/plz.el b/plz.el
index 3844b2c3ef..22f0d232df 100644
--- a/plz.el
+++ b/plz.el
@@ -226,6 +226,45 @@ the initial connection attempt."
:decode (if (and decode-s (not decode)) nil decode)
:as as :then then :else else))
+(cl-defun plz-put (url body &key headers as then else
+ (connect-timeout plz-connect-timeout)
+ (decode t decode-s))
+ "PUT BODY to URL with curl.
+
+AS selects the kind of result to pass to the callback function
+THEN. It may be:
+
+- `buffer' to pass the response buffer.
+- `binary' to pass the response body as an undecoded string.
+- `string' to pass the response body as a decoded string.
+- `response' to pass a `plz-response' struct.
+- A function, which is called in the response buffer with it
+ narrowed to the response body (suitable for, e.g. `json-read').
+
+If DECODE is non-nil, the response body is decoded automatically.
+For binary content, it should be nil. When AS is `binary',
+DECODE is automatically set to nil.
+
+THEN is a callback function, whose sole argument is selected
+above with AS.
+
+ELSE is an optional callback function called when the request
+fails with one argument, a `plz-error' struct. If ELSE is nil,
+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.
+
+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."
+ (declare (indent defun))
+ (plz--curl 'put url
+ :body body
+ :headers headers
+ :connect-timeout connect-timeout
+ :decode (if (and decode-s (not decode)) nil decode)
+ :as as :then then :else else))
+
(cl-defun plz-get-sync (url &key headers as
(connect-timeout plz-connect-timeout)
(decode t decode-s))
@@ -263,9 +302,9 @@ the initial connection attempt."
;; Functions for calling and handling curl processes.
-(cl-defun plz--curl (_method url &key headers connect-timeout
- decode as then else)
- "Get HTTP URL with curl.
+(cl-defun plz--curl (method url &key body headers connect-timeout
+ decode as then else)
+ "Make HTTP METHOD request to URL with curl.
AS selects the kind of result to pass to the callback function
THEN. It may be:
@@ -288,6 +327,8 @@ 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.
+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
request. CONNECT-TIMEOUT may be a number of seconds to timeout
the initial connection attempt."
@@ -297,6 +338,9 @@ the initial connection attempt."
(curl-args (append plz-curl-default-args header-args
(when connect-timeout
(list "--connect-timeout" (number-to-string
connect-timeout)))
+ (when (eq 'put method)
+ (cl-assert body)
+ (list "--data" "@-" "--request" "PUT"))
(list url)))
(decode (pcase as
('binary nil)
@@ -330,6 +374,13 @@ the initial connection attempt."
(funcall then (funcall as))))))))
(setf plz-then then
plz-else else)
+ (when body
+ (cl-typecase body
+ (string (process-send-string process body)
+ (process-send-eof process))
+ (buffer (with-current-buffer body
+ (process-send-region process (point-min) (point-max))
+ (process-send-eof process)))))
process))))
(cl-defun plz--curl-sync (_method url &key headers connect-timeout
diff --git a/tests/test-plz.el b/tests/test-plz.el
index 96adc12808..23ecefd7ff 100644
--- a/tests/test-plz.el
+++ b/tests/test-plz.el
@@ -105,6 +105,21 @@
(let-alist test-json
(should (string-match "curl" .headers.User-Agent)))))
+(ert-deftest plz-put-json-string nil
+ (let* ((json-string (json-encode (list (cons "key" "value"))))
+ (response-json)
+ (process (plz-put "https://httpbin.org/put" json-string
+ :headers '(("Content-Type" . "application/json"))
+ :as #'json-read
+ :then (lambda (json)
+ (setf response-json json)))))
+ (plz-test-wait process)
+ (let-alist response-json
+ (should (string-match "curl" .headers.User-Agent))
+ (should (string= "value" (alist-get 'key (json-read-from-string
.data)))))))
+
+;; TODO: Put JSON buffer.
+
;;;;; Sync
(ert-deftest plz-get-string-sync nil
- [elpa] externals/plz 4588884c11 10/81: Changes, (continued)
- [elpa] externals/plz 4588884c11 10/81: Changes, ELPA Syncer, 2022/05/11
- [elpa] externals/plz 2c19b7f11d 11/81: Changes, ELPA Syncer, 2022/05/11
- [elpa] externals/plz 6a21c7e809 12/81: Significant changes, ELPA Syncer, 2022/05/11
- [elpa] externals/plz 5b00a61ce3 15/81: Rename functions, ELPA Syncer, 2022/05/11
- [elpa] externals/plz 370d0bbc1a 21/81: Tidy, ELPA Syncer, 2022/05/11
- [elpa] externals/plz 971077e1d3 23/81: Tests, ELPA Syncer, 2022/05/11
- [elpa] externals/plz 30e48b1e6a 22/81: Tidy, ELPA Syncer, 2022/05/11
- [elpa] externals/plz 05f93b0b6b 25/81: Meta: Update Makefile, makem.sh, ELPA Syncer, 2022/05/11
- [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 <=
- [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, 2022/05/11
- [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