[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/plz a5f22b23e1 42/81: Add: (plz)
From: |
ELPA Syncer |
Subject: |
[elpa] externals/plz a5f22b23e1 42/81: Add: (plz) |
Date: |
Wed, 11 May 2022 17:58:00 -0400 (EDT) |
branch: externals/plz
commit a5f22b23e182cd9365a6ccefdcb96b0ca51df374
Author: Adam Porter <adam@alphapapa.net>
Commit: Adam Porter <adam@alphapapa.net>
Add: (plz)
Not sure which I like better, but this works.
---
plz.el | 46 +++++++++++++++++++++++++++++++++++++++++++---
tests/test-plz.el | 33 +++++++++++++++++++++++++--------
2 files changed, 68 insertions(+), 11 deletions(-)
diff --git a/plz.el b/plz.el
index e92742ba9d..551330343d 100644
--- a/plz.el
+++ b/plz.el
@@ -265,6 +265,45 @@ 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
+ (connect-timeout plz-connect-timeout)
+ (decode t decode-s))
+ "Request BODY with METHOD 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 method 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))
@@ -338,9 +377,10 @@ 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"))
+ (pcase method
+ ((or 'put 'post)
+ (cl-assert body)
+ (list "--data" "@-" "--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 23ecefd7ff..cd055b4cc7 100644
--- a/tests/test-plz.el
+++ b/tests/test-plz.el
@@ -68,7 +68,7 @@
(ert-deftest plz-get-string nil
(let* ((test-string)
- (process (plz-get "https://httpbin.org/get"
+ (process (plz 'get "https://httpbin.org/get"
:as 'string
:then (lambda (string)
(setf test-string string)))))
@@ -78,7 +78,7 @@
(ert-deftest plz-get-buffer nil
;; The sentinel kills the buffer, so we get the buffer as a string.
(let* ((test-buffer-string)
- (process (plz-get "https://httpbin.org/get"
+ (process (plz 'get "https://httpbin.org/get"
:as 'buffer
:then (lambda (buffer)
(with-current-buffer buffer
@@ -88,7 +88,7 @@
(ert-deftest plz-get-response nil
(let* ((test-response)
- (process (plz-get "https://httpbin.org/get"
+ (process (plz 'get "https://httpbin.org/get"
:as 'response
:then (lambda (response)
(setf test-response response)))))
@@ -97,7 +97,7 @@
(ert-deftest plz-get-json nil
(let* ((test-json)
- (process (plz-get "https://httpbin.org/get"
+ (process (plz 'get "https://httpbin.org/get"
:as #'json-read
:then (lambda (json)
(setf test-json json)))))
@@ -105,11 +105,28 @@
(let-alist test-json
(should (string-match "curl" .headers.User-Agent)))))
+(ert-deftest plz-post-json-string nil
+ (let* ((json-string (json-encode (list (cons "key" "value"))))
+ (response-json)
+ (process (plz 'post "https://httpbin.org/post"
+ :headers '(("Content-Type" . "application/json"))
+ :body json-string
+ :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: POST JSON buffer.
+
(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
+ (process (plz 'put "https://httpbin.org/put"
:headers '(("Content-Type" . "application/json"))
+ :body json-string
:as #'json-read
:then (lambda (json)
(setf response-json json)))))
@@ -146,7 +163,7 @@
(ert-deftest plz-get-curl-error nil
;; Async.
(let* ((err)
- (process (plz-get "https://httpbinnnnnn.org/get/status/404"
+ (process (plz 'get "https://httpbinnnnnn.org/get/status/404"
:as 'string
:else (lambda (e)
(setf err e)))))
@@ -167,7 +184,7 @@
(ert-deftest plz-get-404-error nil
;; Async.
(let* ((err)
- (process (plz-get "https://httpbin.org/get/status/404"
+ (process (plz 'get "https://httpbin.org/get/status/404"
:as 'string
:else (lambda (e)
(setf err e)))))
@@ -189,7 +206,7 @@
(ert-deftest plz-get-jpeg ()
(let* ((test-jpeg)
- (process (plz-get "https://httpbin.org/image/jpeg"
+ (process (plz 'get "https://httpbin.org/image/jpeg"
:as 'binary
:then (lambda (string)
(setf test-jpeg string)))))
- [elpa] externals/plz 2c19b7f11d 11/81: Changes, (continued)
- [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, 2022/05/11
- [elpa] externals/plz a5f22b23e1 42/81: Add: (plz),
ELPA Syncer <=
- [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
- [elpa] externals/plz dd92f48895 65/81: Change: Default to :then 'sync and :as 'string, ELPA Syncer, 2022/05/11