[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/plz e14ea7327d 14/81: Tidying
From: |
ELPA Syncer |
Subject: |
[elpa] externals/plz e14ea7327d 14/81: Tidying |
Date: |
Wed, 11 May 2022 17:57:58 -0400 (EDT) |
branch: externals/plz
commit e14ea7327dd364d6a479ebd6a2455608455ecb0b
Author: Adam Porter <adam@alphapapa.net>
Commit: Adam Porter <adam@alphapapa.net>
Tidying
---
plz.el | 134 ++++++++++++++++++++++++++++++++++++++++++++++++-----------------
1 file changed, 100 insertions(+), 34 deletions(-)
diff --git a/plz.el b/plz.el
index 0b189d07b9..1e1a7bd5d7 100644
--- a/plz.el
+++ b/plz.el
@@ -186,12 +186,32 @@
;;;; Functions
+;;;;; Public
+
(cl-defun plz-get (url &key headers as then else
(connect-timeout plz-connect-timeout)
(decode t))
"Get HTTP URL with curl.
-FIXME: Docstring.
+AS selects the kind of result to pass to the callback function
+THEN. It may be:
+
+- `buffer' to pass the response buffer.
+- `string' to pass the response body as a 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.
+
+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
@@ -208,7 +228,18 @@ the initial connection attempt."
(decode t))
"Get HTTP URL with curl synchronously.
-FIXME: Docstring.
+AS selects the kind of result to return. It may be:
+
+- `string' to pass the response body as a 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.
+
+If the request fails, an error is signaled, 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
@@ -220,11 +251,35 @@ the initial connection attempt."
:decode decode
:as as))
+;;;;; Private
+
+;;;;;; Curl
+
+;; Functions for calling and handling curl processes.
+
(cl-defun plz--request (_method url &key headers connect-timeout
decode as then else)
- "Return curl process for HTTP request to URL.
+ "Get HTTP 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.
+- `string' to pass the response body as a 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').
-FIXME: Docstring.
+If DECODE is non-nil, the response body is decoded automatically.
+
+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
@@ -268,8 +323,25 @@ the initial connection attempt."
(cl-defun plz--request-sync (_method url &key headers connect-timeout
decode as)
- "Return HTTP response for curl called with CURL-ARGS.
-FIXME: Docstring.
+ "Return result for HTTP request to URL made synchronously with curl.
+
+AS selects the kind of result to return. It may be:
+
+- `string' to pass the response body as a 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.
+
+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.
+
+If the request fails, an error is signaled, either
+`plz-curl-error' or `plz-http-error', as appropriate, with a
+`plz-error' struct as the error data.
+
Uses `call-process' to call curl synchronously."
(with-current-buffer (generate-new-buffer " *plz-request-curl*")
(let* ((coding-system-for-read 'binary)
@@ -285,12 +357,12 @@ Uses `call-process' to call curl synchronously."
;; THEn form copied from `plz--request'.
;; TODO: DRY this. Maybe we could use a thread and a condition
variable, but...
(plz-then (pcase-exhaustive as
- ('string (lambda ()
- (let ((coding-system (or
(plz--coding-system) 'utf-8)))
- (plz--narrow-to-body)
- (when decode
- (decode-coding-region (point)
(point-max) coding-system))
- (buffer-string))))
+ ((or `nil 'string) (lambda ()
+ (let ((coding-system (or
(plz--coding-system) 'utf-8)))
+ (plz--narrow-to-body)
+ (when decode
+ (decode-coding-region (point)
(point-max) coding-system))
+ (buffer-string))))
('response #'plz--response)
((pred functionp) (lambda ()
(let ((coding-system (or
(plz--coding-system) 'utf-8)))
@@ -300,12 +372,6 @@ Uses `call-process' to call curl synchronously."
(funcall as)))))))
(plz--sentinel (current-buffer) status))))
-(defun plz--narrow-to-body ()
- "Narrow to body of HTTP response in current buffer."
- (goto-char (point-min))
- (re-search-forward "^\r\n" nil)
- (narrow-to-region (point) (point-max)))
-
(defun plz--sentinel (process-or-buffer status)
"Process buffer of curl output in PROCESS-OR-BUFFER.
If PROCESS-OR-BUFFER if a process, uses its buffer; if a buffer,
@@ -340,13 +406,9 @@ node `(elisp) Sentinels'). Kills the buffer before
returning."
((pred functionp) (funcall plz-else err)))))))
(kill-buffer buffer))))
-(defun plz--http-status ()
- "Return HTTP status code for HTTP response in current buffer.
-Assumes point is at beginning of buffer."
- (save-excursion
- (goto-char (point-min))
- (when (looking-at plz-http-response-status-line-regexp)
- (string-to-number (match-string 2)))))
+;;;;;; HTTP Responses
+
+;; Functions for parsing HTTP responses.
(defun plz--response ()
"Return response struct for HTTP response in current buffer."
@@ -375,6 +437,14 @@ refer to rather than the current buffer's unparsed
headers."
(when content-type
(coding-system-from-name content-type))))
+(defun plz--http-status ()
+ "Return HTTP status code for HTTP response in current buffer.
+Assumes point is at beginning of buffer."
+ (save-excursion
+ (goto-char (point-min))
+ (when (looking-at plz-http-response-status-line-regexp)
+ (string-to-number (match-string 2)))))
+
(defun plz--headers ()
"Return headers alist for HTTP response in current buffer"
(save-excursion
@@ -388,15 +458,11 @@ refer to rather than the current buffer's unparsed
headers."
limit t)
collect (cons (match-string 1) (match-string 2))))))
-(defun plz--decode-body (coding-system)
- "Decode body for HTTP response in current buffer.
-Return length of decoded text. Decodes with
-`decode-coding-region' according to CODING-SYSTEM."
- (save-excursion
- (goto-char (point-min))
- ;; Skip headers.
- (re-search-forward "^\r\n" nil)
- (decode-coding-region (point) (point-max) coding-system)))
+(defun plz--narrow-to-body ()
+ "Narrow to body of HTTP response in current buffer."
+ (goto-char (point-min))
+ (re-search-forward "^\r\n" nil)
+ (narrow-to-region (point) (point-max)))
;;;; Footer
- [elpa] branch externals/plz created (now 9156de9c59), ELPA Syncer, 2022/05/11
- [elpa] externals/plz e7d9513bb9 03/81: Rename, ELPA Syncer, 2022/05/11
- [elpa] externals/plz c7c16e2123 04/81: Changes, ELPA Syncer, 2022/05/11
- [elpa] externals/plz debfc864c8 08/81: Changes, ELPA Syncer, 2022/05/11
- [elpa] externals/plz f0a0d114a6 01/81: Initial commit, ELPA Syncer, 2022/05/11
- [elpa] externals/plz 2d20ca5030 02/81: Changes, ELPA Syncer, 2022/05/11
- [elpa] externals/plz 5f54b5ebec 06/81: Changes, ELPA Syncer, 2022/05/11
- [elpa] externals/plz 1016279174 07/81: Changes, ELPA Syncer, 2022/05/11
- [elpa] externals/plz 897c5bbfb2 13/81: Improvements, error handling, ELPA Syncer, 2022/05/11
- [elpa] externals/plz e14ea7327d 14/81: Tidying,
ELPA Syncer <=
- [elpa] externals/plz 2b7944e0f9 05/81: Changes, ELPA Syncer, 2022/05/11
- [elpa] externals/plz 950a5498cb 16/81: Tidy, ELPA Syncer, 2022/05/11
- [elpa] externals/plz 44e6f02064 17/81: Binary content, ELPA Syncer, 2022/05/11
- [elpa] externals/plz 5bd683ead8 19/81: Tidy tests, ELPA Syncer, 2022/05/11
- [elpa] externals/plz 3249835c8b 18/81: Tidy tests, ELPA Syncer, 2022/05/11
- [elpa] externals/plz 21a7318f1c 26/81: Tests: Tidy/rename, ELPA Syncer, 2022/05/11
- [elpa] externals/plz 3dd2598fe9 24/81: Tidy, ELPA Syncer, 2022/05/11
- [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